Skip to content

fix(@angular-devkit/build-angular): correctly set i18n subPath in webpack browser builder #30253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ export async function inlineLocales(options: InlineOptions) {
if (!transformResult || !transformResult.code) {
throw new Error(`Unknown error occurred processing bundle for "${options.filename}".`);
}

const subPath = i18n.locales[locale].subPath;
const outputPath = path.join(
options.outputPath,
i18n.flatOutput ? '' : locale,
i18n.flatOutput ? '' : subPath,
options.filename,
);
await fs.writeFile(outputPath, transformResult.code);
Expand Down Expand Up @@ -284,9 +284,10 @@ async function inlineLocalesDirect(ast: ParseResult, options: InlineOptions) {
source: string;
map: { file: string; sourceRoot?: string };
};
const subPath = i18n.locales[locale].subPath;
const outputPath = path.join(
options.outputPath,
i18n.flatOutput ? '' : locale,
i18n.flatOutput ? '' : subPath,
options.filename,
);
await fs.writeFile(outputPath, outputCode);
Expand All @@ -309,9 +310,10 @@ async function inlineCopyOnly(options: InlineOptions) {
}

for (const locale of i18n.inlineLocales) {
const subPath = i18n.locales[locale].subPath;
const outputPath = path.join(
options.outputPath,
i18n.flatOutput ? '' : locale,
i18n.flatOutput ? '' : subPath,
options.filename,
);
await fs.writeFile(outputPath, options.code);
Expand Down
73 changes: 73 additions & 0 deletions tests/legacy-cli/e2e/tests/i18n/ivy-localize-sub-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/

import { join } from 'node:path';
import { expectFileToMatch } from '../../utils/fs';
import { ng } from '../../utils/process';
import { updateJsonFile } from '../../utils/project';
import { baseDir, baseHrefs, externalServer, langTranslations, setupI18nConfig } from './setup';

export default async function () {
// Setup i18n tests and config.
await setupI18nConfig();

const URL_SUB_PATH: Record<string, string> = {
'en-US': '',
'fr': 'fr',
'de': 'deutsche',
};

// Update angular.json
await updateJsonFile('angular.json', (workspaceJson) => {
const appProject = workspaceJson.projects['test-project'];
const i18n: Record<string, any> = appProject.i18n;

i18n.sourceLocale = {
subPath: URL_SUB_PATH['en-US'],
};

i18n.locales['fr'] = {
translation: i18n.locales['fr'],
subPath: URL_SUB_PATH['fr'],
};

i18n.locales['de'] = {
translation: i18n.locales['de'],
subPath: URL_SUB_PATH['de'],
};
});

// Build each locale and verify the output.
await ng('build');
for (const { lang } of langTranslations) {
const subPath = URL_SUB_PATH[lang];
const baseHref = subPath ? `/${subPath}/` : '/';
const outputPath = join(baseDir, subPath);

// Verify the HTML lang attribute is present
await expectFileToMatch(`${outputPath}/index.html`, `lang="${lang}"`);

// Verify the HTML base HREF attribute is present
await expectFileToMatch(`${outputPath}/index.html`, `href="${baseHref}"`);

// Execute Application E2E tests for a production build without dev server
const { server, port, url } = await externalServer(outputPath, baseHref);

try {
await ng(
'e2e',
`--port=${port}`,
`--configuration=${lang}`,
'--dev-server-target=',
`--base-url=${url}`,
);
} finally {
server.close();
}
}
}