Skip to content

Commit 876ca1e

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
1 parent 4da732d commit 876ca1e

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
@@ -398,20 +398,6 @@ function getEsBuildCommonPolyfillsOptions(
398398
// Locale data should go first so that project provided polyfill code can augment if needed.
399399
let needLocaleDataPlugin = false;
400400
if (i18nOptions.shouldInline) {
401-
// When inlining, a placeholder is used to allow the post-processing step to inject the $localize locale identifier
402-
polyfills.unshift('angular:locale/placeholder');
403-
buildOptions.plugins?.push(
404-
createVirtualModulePlugin({
405-
namespace: 'angular:locale/placeholder',
406-
entryPointOnly: false,
407-
loadContent: () => ({
408-
contents: `(globalThis.$localize ??= {}).locale = "___NG_LOCALE_INSERT___";\n`,
409-
loader: 'js',
410-
resolveDir: workspaceRoot,
411-
}),
412-
}),
413-
);
414-
415401
// Add locale data for all active locales
416402
// TODO: Inject each individually within the inlining process itself
417403
for (const locale of i18nOptions.inlineLocales) {
@@ -476,8 +462,12 @@ function getEsBuildCommonPolyfillsOptions(
476462
.map((file) => `import '${file.replace(/\\/g, '/')}';`)
477463
.join('\n');
478464

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

0 commit comments

Comments
 (0)