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
@ -7,11 +7,74 @@
|
||||
*/
|
||||
|
||||
import {LOCALE_ID} from '@angular/core';
|
||||
import {ivyEnabled} from '@angular/private/testing';
|
||||
|
||||
import {getLocaleId} from '../src/render3';
|
||||
import {global} from '../src/util/global';
|
||||
import {TestBed} from '../testing';
|
||||
import {describe, expect, inject, it} from '../testing/src/testing_internal';
|
||||
|
||||
{
|
||||
describe('Application module', () => {
|
||||
it('should set the default locale to "en-US"',
|
||||
inject([LOCALE_ID], (defaultLocale: string) => { expect(defaultLocale).toEqual('en-US'); }));
|
||||
|
||||
if (ivyEnabled) {
|
||||
it('should set the ivy locale with the configured LOCALE_ID', () => {
|
||||
TestBed.configureTestingModule({providers: [{provide: LOCALE_ID, useValue: 'fr'}]});
|
||||
const before = getLocaleId();
|
||||
const locale = TestBed.inject(LOCALE_ID);
|
||||
const after = getLocaleId();
|
||||
expect(before).toEqual('en-us');
|
||||
expect(locale).toEqual('fr');
|
||||
expect(after).toEqual('fr');
|
||||
});
|
||||
|
||||
describe('$localize.locale', () => {
|
||||
beforeEach(() => initLocale('de'));
|
||||
afterEach(() => restoreLocale());
|
||||
|
||||
it('should set the ivy locale to `$localize.locale` value if it is defined', () => {
|
||||
// Injecting `LOCALE_ID` should also initialize the ivy locale
|
||||
const locale = TestBed.inject(LOCALE_ID);
|
||||
expect(locale).toEqual('de');
|
||||
expect(getLocaleId()).toEqual('de');
|
||||
});
|
||||
|
||||
it('should set the ivy locale to an application provided LOCALE_ID even if `$localize.locale` is defined',
|
||||
() => {
|
||||
TestBed.configureTestingModule({providers: [{provide: LOCALE_ID, useValue: 'fr'}]});
|
||||
const locale = TestBed.inject(LOCALE_ID);
|
||||
expect(locale).toEqual('fr');
|
||||
expect(getLocaleId()).toEqual('fr');
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let hasGlobalLocalize: boolean;
|
||||
let hasGlobalLocale: boolean;
|
||||
let originalLocale: string;
|
||||
|
||||
function initLocale(locale: string) {
|
||||
hasGlobalLocalize = Object.getOwnPropertyNames(global).includes('$localize');
|
||||
if (!hasGlobalLocalize) {
|
||||
global.$localize = {};
|
||||
}
|
||||
hasGlobalLocale = Object.getOwnPropertyNames(global.$localize).includes('locale');
|
||||
originalLocale = global.$localize.locale;
|
||||
global.$localize.locale = locale;
|
||||
}
|
||||
|
||||
function restoreLocale() {
|
||||
if (hasGlobalLocale) {
|
||||
global.$localize.locale = originalLocale;
|
||||
} else {
|
||||
delete global.$localize.locale;
|
||||
}
|
||||
|
||||
if (!hasGlobalLocalize) {
|
||||
delete global.$localize;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user