# Terminology * **CTC format**: The [Chrome extension & Chrome app i18n format](https://developer.chrome.com/extensions/i18n-messages) with some minor changes. JSON with their specified model for declaring placeholders, examples, etc. Used as an interchange data format. * **LHL syntax** (Lighthouse Localizable syntax): The ICU-friendly string syntax that is used to author `UIStrings` and is seen in the locale files in `shared/localization/locales/*.json`. Lighthouse has a custom syntax these strings combines many ICU message features along with some markdown. * **ICU**: ICU (International Components for Unicode) is a localization project and standard defined by the Unicode consortium. In general, we refer to "ICU" as the [ICU message formatting](http://userguide.icu-project.org/formatparse/messages) syntax. # The Lighthouse i18n pipeline The translation pipeline has 3 distinct stages, the Collection done at build time, the Translation done in the Google TC pipeline, and the Replacement done at runtime. The collection and translation pipeline: ``` Source files: Locale files: +---------------------------+ +---------------------------------------------- | ++ | shared/localization/locales/en-US.json | | const UIStrings = { ... };|-+ +---> | shared/localization/locales/en-XL.json | | |-| | +----------------------------------------------+ +-----------------------------| | | || +----------------------------| | | shared/localization/locales/*.json |-<+ +---------------------------+ | | || | | | +----------------------------------------------| | $ yarn | | +---------------------------------------------+ | i18n:collect-strings +--------------------+ | | | v ▐ ▐ +---------------+ | +------------+------+ ▐ Google TC Pipeline ▐ +->| *.ctc.json |---+ | en-US.ctc.json | +--------------> ▐ (~2 weeks) ▐ +---------------+ +-------------------+ $ g3/import….sh ▐ ▐ $ g3/export….sh ``` #### String Collection workflow (build time) To a typical developer, the pipeline looks like this: * LH contributor makes any changes to strings. ```shell # collect UIStrings and bake the en-US & en-XL locales $ yarn i18n:collect-strings # Test to see that the new translations are valid and apply to all strings $ yarn build-sample-reports && open dist/xl-accented/index.html ``` Note: Why do `en-US` and `en-XL` get baked early? We write all our strings in `en-US` by default, so they do not need to be translated, so it can be immediately baked without going to the translators. Similarly, `en-XL` is a debugging language, it is an automated version of `en-US` that simply adds markers to `en` strings in order to make it obvious that something has or hasn't been translated. So neither of these files need to go to translators to be used, and both can be used at develop-time to help developer i18n workflow. #### String Translation in Google Translation Console * Googler is ready to kick off the TC pipeline again. ```shell # collect UIStrings (to make sure everything is up to date) $ yarn i18n:collect-strings # Extract the CTC format files to translation console $ sh import-source-from-github.sh # Submit CL. Wait ~2 weeks for translations # Import the translated CTC format files to locales/ and bake them $ sh export-tc-dump-to-github.sh ``` #### String Replacement (runtime) See [Appendix A: How runtime string replacement works](#appendix) # Writing UIStrings with LHL We want to keep strings close to the code in which they are used so that developers can easily understand their context. We use `i18n.js` to extract the `UIStrings` strings from individual js files. LHL strings in each module are defined in a `UIStrings` object with the strings as its properties. JSDoc is used to provide additional information about each string. The LHL syntax is based primarily around the standardized [ICU message formatting](http://userguide.icu-project.org/formatparse/messages) syntax. ### Basic example A simple string. ```javascript const UIStrings = { /** Imperative title of a Lighthouse audit that ... */ title: 'Minify CSS', }; ``` For proper translation, **all** strings must be accompanied by a description, written as a preceeding comment. ### Replacements and primitive formatting Replacements (aka substitutions) include string replacements like `{some_name}` and number formatting like `{timeInMs, number, milliseconds}`. #### Direct ICU replacement `{some_name}` is called _Direct ICU_ since the replacement is a direct substitution of ICU with a variable and uses no custom formatting. This is simply a direct replacement of text into a string. Often used for proper nouns, code, or other text that is dynamic and added at runtime. ICU replacements must use a JSDoc-like syntax to specify an example for direct ICU replacements: * To specify the description, use `@description …`: * `@description Label string used to…` * To specify an example for an ICU replacement, use `@example {…} …`: * `@example {This is an example ICU replacement} variableName` ```javascript const UIStrings = { /** * @description Error message explaining ... * @example {NO_SPEEDLINE_FRAMES} errorCode */ didntCollectScreenshots: `Chrome didn't .... ({errorCode})`, }; ``` #### Complex ICU replacement `{timeInMs, number, milliseconds}` is called _Complex ICU_ since the replacement is for numbers and other complex replacements that use the custom formatters in Lighthouse. The supported complex ICU formats are: `milliseconds`, `seconds`, `bytes`, `percent`, and `extendedPercent`. These complex ICU formats are automatically given @example values during `yarn i18n:collect-strings`. Therefore, a normal description string can be used: ```javascript const UIStrings = { /** Description of display value. */ displayValueText: 'Interactive at {timeInMs, number, seconds} s', }; ``` ### Ordinals (Numeric Selects), Plurals An ordinal ICU message is used when the message contains "plurals", wherein a sub-message would need to be selected from a list of messages depending on the value of `itemCount` (in this example). They are a flavor of "Selects" that have a unique syntax. ```javascript displayValue: `{itemCount, plural, =1 {1 link found} other {# links found} }`, ``` Note: Why are direct ICU and complex ICU placeholdered out, but Ordinals are not? Direct and complex ICU should not contain elements that need to be translated (Direct ICU replaces universal proper nouns, and Complex ICU replaces number formatting), while ordinals do need to be translated. Ordinals and selects are therefore handled specially, and do not need to be placeholdered out. ### Selects A select ICU message is used when the message should select a sub-message based on the value of a variable `pronoun` in this case. This is often used for gender based selections, but can be used for any enum. Lighthouse does not use selects very often. ```javascript displayValue: `{pronoun, select, male {He programmed the link.} female {She programmed the link.} other {They programmed the link.} }`, ``` ### Markdown Some strings, like audit descriptions, can also contain a subset of markdown. See [`audit.d.ts`](https://github.com/GoogleChrome/lighthouse/blob/5e52dcca72b35943d14cc7c27613517c425250b9/types/audit.d.ts) for which properties support markdown rendering and will be rendered in the report. **Inline code blocks** To format some text as code it should be contained in `backticks`. Any text within the backticks will not be translated. This should be used whenever code is non-translatable. Such as HTML tags or snippets of code. Also note that there is no escape character for using backticks as part of the string, so ONLY use backticks to define code blocks. ```javascript const UIStrings = { title: 'Document has a `