angular-docs-cn/packages/localize/test/translate_spec.ts

86 lines
2.9 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright Google Inc. 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.io/license
*/
feat(ivy): i18n - reorganize entry-points for better reuse (#32488) This is a refactoring that moves the source code around to provide a better platform for adding the compile-time inlining. 1. Move the global side-effect import from the primary entry-point to a secondary entry-point @angular/localize/init. This has two benefits: first it allows the top level entry-point to contain tree-shakable shareable code; second it gives the side-effect import more of an "action" oriented name, which indicates that importing it does something tangible 2. Move all the source code into the top src folder, and import the localize related functions into the localize/init/index.ts entry-point. This allows the different parts of the package to share code without a proliferation of secondary entry-points (i.e. localize/utils). 3. Avoid publicly exporting any utilities at this time - the only public API at this point are the global `$localize` function and the two runtime helpers `loadTranslations()` and `clearTranslations()`. This does not mean that we will not expose additional helpers for 3rd party tooling in the future, but it avoid us preemptively exposing something that we might want to change in the near future. Notes: It is not possible to have the `$localize` code in the same Bazel package as the rest of the code. If we did this, then the bundled `@angular/localize/init` entry-point code contains all of the helper code, even though most of it is not used. Equally it is not possible to have the `$localize` types (i.e. `LocalizeFn` and `TranslateFn`) defined in the `@angular/localize/init` entry-point because these types are needed for the runtime code, which is inside the primary entry-point. Importing them from `@angular/localize/init` would run the side-effect. The solution is to have a Bazel sub-package at `//packages/localize/src/localize` which contains these types and the `$localize` function implementation. The primary `//packages/localize` entry-point imports the types without any side-effect. The secondary `//packages/localize/init` entry-point imports the `$localize` function and attaches it to the global scope as a side-effect, without bringing with it all the other utility functions. BREAKING CHANGES: The entry-points have changed: * To attach the `$localize` function to the global scope import from `@angular/localize/init`. Previously it was `@angular/localize`. * To access the `loadTranslations()` and `clearTranslations()` functions, import from `@angular/localize`. Previously it was `@angular/localize/run_time`. PR Close #32488
2019-08-10 07:51:30 -04:00
import '@angular/localize/init';
import {clearTranslations, loadTranslations} from '../src/translate';
describe('$localize tag with translations', () => {
describe('identities', () => {
beforeEach(() => {
loadTranslations({
'abc': 'abc',
'abc{$ph_1}': 'abc{$ph_1}',
'abc{$ph_1}def': 'abc{$ph_1}def',
'abc{$ph_1}def{$ph_2}': 'abc{$ph_1}def{$ph_2}',
'Hello, {$ph_1}!': 'Hello, {$ph_1}!',
});
});
afterEach(() => { clearTranslations(); });
it('should render template literals as-is', () => {
expect($localize `abc`).toEqual('abc');
expect($localize `abc${1 + 2 + 3}`).toEqual('abc6');
expect($localize `abc${1 + 2 + 3}def`).toEqual('abc6def');
expect($localize `abc${1 + 2 + 3}def${4 + 5 + 6}`).toEqual('abc6def15');
const getName = () => 'World';
expect($localize `Hello, ${getName()}!`).toEqual('Hello, World!');
});
});
describe('to upper-case messageParts', () => {
beforeEach(() => {
loadTranslations({
'abc': 'ABC',
'abc{$ph_1}': 'ABC{$ph_1}',
'abc{$ph_1}def': 'ABC{$ph_1}DEF',
'abc{$ph_1}def{$ph_2}': 'ABC{$ph_1}DEF{$ph_2}',
'Hello, {$ph_1}!': 'HELLO, {$ph_1}!',
});
});
afterEach(() => { clearTranslations(); });
it('should render template literals with messages upper-cased', () => {
expect($localize `abc`).toEqual('ABC');
expect($localize `abc${1 + 2 + 3}`).toEqual('ABC6');
expect($localize `abc${1 + 2 + 3}def`).toEqual('ABC6DEF');
expect($localize `abc${1 + 2 + 3}def${4 + 5 + 6}`).toEqual('ABC6DEF15');
const getName = () => 'World';
expect($localize `Hello, ${getName()}!`).toEqual('HELLO, World!');
});
});
describe('to reverse expressions', () => {
beforeEach(() => {
loadTranslations({
'abc{$ph_1}def{$ph_2} - Hello, {$ph_3}!': 'abc{$ph_3}def{$ph_2} - Hello, {$ph_1}!',
});
});
afterEach(() => { clearTranslations(); });
it('should render template literals with expressions reversed', () => {
const getName = () => 'World';
expect($localize `abc${1 + 2 + 3}def${4 + 5 + 6} - Hello, ${getName()}!`)
.toEqual('abcWorlddef15 - Hello, 6!');
});
});
describe('to remove expressions', () => {
beforeEach(() => {
loadTranslations({
'abc{$ph_1}def{$ph_2} - Hello, {$ph_3}!': 'abc{$ph_1} - Hello, {$ph_3}!',
});
});
afterEach(() => { clearTranslations(); });
it('should render template literals with expressions removed', () => {
const getName = () => 'World';
expect($localize `abc${1 + 2 + 3}def${4 + 5 + 6} - Hello, ${getName()}!`)
.toEqual('abc6 - Hello, World!');
});
});
});