Skip to content

Commit 21828c9

Browse files
authored
[localize] New msg signature with options object (lit#1442)
Changes the lit-localize msg signature, the first step in adding support for automatic localize message IDs (lit#1437). Before: msg(id: string, template: string|TemplateResult|Function, ...args: any[]) After: msg(template: string|TemplateResult|Function, options: {id: string: args?: any[]}) In the next PR, id will become optional. Also fixes the json-schema build issue.
1 parent 27d604f commit 21828c9

37 files changed

+602
-461
lines changed

packages/localize/CHANGELOG.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,23 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
<!-- ## Unreleased -->
8+
## Unreleased
9+
10+
### Changed
11+
12+
- **[BREAKING]** The signature for the `msg` function has changed:
13+
14+
Before:
15+
16+
```ts
17+
msg(id: string, template: string|TemplateResult|Function, ...args: any[])
18+
```
19+
20+
After:
21+
22+
```ts
23+
msg(template: string|TemplateResult|Function, options: {id: string: args?: any[]})
24+
```
925

1026
## [0.5.1] - 2020-11-09
1127

packages/localize/README.md

+17-15
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Wrap your template with the `msg` function to make it localizable:
3333
```typescript
3434
import {html} from 'lit-html';
3535
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);
3737
```
3838

3939
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_.
130130
import {msg} from '@lit/localize';
131131

132132
render(
133-
html`<p>${msg('greeting', html`Hello <b>World</b>!`)}</p>`,
133+
html`<p>${msg(html`Hello <b>World</b>!`, {id: 'greeting'})}</p>`,
134134
document.body
135135
);
136136
```
@@ -216,8 +216,7 @@ The `@lit/localize` module exports the following functions:
216216
> signatures to identify calls to `msg` and other APIs during analysis of your
217217
> code. Casting a lit-localize function to a type that does not include its
218218
> 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
221220
> re-assign lit-localize functions or pass them as parameters, as long as the
222221
> distinctive type signature is preserved. If needed, you can reference each
223222
> function's distinctive type with e.g. `typeof msg`.
@@ -324,46 +323,49 @@ promise resolves.
324323
Throws if the given locale is not contained by the configured `sourceLocale` or
325324
`targetLocales`.
326325

327-
### `msg(id: string, template, ...args) => string|TemplateResult`
326+
### `msg(template: string|TemplateResult|Function, options: {id: string, args?: any[]}) => string|TemplateResult`
328327

329328
Make a string or lit-html template localizable.
330329

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.
332331

333332
The `template` parameter can have any of these types:
334333

335334
- A plain string with no placeholders:
336335

337336
```typescript
338-
msg('greeting', 'Hello World!');
337+
msg('Hello World!', {id: 'greeting'});
339338
```
340339

341340
- A lit-html
342341
[`TemplateResult`](https://lit-html.polymer-project.org/api/classes/_lit_html_.templateresult.html)
343342
that may contain embedded HTML:
344343

345344
```typescript
346-
msg('greeting', html`Hello <b>World</b>!`);
345+
msg(html`Hello <b>World</b>!`, {id: 'greeting'});
347346
```
348347

349348
- A function that returns a [template
350349
literal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals)
351350
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:
354353

355354
```typescript
356-
msg('greeting', (name) => `Hello ${name}!`, getUsername());
355+
msg((name) => `Hello ${name}!`, {id: 'greeting', args: [getUsername()]});
357356
```
358357

359358
- A function that returns a lit-html
360359
[`TemplateResult`](https://lit-html.polymer-project.org/api/classes/_lit_html_.templateresult.html)
361360
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:
364363

365364
```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+
});
367369
```
368370

369371
In transform mode, calls to this function are replaced with the static localized
@@ -465,7 +467,7 @@ class MyElement extends Localized(LitElement) {
465467
render() {
466468
// Whenever setLocale() is called, and templates for that locale have
467469
// 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>`;
469471
}
470472
}
471473
```

packages/localize/examples/runtime/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"private": true,
66
"scripts": {
77
"build": "rm -rf lib/* && lit-localize && tsc",
8-
"serve": "es-dev-server --node-resolve"
8+
"serve": "es-dev-server --node-resolve --preserve-symlinks"
99
},
1010
"dependencies": {
1111
"@lit/localize": "^0.5.1",

packages/localize/examples/runtime/src/locale-codes.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,18 @@ export const sourceLocale = `en`;
88

99
/**
1010
* The other locale codes that this application is localized into. Sorted
11-
* longest first, then lexicographically.
11+
* lexicographically.
1212
*/
1313
export const targetLocales = [
1414
`es-419`,
1515
`zh_CN`,
1616
] as const;
1717

1818
/**
19-
* All valid project locale codes. Sorted longest first, then
20-
* lexicographically.
19+
* All valid project locale codes. Sorted lexicographically.
2120
*/
2221
export const allLocales = [
22+
`en`,
2323
`es-419`,
2424
`zh_CN`,
25-
`en`,
2625
] as const;

packages/localize/examples/runtime/src/locales/es-419.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
/* eslint-disable @typescript-eslint/no-explicit-any */
99

1010
export const templates = {
11-
greeting: html`Hola <b>Mundo</b>!`,
11+
'greeting': html`Hola <b>Mundo</b>!`,
1212
};
1313

packages/localize/examples/runtime/src/locales/zh_CN.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
/* eslint-disable @typescript-eslint/no-explicit-any */
99

1010
export const templates = {
11-
greeting: html`你好, <b>世界!</b>`,
11+
'greeting': html`你好, <b>世界!</b>`,
1212
};
1313

packages/localize/examples/runtime/src/x-greeter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {Localized} from '@lit/localize/localized-element.js';
44

55
export class XGreeter extends Localized(LitElement) {
66
render() {
7-
return html`<p>${msg('greeting', html`Hello <b>World</b>!`)}</p>`;
7+
return html`<p>${msg(html`Hello <b>World</b>!`, {id: 'greeting'})}</p>`;
88
}
99
}
1010
customElements.define('x-greeter', XGreeter);

packages/localize/examples/transform/package-lock.json

+18-26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/localize/examples/transform/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"private": true,
66
"scripts": {
77
"build": "rm -rf lib/* && lit-localize",
8-
"serve": "es-dev-server --node-resolve"
8+
"serve": "es-dev-server --node-resolve --preserve-symlinks"
99
},
1010
"dependencies": {
1111
"@lit/localize": "^0.5.1",

packages/localize/examples/transform/src/locale-codes.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,18 @@ export const sourceLocale = `en`;
88

99
/**
1010
* The other locale codes that this application is localized into. Sorted
11-
* longest first, then lexicographically.
11+
* lexicographically.
1212
*/
1313
export const targetLocales = [
1414
`es-419`,
1515
`zh_CN`,
1616
] as const;
1717

1818
/**
19-
* All valid project locale codes. Sorted longest first, then
20-
* lexicographically.
19+
* All valid project locale codes. Sorted lexicographically.
2120
*/
2221
export const allLocales = [
22+
`en`,
2323
`es-419`,
2424
`zh_CN`,
25-
`en`,
2625
] as const;

packages/localize/examples/transform/src/x-greeter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {msg} from '@lit/localize';
33

44
export class XGreeter extends LitElement {
55
render() {
6-
return html`<p>${msg('greeting', html`Hello <b>World</b>!`)}</p>`;
6+
return html`<p>${msg(html`Hello <b>World</b>!`, {id: 'greeting'})}</p>`;
77
}
88
}
99
customElements.define('x-greeter', XGreeter);

packages/localize/package-lock.json

+3-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/localize/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"dependencies": {
4848
"fs-extra": "^9.0.0",
4949
"glob": "^7.1.6",
50-
"jsonschema": "^1.2.6",
50+
"jsonschema": "^1.4.0",
5151
"lit-element": "^2.3.1",
5252
"lit-html": "^1.2.1",
5353
"minimist": "^1.2.5",
@@ -62,7 +62,6 @@
6262
"@types/diff": "^4.0.2",
6363
"@types/fs-extra": "^9.0.1",
6464
"@types/glob": "^7.1.1",
65-
"@types/jsonschema": "^1.1.1",
6665
"@types/minimist": "^1.2.0",
6766
"@types/mocha": "^8.0.2",
6867
"@types/node": "^14.0.1",

packages/localize/src/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export function readConfigFileAndWriteSchema(configPath: string): Config {
127127

128128
// eslint-disable-next-line @typescript-eslint/no-var-requires
129129
const schema = require('../config.schema.json');
130-
const result = jsonSchema.validate(parsed, schema, {propertyName: 'config'});
130+
const result = jsonSchema.validate(parsed, schema);
131131
if (result.errors.length > 0) {
132132
throw new KnownError(
133133
`Error validating config file ${configPath}:\n\n` +

0 commit comments

Comments
 (0)