You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| Output | A full build of your application for each locale, with all `msg` calls replaced with static localized templates. | A dynamically loadable template module for each target locale. |
| Switch locales | Refresh page and load a different `.js` file | Call `setLocale()` and re-render using any of:<br><br>- `lit-localize-status` event<br>- `setLocale` promise<br>- `Localized` mixin for `LitElement`|
77
+
| Advantages | - Fastest rendering<br>- Fewer bytes for a single locale | - Faster locale switching<br>- Fewer _marginal_ bytes when switching locales |
78
+
21
79
## Tutorial
22
80
23
81
1. Install lit-localize. You get both a client library and a command-line tool.
@@ -234,9 +292,10 @@ Return the active locale code.
234
292
235
293
### `setLocale(locale: string) => Promise`
236
294
237
-
Set the active locale code, and begin loading templates for that locale using
238
-
the `loadLocale` function that was passed to `configureLocalization`. Returns a
239
-
promise that resolves when the next locale is ready to be rendered.
295
+
Available only in runtime mode. Set the active locale code, and begin loading
296
+
templates for that locale using the `loadLocale` function that was passed to
297
+
`configureLocalization`. Returns a promise that resolves when the next locale is
298
+
ready to be rendered.
240
299
241
300
Note that if a second call to `setLocale` is made while the first requested
242
301
locale is still loading, then the second call takes precedence, and the promise
|`sourceLocale`|`string`| Required locale code that templates in the source code are written in. |
490
+
|`targetLocales`|`string[]`| Required locale codes that templates will be localized to. |
491
+
|`tsConfig`|`string`| Path to a `tsconfig.json` file that describes the TypeScript source files from which messages will be extracted. |
492
+
|`output.mode`|`"transform"`, `"runtime"`| What kind of output should be produced. See [modes](#modes). |
493
+
|`interchange.format`|`"xliff"`, `"xlb"`| Data format to be consumed by your localization process. Options:<br><br>- `"xliff"`: [XLIFF 1.2](http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html) XML format<br>- `"xlb"`: Google-internal XML format |
494
+
| <h4colspan="3">Transform mode only</h4> |
495
+
|`output.outputDir`|`string`| Output directory for generated TypeScript modules. Into this directory will be generated a `<locale>.ts` for each `targetLocale`, each a TypeScript module that exports the translations in that locale keyed by message ID. |
496
+
| <h4colspan="3">XLIFF only</h4> ||
497
+
|`interchange.xliffDir`|`string`| Directory on disk to read/write `.xlf` XML files. For each target locale, the file path `"<xliffDir>/<locale>.xlf"` will be used. |
498
+
499
+
## FAQ
500
+
501
+
-[How should I set the initial locale in transform mode?](#how-should-i-set-the-initial-locale-in-transform-mode)
502
+
-[How should I switch locales in transform mode?](#how-should-i-switch-locales-in-transform-mode)
503
+
504
+
### How should I set the initial locale in transform mode?
505
+
506
+
In transform mode, the locale is determined simply by the JavaScript bundle you
507
+
load. How you determine which bundle to load when your page loads is up to you.
508
+
509
+
> IMPORTANT: Take care to always validate your locale codes when dynamically
510
+
> choosing a script name! The example below is safe because a script can only be
511
+
> loaded if it matches one of the fixed locale codes in the regular expression,
512
+
> but if our matching logic was less precise, it could result in bugs or attacks
513
+
> that inject insecure JavaScript.
514
+
515
+
For example, if your application's locale is reflected in the URL, you can
516
+
include an inline script in your HTML file that checks the URL and inserts the
517
+
appropriate `<script>` tag:
518
+
519
+
```html
520
+
<!DOCTYPE html>
521
+
<script>
522
+
// If the subdomain matches one of our locale codes, load that bundle.
523
+
// Otherwise, load the default locale bundle.
524
+
//
525
+
// E.g. https://es-419.example.com/
526
+
// ^^^^^^
527
+
constmatch=window.location.href.match(
528
+
/^https?:\/\/(es-419|zh_CN|en|es)\./
529
+
);
530
+
constlocale= match ? match[1] :'en';
531
+
constscript=document.createElement('script');
532
+
script.type='module';
533
+
script.src=`/${locale}.js`;
534
+
document.head.appendChild(script);
535
+
</script>
536
+
```
537
+
538
+
Implementing logic similar to this on your _server_ so that the appropriate
539
+
script tag is statically rendered into your HTML file will usually result in the
540
+
best performance, because the browser will start downloading your script as
541
+
early as possible.
542
+
543
+
### How should I switch locales in transform mode?
544
+
545
+
In transform mode, the `setLocale` function is not available. Instead, reload
546
+
the page so that the next load will pick a different locale bundle.
547
+
548
+
For example, this `locale-picker` custom element loads a new subdomain whenever
0 commit comments