146
146
147
147
The ` lit-localize ` module exports the following functions:
148
148
149
+ > Note that lit-localize relies on distinctive, annotated TypeScript type
150
+ > signatures to identify calls to ` msg ` and other APIs during analysis of your
151
+ > code. Casting a lit-localize function to a type that does not include its
152
+ > annotation will prevent lit-localize from being able to extract and transform
153
+ > templates from your application. For example, a cast like
154
+ > ` (msg as any)("greeting", "Hello") ` will not be identified. It is safe to
155
+ > re-assign lit-localize functions or pass them as parameters, as long as the
156
+ > distinctive type signature is preserved. If needed, you can reference each
157
+ > function's distinctive type with e.g. ` typeof msg ` .
158
+
149
159
### ` configureLocalization(configuration) `
150
160
151
- Set runtime localization configuration.
161
+ Set configuration parameters for lit-localize when in runtime mode. Returns an
162
+ object with functions:
163
+
164
+ - [ ` getLocale ` ] ( #getlocale-string ) : Return the active locale code.
165
+ - [ ` setLocale ` ] ( #setlocalelocale-string-promise ) : Set the active locale code.
166
+
167
+ Throws if called more than once.
152
168
153
- In runtime mode, this function must be called once, before any calls to ` msg() ` .
169
+ When in transform mode, the lit-localize CLI will error if this function is
170
+ called. Use
171
+ [ ` configureTransformLocalization ` ] ( #configuretransformlocalizationconfiguration )
172
+ instead.
154
173
155
174
The ` configuration ` object must have the following properties:
156
175
@@ -168,37 +187,65 @@ The `configuration` object must have the following properties:
168
187
Example:
169
188
170
189
``` typescript
171
- configureLocalization ({
190
+ const {getLocale, setLocale} = configureLocalization ({
172
191
sourceLocale: ' en' ,
173
192
targetLocales: [' es-419' , ' zh_CN' ],
174
193
loadLocale : (locale ) => import (` /${locale }.js ` ),
175
194
});
176
195
```
177
196
178
- In transform mode, this function is not required, and calls to it will be
179
- replaced with ` undefined ` .
197
+ ### ` configureTransformLocalization(configuration) `
180
198
181
- ### ` getLocale() => string `
199
+ Set configuration parameters for lit-localize when in transform mode. Returns an
200
+ object with function:
182
201
183
- Return the active locale code.
202
+ - [ ` getLocale ` ] ( #getlocale-string ) : Return the active locale code.
184
203
185
- In transform mode, calls to this function are transformed into the static locale
186
- code string for each emitted locale.
204
+ (Note that [ ` setLocale ` ] ( #setlocalelocale-string-promise ) is not available from
205
+ this function, because changing locales at runtime is not supported in transform
206
+ mode.)
187
207
188
- ### ` setLocale(locale: string) `
208
+ Throws if called more than once.
189
209
190
- Set the active locale code, and begin loading templates for that locale using
191
- the ` loadLocale ` function that was passed to ` configureLocalization ` .
210
+ The ` configuration ` object must have the following properties:
192
211
193
- In transform mode, calls to this function are replaced with ` undefined ` .
212
+ - ` sourceLocale: string ` : Required locale code in which source templates in this
213
+ project are written, and the active locale.
194
214
195
- ### ` localeReady() => Promise `
215
+ Example:
196
216
197
- Return a promise that is resolved when the next set of templates are loaded and
198
- available for rendering. Applications in runtime mode should always ` await localeReady() ` before rendering.
217
+ ``` typescript
218
+ const {getLocale} = configureLocalization ({
219
+ sourceLocale: ' en' ,
220
+ });
221
+ ```
222
+
223
+ In transform mode, calls to this function are transformed to an object with a
224
+ ` getLocale ` implementation that returns the static locale code for each locale
225
+ bundle. For example:
226
+
227
+ ``` typescript
228
+ const {getLocale} = {getLocale : () => ' es-419' };
229
+ ```
199
230
200
- In transform mode, calls to this function are replaced with
201
- ` Promise.resolve(undefined) ` .
231
+ ### ` getLocale() => string `
232
+
233
+ Return the active locale code.
234
+
235
+ ### ` setLocale(locale: string) => Promise `
236
+
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.
240
+
241
+ Note that if a second call to ` setLocale ` is made while the first requested
242
+ locale is still loading, then the second call takes precedence, and the promise
243
+ returned from the first call will resolve when second locale is ready. If you
244
+ need to know whether a particular locale was loaded, check ` getLocale ` after the
245
+ promise resolves.
246
+
247
+ Throws if the given locale is not contained by the configured ` sourceLocale ` or
248
+ ` targetLocales ` .
202
249
203
250
### ` msg(id: string, template, ...args) => string|TemplateResult `
204
251
@@ -249,21 +296,76 @@ template for each emitted locale. For example:
249
296
html ` Hola <b>${getUsername ()}!</b> ` ;
250
297
```
251
298
252
- ### ` LOCALE_CHANGED_EVENT: string `
299
+ ### ` LOCALE_STATUS_EVENT `
300
+
301
+ Name of the [ ` lit-localize-status ` event] ( #lit-localize-status-event ) .
253
302
254
- Whenever the locale changes and templates have finished loading, an event by
255
- this name (` "lit-localize-locale-changed" ` ) is dispatched to ` window ` .
303
+ ## ` lit-localize-status ` event
304
+
305
+ In runtime mode, whenever a locale change starts, finishes successfully, or
306
+ fails, lit-localize will dispatch a ` lit-localize-status ` event to ` window ` .
256
307
257
308
You can listen for this event to know when your application should be
258
309
re-rendered following a locale change. See also the
259
310
[ ` Localized ` ] ( #localized-mixin ) mixin, which automatically re-renders
260
311
` LitElement ` classes using this event.
261
312
262
- ``` typescript
263
- import {LOCALE_CHANGED_EVENT } from ' lit-localize' ;
313
+ ### Event types
314
+
315
+ The ` detail.status ` string property tells you what kind of status change has occured,
316
+ and can be one of: ` loading ` , ` ready ` , or ` error ` :
317
+
318
+ #### ` loading `
319
+
320
+ A new locale has started to load. The ` detail ` object also contains:
321
+
322
+ - ` loadingLocale: string ` : Code of the locale that has started loading.
323
+
324
+ A ` loading ` status can be followed by a ` ready ` , ` error ` , or ` loading ` status.
325
+
326
+ In the case that a second locale is requested before the first one finishes
327
+ loading, a new ` loading ` event is dispatched, and no ` ready ` or ` error ` event
328
+ will be dispatched for the first request, because it is now stale.
264
329
265
- window .addEventListener (LOCALE_CHANGED_EVENT , () => {
266
- renderApplication ();
330
+ #### ` ready `
331
+
332
+ A new locale has successfully loaded and is ready for rendering. The ` detail ` object also contains:
333
+
334
+ - ` readyLocale: string ` : Code of the locale that has successfully loaded.
335
+
336
+ A ` ready ` status can be followed only by a ` loading ` status.
337
+
338
+ #### ` error `
339
+
340
+ A new locale failed to load. The ` detail ` object also contains the following
341
+ properties:
342
+
343
+ - ` errorLocale: string ` : Code of the locale that failed to load.
344
+ - ` errorMessage: string ` : Error message from locale load failure.
345
+
346
+ An ` error ` status can be followed only by a ` loading ` status.
347
+
348
+ ### Event example
349
+
350
+ ``` typescript
351
+ // Show/hide a progress indicator whenever a new locale is loading,
352
+ // and re-render the application every time a new locale successfully loads.
353
+ window .addEventListener (' lit-localize-status' , (event ) => {
354
+ const spinner = document .querySelector (' #spinner' );
355
+ if (event .detail .status === ' loading' ) {
356
+ console .log (` Loading new locale: ${event .detail .loadingLocale } ` );
357
+ spinner .removeAttribute (' hidden' );
358
+ } else if (event .detail .status === ' ready' ) {
359
+ console .log (` Loaded new locale: ${event .detail .readyLocale } ` );
360
+ spinner .addAttribute (' hidden' );
361
+ renderApplication ();
362
+ } else if (event .detail .status === ' error' ) {
363
+ console .error (
364
+ ` Error loading locale ${event .detail .errorLocale }: ` +
365
+ event .detail .errorMessage
366
+ );
367
+ spinner .addAttribute (' hidden' );
368
+ }
267
369
});
268
370
```
269
371
0 commit comments