Skip to content

Commit 56ba5f8

Browse files
committed
Update transform mode for API changes made in PR #40
1 parent 0c6a455 commit 56ba5f8

File tree

9 files changed

+102
-73
lines changed

9 files changed

+102
-73
lines changed

README.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ The `lit-localize` module exports the following functions:
2121
Set configuration parameters for lit-localize when in runtime mode. Returns an
2222
object with functions:
2323

24-
- [`getLocale`](#getLocale): Return the active locale code.
25-
- [`setLocale`](#setLocale): Set the active locale code.
24+
- [`getLocale`](#getlocale-string): Return the active locale code.
25+
- [`setLocale`](#setlocalelocale-string-promise): Set the active locale code.
2626

2727
Throws if called more than once.
2828

29+
When in transform mode, the lit-localize CLI will error if this function is
30+
called. Use
31+
[`configureTransformLocalization`](#configuretransformlocalizationconfiguration)
32+
instead.
33+
2934
The `configuration` object must have the following properties:
3035

3136
- `sourceLocale: string`: Required locale code in which source templates in this
@@ -52,12 +57,13 @@ const {getLocale, setLocale} = configureLocalization({
5257
### `configureTransformLocalization(configuration)`
5358

5459
Set configuration parameters for lit-localize when in transform mode. Returns an
55-
object with functions:
60+
object with function:
5661

57-
- [`getLocale`](#getLocale): Return the active locale code.
62+
- [`getLocale`](#getlocale-string): Return the active locale code.
5863

59-
(Note that [`setLocale`](#setLocale) is not available, because changing locales
60-
at runtime is not supported in transform mode.)
64+
(Note that [`setLocale`](#setlocalelocale-string-promise) is not available from
65+
this function, because changing locales at runtime is not supported in transform
66+
mode.)
6167

6268
Throws if called more than once.
6369

@@ -74,14 +80,19 @@ const {getLocale} = configureLocalization({
7480
});
7581
```
7682

83+
In transform mode, calls to this function are transformed to an object with a
84+
`getLocale` implementation that returns the static locale code for each locale
85+
bundle. For example:
86+
87+
```typescript
88+
const {getLocale} = {getLocale: () => 'es-419'};
89+
```
90+
7791
### `getLocale(): string`
7892

7993
Return the active locale code.
8094

81-
In transform mode, calls to this function are transformed into the static locale
82-
code string for each emitted locale.
83-
84-
### `setLocale(locale: string)`
95+
### `setLocale(locale: string): Promise`
8596

8697
Set the active locale code, and begin loading templates for that locale using
8798
the `loadLocale` function that was passed to `configureLocalization`. Returns a

src/outputters/transform.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -118,29 +118,40 @@ class Transformer {
118118
return undefined;
119119
}
120120

121-
// configureLocalization(...) -> undefined
121+
// configureTransformLocalization(...) -> {getLocale: () => "es-419"}
122122
if (
123-
this.isCallToTaggedFunction(node, '_LIT_LOCALIZE_CONFIGURE_LOCALIZATION_')
123+
this.isCallToTaggedFunction(
124+
node,
125+
'_LIT_LOCALIZE_CONFIGURE_TRANSFORM_LOCALIZATION_'
126+
)
124127
) {
125-
return ts.createIdentifier('undefined');
126-
}
127-
128-
// getLocale() -> "es-419"
129-
if (this.isCallToTaggedFunction(node, '_LIT_LOCALIZE_GET_LOCALE_')) {
130-
return ts.createStringLiteral(this.locale);
131-
}
132-
133-
// setLocale("es-419") -> undefined
134-
if (this.isCallToTaggedFunction(node, '_LIT_LOCALIZE_SET_LOCALE_')) {
135-
return ts.createIdentifier('undefined');
128+
return ts.createObjectLiteral(
129+
[
130+
ts.createPropertyAssignment(
131+
ts.createIdentifier('getLocale'),
132+
ts.createArrowFunction(
133+
undefined,
134+
undefined,
135+
[],
136+
undefined,
137+
ts.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
138+
ts.createStringLiteral(this.locale)
139+
)
140+
),
141+
],
142+
false
143+
);
136144
}
137145

138-
// localeReady() -> Promise.resolve(undefined)
139-
if (this.isCallToTaggedFunction(node, '_LIT_LOCALIZE_LOCALE_READY_')) {
140-
return ts.createCall(
141-
ts.createPropertyAccess(ts.createIdentifier('Promise'), 'resolve'),
142-
[],
143-
[ts.createIdentifier('undefined')]
146+
// configureLocalization(...) -> Error
147+
if (
148+
this.isCallToTaggedFunction(node, '_LIT_LOCALIZE_CONFIGURE_LOCALIZATION_')
149+
) {
150+
// TODO(aomarks) This error is not surfaced earlier in the analysis phase
151+
// as a nicely formatted diagnostic, but it should be.
152+
throw new KnownError(
153+
'Cannot use configureLocalization in transform mode. ' +
154+
'Use configureTransformLocalization instead.'
144155
);
145156
}
146157

src/tests/transform.unit.test.ts

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -334,51 +334,34 @@ test('exclude different msg function', (t) => {
334334
);
335335
});
336336

337-
test('configureLocalization() -> undefined', (t) => {
337+
test('configureTransformLocalization() -> {getLocale: () => "es-419"}', (t) => {
338338
checkTransform(
339339
t,
340-
`import {configureLocalization} from './lib_client/index.js';
341-
configureLocalization({
340+
`import {configureTransformLocalization} from './lib_client/index.js';
341+
const {getLocale} = configureTransformLocalization({
342342
sourceLocale: 'en',
343-
targetLocales: ['es-419'],
344-
loadLocale: (locale: string) => import(\`/\${locale}.js\`),
345-
});`,
346-
`undefined;`
347-
);
348-
});
349-
350-
test('getLocale() -> "es-419"', (t) => {
351-
checkTransform(
352-
t,
353-
`import {getLocale} from './lib_client/index.js';
354-
getLocale();`,
355-
`"en";`,
356-
{locale: 'es-419'}
357-
);
358-
359-
checkTransform(
360-
t,
361-
`import {getLocale} from './lib_client/index.js';
362-
getLocale();`,
363-
`"es-419";`,
343+
});
344+
const locale = getLocale();`,
345+
`const {getLocale} = {getLocale: () => 'es-419'};
346+
const locale = getLocale();`,
364347
{locale: 'es-419'}
365348
);
366349
});
367350

368-
test('setLocale() -> undefined', (t) => {
369-
checkTransform(
370-
t,
371-
`import {setLocale} from './lib_client/index.js';
372-
setLocale("es-419");`,
373-
`undefined;`
374-
);
375-
});
376-
377-
test('localeReady() -> Promise.resolve(undefined)', (t) => {
378-
checkTransform(
379-
t,
380-
`import {localeReady} from './lib_client/index.js';
381-
localeReady().then(() => console.log('ok'))`,
382-
`Promise.resolve(undefined).then(() => console.log('ok'))`
351+
test('configureLocalization() throws', (t) => {
352+
t.throws(
353+
() =>
354+
checkTransform(
355+
t,
356+
`import {configureLocalization} from './lib_client/index.js';
357+
configureLocalization({
358+
sourceLocale: 'en',
359+
targetLocales: ['es-419'],
360+
loadLocale: (locale: string) => import(\`/\${locale}.js\`),
361+
});`,
362+
`undefined;`
363+
),
364+
undefined,
365+
'Cannot use configureLocalization in transform mode'
383366
);
384367
});

src_client/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ let loading = new Deferred<void>();
111111
*
112112
* Throws if called more than once.
113113
*/
114-
export const configureLocalization: ((config: RuntimeConfiguration) => void) & {
114+
export const configureLocalization: ((
115+
config: RuntimeConfiguration
116+
) => {getLocale: typeof getLocale; setLocale: typeof setLocale}) & {
115117
_LIT_LOCALIZE_CONFIGURE_LOCALIZATION_?: never;
116118
} = (config: RuntimeConfiguration) => {
117119
if (configured === true) {
@@ -133,14 +135,18 @@ export const configureLocalization: ((config: RuntimeConfiguration) => void) & {
133135
*
134136
* Throws if called more than once.
135137
*/
136-
export function configureTransformLocalization(config: TransformConfiguration) {
138+
export const configureTransformLocalization: ((
139+
config: TransformConfiguration
140+
) => {getLocale: typeof getLocale}) & {
141+
_LIT_LOCALIZE_CONFIGURE_TRANSFORM_LOCALIZATION_?: never;
142+
} = (config: TransformConfiguration) => {
137143
if (configured === true) {
138144
throw new Error('lit-localize can only be configured once');
139145
}
140146
configured = true;
141147
activeLocale = sourceLocale = config.sourceLocale;
142148
return {getLocale};
143-
}
149+
};
144150

145151
/**
146152
* Return the active locale code.

testdata/transform/goldens/foo.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import {html} from 'lit-html';
2-
import {msg} from '../../../lib_client/index.js';
2+
import {
3+
msg,
4+
configureTransformLocalization,
5+
} from '../../../lib_client/index.js';
6+
7+
const {getLocale} = configureTransformLocalization({sourceLocale: 'en'});
8+
console.log(`Locale is ${getLocale()}`);
39

410
msg('string', 'Hello World!');
511

testdata/transform/goldens/tsout/en/foo.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import {html} from 'lit-html';
2+
const {getLocale} = {getLocale: () => 'en'};
3+
console.log(`Locale is ${getLocale()}`);
24
('Hello World!');
35
html`Hello <b><i>World!</i></b>`;
46
`Hello World!`;

testdata/transform/goldens/tsout/es-419/foo.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import {html} from 'lit-html';
2+
const {getLocale} = {getLocale: () => 'es-419'};
3+
console.log(`Locale is ${getLocale()}`);
24
`Hola Mundo!`;
35
html`Hola <b><i>Mundo!</i></b>`;
46
`Hola World!`;

testdata/transform/goldens/tsout/zh_CN/foo.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import {html} from 'lit-html';
2+
const {getLocale} = {getLocale: () => 'zh_CN'};
3+
console.log(`Locale is ${getLocale()}`);
24
`\u4F60\u597D\uFF0C\u4E16\u754C\uFF01`;
35
html`你好, <b><i>世界!</i></b>`;
46
`Hello World!`;

testdata/transform/input/foo.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import {html} from 'lit-html';
2-
import {msg} from '../../../lib_client/index.js';
2+
import {
3+
msg,
4+
configureTransformLocalization,
5+
} from '../../../lib_client/index.js';
6+
7+
const {getLocale} = configureTransformLocalization({sourceLocale: 'en'});
8+
console.log(`Locale is ${getLocale()}`);
39

410
msg('string', 'Hello World!');
511

0 commit comments

Comments
 (0)