2019-08-10 07:51:30 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2020-04-13 19:40:21 -04:00
|
|
|
import {$localize, _global, LocalizeFn} from '../src/localize';
|
2019-08-10 07:51:30 -04:00
|
|
|
|
|
|
|
export {LocalizeFn, TranslateFn} from '../src/localize';
|
|
|
|
|
|
|
|
// Attach $localize to the global context, as a side-effect of this module.
|
|
|
|
_global.$localize = $localize;
|
|
|
|
|
|
|
|
// `declare global` allows us to escape the current module and place types on the global namespace
|
|
|
|
declare global {
|
|
|
|
/**
|
|
|
|
* Tag a template literal string for localization.
|
|
|
|
*
|
|
|
|
* For example:
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* $localize `some string to localize`
|
|
|
|
* ```
|
|
|
|
*
|
2019-09-13 07:46:05 -04:00
|
|
|
* **Providing meaning, description and id**
|
|
|
|
*
|
|
|
|
* You can optionally specify one or more of `meaning`, `description` and `id` for a localized
|
|
|
|
* string by pre-pending it with a colon delimited block of the form:
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* $localize`:meaning|description@@id:source message text`;
|
|
|
|
*
|
|
|
|
* $localize`:meaning|:source message text`;
|
|
|
|
* $localize`:description:source message text`;
|
|
|
|
* $localize`:@@id:source message text`;
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* This format is the same as that used for `i18n` markers in Angular templates. See the
|
|
|
|
* [Angular 18n guide](guide/i18n#template-translations).
|
|
|
|
*
|
2019-08-10 07:51:30 -04:00
|
|
|
* **Naming placeholders**
|
|
|
|
*
|
2019-09-05 09:11:31 -04:00
|
|
|
* If the template literal string contains expressions, then the expressions will be automatically
|
|
|
|
* associated with placeholder names for you.
|
2019-08-10 07:51:30 -04:00
|
|
|
*
|
2019-09-05 09:11:31 -04:00
|
|
|
* For example:
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* $localize `Hi ${name}! There are ${items.length} items.`;
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* will generate a message-source of `Hi {$PH}! There are {$PH_1} items`.
|
|
|
|
*
|
|
|
|
* The recommended practice is to name the placeholder associated with each expression though.
|
|
|
|
*
|
|
|
|
* Do this by providing the placeholder name wrapped in `:` characters directly after the
|
|
|
|
* expression. These placeholder names are stripped out of the rendered localized string.
|
|
|
|
*
|
|
|
|
* For example, to name the `items.length` expression placeholder `itemCount` you write:
|
2019-08-10 07:51:30 -04:00
|
|
|
*
|
|
|
|
* ```ts
|
2019-09-05 09:11:31 -04:00
|
|
|
* $localize `There are ${items.length}:itemCount: items`;
|
2019-08-10 07:51:30 -04:00
|
|
|
* ```
|
|
|
|
*
|
2019-09-13 07:46:05 -04:00
|
|
|
* **Escaping colon markers**
|
|
|
|
*
|
|
|
|
* If you need to use a `:` character directly at the start of a tagged string that has no
|
|
|
|
* metadata block, or directly after a substitution expression that has no name you must escape
|
|
|
|
* the `:` by preceding it with a backslash:
|
2019-08-10 07:51:30 -04:00
|
|
|
*
|
|
|
|
* For example:
|
|
|
|
*
|
|
|
|
* ```ts
|
2019-09-13 07:46:05 -04:00
|
|
|
* // message has a metadata block so no need to escape colon
|
|
|
|
* $localize `:some description::this message starts with a colon (:)`;
|
|
|
|
* // no metadata block so the colon must be escaped
|
|
|
|
* $localize `\:this message starts with a colon (:)`;
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* // named substitution so no need to escape colon
|
2019-08-10 07:51:30 -04:00
|
|
|
* $localize `${label}:label:: ${}`
|
2019-09-13 07:46:05 -04:00
|
|
|
* // anonymous substitution so colon must be escaped
|
2019-08-10 07:51:30 -04:00
|
|
|
* $localize `${label}\: ${}`
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* **Processing localized strings:**
|
|
|
|
*
|
|
|
|
* There are three scenarios:
|
|
|
|
*
|
|
|
|
* * **compile-time inlining**: the `$localize` tag is transformed at compile time by a
|
2019-09-13 07:46:05 -04:00
|
|
|
* transpiler, removing the tag and replacing the template literal string with a translated
|
|
|
|
* literal string from a collection of translations provided to the transpilation tool.
|
2019-08-10 07:51:30 -04:00
|
|
|
*
|
|
|
|
* * **run-time evaluation**: the `$localize` tag is a run-time function that replaces and
|
2019-09-13 07:46:05 -04:00
|
|
|
* reorders the parts (static strings and expressions) of the template literal string with strings
|
|
|
|
* from a collection of translations loaded at run-time.
|
2019-08-10 07:51:30 -04:00
|
|
|
*
|
|
|
|
* * **pass-through evaluation**: the `$localize` tag is a run-time function that simply evaluates
|
|
|
|
* the original template literal string without applying any translations to the parts. This
|
2019-09-13 07:46:05 -04:00
|
|
|
* version is used during development or where there is no need to translate the localized
|
|
|
|
* template literals.
|
2019-08-10 07:51:30 -04:00
|
|
|
*
|
|
|
|
* @param messageParts a collection of the static parts of the template string.
|
|
|
|
* @param expressions a collection of the values of each placeholder in the template string.
|
|
|
|
* @returns the translated string, with the `messageParts` and `expressions` interleaved together.
|
|
|
|
*/
|
|
|
|
const $localize: LocalizeFn;
|
|
|
|
}
|