feat(ivy): allow the locale to be set via a global property (#33314)
In the post-$localize world the current locale value is defined by setting `$localize.locale` which is then read at runtime by Angular in the provider for the `LOCALE_ID` token and also passed to the ivy machinery via`setLocaleId()`. The $localize compile-time inlining tooling can replace occurrences of `$localize.locale` with a string literal, similar to how translations are inlined. // FW-1639 See https://github.com/angular/angular-cli/issues/15896 PR Close #33314
This commit is contained in:

committed by
Andrew Kushnir

parent
ed4244e932
commit
fde8363e0d
@ -24,6 +24,8 @@ import {SCHEDULER} from './render3/component_ref';
|
||||
import {setLocaleId} from './render3/i18n';
|
||||
import {NgZone} from './zone';
|
||||
|
||||
declare const $localize: {locale?: string};
|
||||
|
||||
export function _iterableDiffersFactory() {
|
||||
return defaultIterableDiffers;
|
||||
}
|
||||
@ -33,22 +35,38 @@ export function _keyValueDiffersFactory() {
|
||||
}
|
||||
|
||||
export function _localeFactory(locale?: string): string {
|
||||
if (locale) {
|
||||
if (ivyEnabled) {
|
||||
setLocaleId(locale);
|
||||
}
|
||||
return locale;
|
||||
locale = locale || getGlobalLocale();
|
||||
if (ivyEnabled) {
|
||||
setLocaleId(locale);
|
||||
}
|
||||
// Use `goog.LOCALE` as default value for `LOCALE_ID` token for Closure Compiler.
|
||||
// Note: default `goog.LOCALE` value is `en`, when Angular used `en-US`. In order to preserve
|
||||
// backwards compatibility, we use Angular default value over Closure Compiler's one.
|
||||
return locale;
|
||||
}
|
||||
|
||||
/**
|
||||
* Work out the locale from the potential global properties.
|
||||
*
|
||||
* * Closure Compiler: use `goog.LOCALE`.
|
||||
* * Ivy enabled: use `$localize.locale`
|
||||
*/
|
||||
export function getGlobalLocale(): string {
|
||||
if (ngI18nClosureMode && typeof goog !== 'undefined' && goog.LOCALE !== 'en') {
|
||||
if (ivyEnabled) {
|
||||
setLocaleId(goog.LOCALE);
|
||||
}
|
||||
// * The default `goog.LOCALE` value is `en`, while Angular used `en-US`.
|
||||
// * In order to preserve backwards compatibility, we use Angular default value over
|
||||
// Closure Compiler's one.
|
||||
return goog.LOCALE;
|
||||
} else {
|
||||
// KEEP `typeof $localize !== 'undefined' && $localize.locale` IN SYNC WITH THE LOCALIZE
|
||||
// COMPILE-TIME INLINER.
|
||||
//
|
||||
// * During compile time inlining of translations the expression will be replaced
|
||||
// with a string literal that is the current locale. Other forms of this expression are not
|
||||
// guaranteed to be replaced.
|
||||
//
|
||||
// * During runtime translation evaluation, the developer is required to set `$localize.locale`
|
||||
// if required, or just to provide their own `LOCALE_ID` provider.
|
||||
return (ivyEnabled && typeof $localize !== 'undefined' && $localize.locale) ||
|
||||
DEFAULT_LOCALE_ID;
|
||||
}
|
||||
return DEFAULT_LOCALE_ID;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user