Skip to content

Commit 33eaa17

Browse files
authored
Merge pull request #22 from PolymerLabs/break-bug
Fix code-generation bugs and lint error
2 parents aabb720 + bc5d765 commit 33eaa17

File tree

10 files changed

+126
-27
lines changed

10 files changed

+126
-27
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ 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+
## [0.2.0] - 2020-05-13
911

1012
- Add support for the XLIFF localization interchange format:
1113
https://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html
@@ -14,6 +16,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1416
The interchange format is set with `interchange.format` (currently `xliff` or
1517
`xlb`), and other format-specific configuration is set in that object.
1618

19+
- Fix code generation bug where having more than one `targetLocale` would
20+
compile to invalid TypeScript (extra commas).
21+
22+
- Disable eslint warnings about camelcase for locale module imports like
23+
`zh_CN.ts`.
24+
1725
## [0.1.2] - 2020-05-09
1826

1927
- Add support for variables:

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lit-localize",
3-
"version": "0.1.2",
3+
"version": "0.2.0",
44
"description": "Localization for lit-html",
55
"license": "BSD-3-Clause",
66
"author": "The Polymer Project Authors",

src/module-generation.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,20 @@ export function generateMsgModule(
4646
(locale) =>
4747
`import {messages as ${locale.replace(
4848
'-',
49-
''
49+
'_'
5050
)}Messages} from './${locale}.js';`
5151
)
5252
.join('\n');
5353
return `
5454
// Do not modify this file by hand!
5555
// Re-generate this file by running lit-localize
5656
57+
/* eslint-disable @typescript-eslint/no-explicit-any */
58+
/* eslint-disable @typescript-eslint/camelcase */
59+
5760
import {TemplateResult} from 'lit-html';
5861
${localeImports}
5962
60-
/* eslint-disable @typescript-eslint/no-explicit-any */
61-
6263
export const supportedLocales = [${localesArray}] as const;
6364
6465
export type SupportedLocale = typeof supportedLocales[number];
@@ -113,12 +114,14 @@ export function generateMsgModule(
113114
case defaultLocale:
114115
resolved = source;
115116
break;
116-
${targetLocales.map(
117-
(locale) => `
117+
${targetLocales
118+
.map(
119+
(locale) => `
118120
case '${locale}':
119-
resolved = ${locale.replace('-', '')}Messages[name];
121+
resolved = ${locale.replace('-', '_')}Messages[name];
120122
break;`
121-
)}
123+
)
124+
.join('')}
122125
default:
123126
console.warn(\`\${locale} is not a supported locale\`);
124127
}
@@ -170,13 +173,13 @@ export function generateLocaleModule(
170173
}
171174
translatedMsgNames.add(msg.name);
172175
const msgStr = makeMessageString(msg.contents, canon);
173-
if (canon.isLitTemplate) {
174-
importLit = true;
175-
}
176176
const patchedMsgStr = applyPatches(patches, locale, msg.name, msgStr);
177177
entries.push(`${msg.name}: ${patchedMsgStr},`);
178178
}
179179
for (const msg of canonMsgs) {
180+
if (msg.isLitTemplate) {
181+
importLit = true;
182+
}
180183
if (translatedMsgNames.has(msg.name)) {
181184
continue;
182185
}

testdata/xlb/goldens/tsout/localization.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
// Do not modify this file by hand!
33
// Re-generate this file by running lit-localize
44

5-
import {TemplateResult} from 'lit-html';
6-
import {messages as es419Messages} from './es-419.js';
7-
85
/* eslint-disable @typescript-eslint/no-explicit-any */
6+
/* eslint-disable @typescript-eslint/camelcase */
7+
8+
import {TemplateResult} from 'lit-html';
9+
import {messages as es_419Messages} from './es-419.js';
910

1011
export const supportedLocales = ['en', 'es-419'] as const;
1112

@@ -63,7 +64,7 @@
6364
break;
6465

6566
case 'es-419':
66-
resolved = es419Messages[name];
67+
resolved = es_419Messages[name];
6768
break;
6869
default:
6970
console.warn(`${locale} is not a supported locale`);

testdata/xliff/goldens/lit-localize.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"$schema": "https://raw.githubusercontent.com/PolymerLabs/lit-localize/master/config.schema.json",
33
"sourceLocale": "en",
44
"targetLocales": [
5-
"es-419"
5+
"es-419",
6+
"zh_CN"
67
],
78
"tsConfig": "tsconfig.json",
89
"tsOut": "tsout",

testdata/xliff/goldens/tsout/localization.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
// Do not modify this file by hand!
33
// Re-generate this file by running lit-localize
44

5-
import {TemplateResult} from 'lit-html';
6-
import {messages as es419Messages} from './es-419.js';
7-
85
/* eslint-disable @typescript-eslint/no-explicit-any */
6+
/* eslint-disable @typescript-eslint/camelcase */
97

10-
export const supportedLocales = ['en', 'es-419'] as const;
8+
import {TemplateResult} from 'lit-html';
9+
import {messages as es_419Messages} from './es-419.js';
10+
import {messages as zh_CNMessages} from './zh_CN.js';
11+
12+
export const supportedLocales = ['en', 'es-419', 'zh_CN'] as const;
1113

1214
export type SupportedLocale = typeof supportedLocales[number];
1315

@@ -63,7 +65,10 @@
6365
break;
6466

6567
case 'es-419':
66-
resolved = es419Messages[name];
68+
resolved = es_419Messages[name];
69+
break;
70+
case 'zh_CN':
71+
resolved = zh_CNMessages[name];
6772
break;
6873
default:
6974
console.warn(`${locale} is not a supported locale`);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
// Do not modify this file by hand!
3+
// Re-generate this file by running lit-localize
4+
5+
import {html} from 'lit-html';
6+
7+
/* eslint-disable no-irregular-whitespace */
8+
/* eslint-disable @typescript-eslint/camelcase */
9+
/* eslint-disable @typescript-eslint/no-explicit-any */
10+
11+
export const messages = {
12+
string: `Hello World!`,
13+
lit: html`Hello <b><i>World!</i></b>`,
14+
variables_1: (name: any) => `Hello ${name}!`,
15+
lit_variables_1: (url: any, name: any) => html`Hello ${name}, click <a href="${url}">here</a>!`,
16+
lit_variables_2: (x: any) => html`${x}y${x}y${x}`,
17+
lit_variables_3: (x: any) => html`<b>
18+
${x}
19+
</b>
20+
<i>
21+
y
22+
</i>
23+
<b>
24+
${x}
25+
</b>
26+
<i>
27+
y
28+
</i>
29+
<b>
30+
${x}
31+
</b>`,
32+
};
33+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<file
3+
original="lit-localize-inputs"
4+
datatype="plaintext"
5+
source-language="en"
6+
target-language="zh_CN"
7+
>
8+
<body>
9+
<trans-unit id="string">
10+
<source>Hello World!</source>
11+
</trans-unit>
12+
<trans-unit id="lit">
13+
<source>Hello <ph id="0">&lt;b>&lt;i></ph>World!<ph
14+
id="1"
15+
>&lt;/i>&lt;/b></ph></source>
16+
</trans-unit>
17+
<trans-unit id="variables_1">
18+
<source>Hello <ph id="0">${name}</ph>!</source>
19+
</trans-unit>
20+
<trans-unit id="lit_variables_1">
21+
<source>Hello <ph id="0">${name}</ph>, click <ph
22+
id="1"
23+
>&lt;a href="${url}"></ph>here<ph id="2">&lt;/a></ph>!</source>
24+
</trans-unit>
25+
<trans-unit id="lit_variables_2">
26+
<source><ph id="0">${x}</ph>y<ph id="1">${x}</ph>y<ph
27+
id="2"
28+
>${x}</ph></source>
29+
</trans-unit>
30+
<trans-unit id="lit_variables_3">
31+
<source><ph id="0">&lt;b>
32+
${x}
33+
&lt;/b>
34+
&lt;i></ph>
35+
y
36+
<ph id="1">&lt;/i>
37+
&lt;b>
38+
${x}
39+
&lt;/b>
40+
&lt;i></ph>
41+
y
42+
<ph id="2">&lt;/i>
43+
&lt;b>
44+
${x}
45+
&lt;/b></ph></source>
46+
</trans-unit>
47+
</body>
48+
</file>

testdata/xliff/input/lit-localize.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"sourceLocale": "en",
3-
"targetLocales": ["es-419"],
3+
"targetLocales": ["es-419", "zh_CN"],
44
"tsConfig": "tsconfig.json",
55
"tsOut": "tsout",
66
"interchange": {

0 commit comments

Comments
 (0)