@@ -33,7 +33,7 @@ Wrap your template with the `msg` function to make it localizable:
33
33
``` typescript
34
34
import {html } from ' lit-html' ;
35
35
import {msg } from ' @lit/localize' ;
36
- render (msg (' greeting ' , html ` Hello <b>World</b>! ` ), document .body );
36
+ render (msg (html ` Hello <b>World</b>! ` , {id: ' greeting ' } ), document .body );
37
37
```
38
38
39
39
Run ` lit-localize ` to extract all localizable templates and generate an XLIFF
@@ -130,7 +130,7 @@ lit-localize supports two output modes: _transform_ and _runtime_.
130
130
import {msg } from ' @lit/localize' ;
131
131
132
132
render (
133
- html ` <p>${msg (' greeting ' , html ` Hello <b>World</b>! ` )}</p> ` ,
133
+ html ` <p>${msg (html ` Hello <b>World</b>! ` , {id: ' greeting ' } )}</p> ` ,
134
134
document .body
135
135
);
136
136
```
@@ -216,8 +216,7 @@ The `@lit/localize` module exports the following functions:
216
216
> signatures to identify calls to ` msg ` and other APIs during analysis of your
217
217
> code. Casting a lit-localize function to a type that does not include its
218
218
> annotation will prevent lit-localize from being able to extract and transform
219
- > templates from your application. For example, a cast like
220
- > ` (msg as any)("greeting", "Hello") ` will not be identified. It is safe to
219
+ > templates from your application. For example, a cast like ` (msg as any)("Hello", {id: "greeting"}) ` will not be identified. It is safe to
221
220
> re-assign lit-localize functions or pass them as parameters, as long as the
222
221
> distinctive type signature is preserved. If needed, you can reference each
223
222
> function's distinctive type with e.g. ` typeof msg ` .
@@ -324,46 +323,49 @@ promise resolves.
324
323
Throws if the given locale is not contained by the configured ` sourceLocale ` or
325
324
` targetLocales ` .
326
325
327
- ### ` msg(id : string, template, ... args) => string|TemplateResult `
326
+ ### ` msg(template : string|TemplateResult|Function, options: {id: string, args?: any[]} ) => string|TemplateResult `
328
327
329
328
Make a string or lit-html template localizable.
330
329
331
- The ` id ` parameter is a project-wide unique identifier for this template.
330
+ The ` options. id` parameter is a project-wide unique identifier for this template.
332
331
333
332
The ` template ` parameter can have any of these types:
334
333
335
334
- A plain string with no placeholders:
336
335
337
336
``` typescript
338
- msg (' greeting ' , ' Hello World!' );
337
+ msg (' Hello World!' , {id: ' greeting ' } );
339
338
```
340
339
341
340
- A lit-html
342
341
[ ` TemplateResult ` ] ( https://lit-html.polymer-project.org/api/classes/_lit_html_.templateresult.html )
343
342
that may contain embedded HTML:
344
343
345
344
``` typescript
346
- msg (' greeting ' , html ` Hello <b>World</b>! ` );
345
+ msg (html ` Hello <b>World</b>! ` , {id: ' greeting ' } );
347
346
```
348
347
349
348
- A function that returns a [ template
350
349
literal] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals )
351
350
string that may contain placeholders. Placeholders may only reference
352
- parameters of the function, which will be called with the 3rd and onwards
353
- parameters to ` msg ` .
351
+ parameters of the function. The function will be invoked with the
352
+ ` options.args ` array as its parameters:
354
353
355
354
``` typescript
356
- msg (' greeting ' , (name ) => ` Hello ${name }! ` , getUsername ());
355
+ msg ((name ) => ` Hello ${name }! ` , {id: ' greeting ' , args: [ getUsername ()]} );
357
356
```
358
357
359
358
- A function that returns a lit-html
360
359
[ ` TemplateResult ` ] ( https://lit-html.polymer-project.org/api/classes/_lit_html_.templateresult.html )
361
360
that may contain embedded HTML, and may contain placeholders. Placeholders may
362
- only reference parameters of the function, which will be called with the 3rd
363
- and onwards parameters to ` msg ` :
361
+ only reference parameters of the function. The function will be invoked with
362
+ the ` options.args ` array as its parameters :
364
363
365
364
``` typescript
366
- msg (' greeting' , (name ) => html ` Hello <b>${name }</b>! ` , getUsername ());
365
+ msg ((name ) => html ` Hello <b>${name }</b>! ` , {
366
+ id: ' greeting' ,
367
+ args: [getUsername ()],
368
+ });
367
369
```
368
370
369
371
In transform mode, calls to this function are replaced with the static localized
@@ -465,7 +467,7 @@ class MyElement extends Localized(LitElement) {
465
467
render() {
466
468
// Whenever setLocale() is called, and templates for that locale have
467
469
// finished loading, this render() function will be re-invoked.
468
- return html ` <p>${msg (' greeting ' , html ` Hello <b>World!</b> ` )}</p> ` ;
470
+ return html ` <p>${msg (html ` Hello <b>World!</b> ` , {id: ' greeting ' } )}</p> ` ;
469
471
}
470
472
}
471
473
```
0 commit comments