|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | // This file contains the public API of the `@angular/localize` entry-point |
| 10 | +import {LocalizeFn} from './src/localize'; |
10 | 11 |
|
11 | 12 | export {clearTranslations, loadTranslations} from './src/translate'; |
12 | 13 | export {MessageId, TargetMessage} from './src/utils'; |
13 | 14 |
|
14 | 15 | // Exports that are not part of the public API |
15 | 16 | export * from './private'; |
| 17 | + |
| 18 | +// `declare global` allows us to escape the current module and place types on the global namespace |
| 19 | +declare global { |
| 20 | + /** |
| 21 | + * Tag a template literal string for localization. |
| 22 | + * |
| 23 | + * For example: |
| 24 | + * |
| 25 | + * ```ts |
| 26 | + * $localize `some string to localize` |
| 27 | + * ``` |
| 28 | + * |
| 29 | + * **Providing meaning, description and id** |
| 30 | + * |
| 31 | + * You can optionally specify one or more of `meaning`, `description` and `id` for a localized |
| 32 | + * string by pre-pending it with a colon delimited block of the form: |
| 33 | + * |
| 34 | + * ```ts |
| 35 | + * $localize`:meaning|description@@id:source message text`; |
| 36 | + * |
| 37 | + * $localize`:meaning|:source message text`; |
| 38 | + * $localize`:description:source message text`; |
| 39 | + * $localize`:@@id:source message text`; |
| 40 | + * ``` |
| 41 | + * |
| 42 | + * This format is the same as that used for `i18n` markers in Angular templates. See the |
| 43 | + * [Angular i18n guide](guide/i18n-common-prepare#mark-text-in-component-template). |
| 44 | + * |
| 45 | + * **Naming placeholders** |
| 46 | + * |
| 47 | + * If the template literal string contains expressions, then the expressions will be automatically |
| 48 | + * associated with placeholder names for you. |
| 49 | + * |
| 50 | + * For example: |
| 51 | + * |
| 52 | + * ```ts |
| 53 | + * $localize `Hi ${name}! There are ${items.length} items.`; |
| 54 | + * ``` |
| 55 | + * |
| 56 | + * will generate a message-source of `Hi {$PH}! There are {$PH_1} items`. |
| 57 | + * |
| 58 | + * The recommended practice is to name the placeholder associated with each expression though. |
| 59 | + * |
| 60 | + * Do this by providing the placeholder name wrapped in `:` characters directly after the |
| 61 | + * expression. These placeholder names are stripped out of the rendered localized string. |
| 62 | + * |
| 63 | + * For example, to name the `items.length` expression placeholder `itemCount` you write: |
| 64 | + * |
| 65 | + * ```ts |
| 66 | + * $localize `There are ${items.length}:itemCount: items`; |
| 67 | + * ``` |
| 68 | + * |
| 69 | + * **Escaping colon markers** |
| 70 | + * |
| 71 | + * If you need to use a `:` character directly at the start of a tagged string that has no |
| 72 | + * metadata block, or directly after a substitution expression that has no name you must escape |
| 73 | + * the `:` by preceding it with a backslash: |
| 74 | + * |
| 75 | + * For example: |
| 76 | + * |
| 77 | + * ```ts |
| 78 | + * // message has a metadata block so no need to escape colon |
| 79 | + * $localize `:some description::this message starts with a colon (:)`; |
| 80 | + * // no metadata block so the colon must be escaped |
| 81 | + * $localize `\:this message starts with a colon (:)`; |
| 82 | + * ``` |
| 83 | + * |
| 84 | + * ```ts |
| 85 | + * // named substitution so no need to escape colon |
| 86 | + * $localize `${label}:label:: ${}` |
| 87 | + * // anonymous substitution so colon must be escaped |
| 88 | + * $localize `${label}\: ${}` |
| 89 | + * ``` |
| 90 | + * |
| 91 | + * **Processing localized strings:** |
| 92 | + * |
| 93 | + * There are three scenarios: |
| 94 | + * |
| 95 | + * * **compile-time inlining**: the `$localize` tag is transformed at compile time by a |
| 96 | + * transpiler, removing the tag and replacing the template literal string with a translated |
| 97 | + * literal string from a collection of translations provided to the transpilation tool. |
| 98 | + * |
| 99 | + * * **run-time evaluation**: the `$localize` tag is a run-time function that replaces and |
| 100 | + * reorders the parts (static strings and expressions) of the template literal string with strings |
| 101 | + * from a collection of translations loaded at run-time. |
| 102 | + * |
| 103 | + * * **pass-through evaluation**: the `$localize` tag is a run-time function that simply evaluates |
| 104 | + * the original template literal string without applying any translations to the parts. This |
| 105 | + * version is used during development or where there is no need to translate the localized |
| 106 | + * template literals. |
| 107 | + * |
| 108 | + * @param messageParts a collection of the static parts of the template string. |
| 109 | + * @param expressions a collection of the values of each placeholder in the template string. |
| 110 | + * @returns the translated string, with the `messageParts` and `expressions` interleaved together. |
| 111 | + */ |
| 112 | + const $localize: LocalizeFn; |
| 113 | +} |
0 commit comments