Skip to content

Commit 22880d9

Browse files
committed
fix(@angular-devkit/build-angular): correct set locale when using esbuild based builders
In some cases the `$localize.locale` was being set prior to `$localize` being set through `@angular/localize`. This caused `$localize.locale` to be reset to `en-US`. This change moves the order in which `$localize.locale` is set. Partially addresses: #26350 (cherry picked from commit 876ca1e)
1 parent a068067 commit 22880d9

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

packages/angular_devkit/build_angular/src/tools/esbuild/application-code-bundle.ts

+6-16
Original file line numberDiff line numberDiff line change
@@ -396,20 +396,6 @@ function getEsBuildCommonPolyfillsOptions(
396396
// Locale data should go first so that project provided polyfill code can augment if needed.
397397
let needLocaleDataPlugin = false;
398398
if (i18nOptions.shouldInline) {
399-
// When inlining, a placeholder is used to allow the post-processing step to inject the $localize locale identifier
400-
polyfills.unshift('angular:locale/placeholder');
401-
buildOptions.plugins?.push(
402-
createVirtualModulePlugin({
403-
namespace: 'angular:locale/placeholder',
404-
entryPointOnly: false,
405-
loadContent: () => ({
406-
contents: `(globalThis.$localize ??= {}).locale = "___NG_LOCALE_INSERT___";\n`,
407-
loader: 'js',
408-
resolveDir: workspaceRoot,
409-
}),
410-
}),
411-
);
412-
413399
// Add locale data for all active locales
414400
// TODO: Inject each individually within the inlining process itself
415401
for (const locale of i18nOptions.inlineLocales) {
@@ -474,8 +460,12 @@ function getEsBuildCommonPolyfillsOptions(
474460
.map((file) => `import '${file.replace(/\\/g, '/')}';`)
475461
.join('\n');
476462

477-
// If not inlining translations and source locale is defined, inject the locale specifier
478-
if (!i18nOptions.shouldInline && i18nOptions.hasDefinedSourceLocale) {
463+
// The below should be done after loading `$localize` as otherwise the locale will be overridden.
464+
if (i18nOptions.shouldInline) {
465+
// When inlining, a placeholder is used to allow the post-processing step to inject the $localize locale identifier.
466+
contents += '(globalThis.$localize ??= {}).locale = "___NG_LOCALE_INSERT___";\n';
467+
} else if (i18nOptions.hasDefinedSourceLocale) {
468+
// If not inlining translations and source locale is defined, inject the locale specifier.
479469
contents += `(globalThis.$localize ??= {}).locale = "${i18nOptions.sourceLocale}";\n`;
480470
}
481471

0 commit comments

Comments
 (0)