Skip to content

Commit a4721f6

Browse files
authored
Merge pull request #25 from PolymerLabs/xml
Add <xliff> element, remove prettier formatting, release
2 parents 98ff1eb + 60b6ce9 commit a4721f6

File tree

9 files changed

+96
-192
lines changed

9 files changed

+96
-192
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
<!-- ## Unreleased -->
99

10+
## [0.2.3] - 2020-05-13
11+
12+
- Fix missing `<xliff>` element in XLIFF output.
13+
- Formatting change to XML output (e.g. fewer line breaks).
14+
1015
## [0.2.2] - 2020-05-13
1116

1217
- Fix incorrect path resolution when loading XLB files.

package-lock.json

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

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lit-localize",
3-
"version": "0.2.2",
3+
"version": "0.2.3",
44
"description": "Localization for lit-html",
55
"license": "BSD-3-Clause",
66
"author": "The Polymer Project Authors",
@@ -28,13 +28,11 @@
2828
"prepack": "npm run build"
2929
},
3030
"dependencies": {
31-
"@prettier/plugin-xml": "^0.7.2",
3231
"fs-extra": "^9.0.0",
3332
"glob": "^7.1.6",
3433
"jsonschema": "^1.2.6",
3534
"minimist": "^1.2.5",
3635
"parse5": "^6.0.0",
37-
"prettier": "^2.0.5",
3836
"source-map-support": "^0.5.19",
3937
"typescript": "^3.8.3",
4038
"xmldom": "^0.3.0"
@@ -47,7 +45,6 @@
4745
"@types/minimist": "^1.2.0",
4846
"@types/node": "^14.0.1",
4947
"@types/parse5": "^5.0.2",
50-
"@types/prettier": "^2.0.0",
5148
"@types/xmldom": "^0.1.29",
5249
"@typescript-eslint/eslint-plugin": "^2.30.0",
5350
"@typescript-eslint/parser": "^2.30.0",
@@ -57,6 +54,7 @@
5754
"dir-compare": "^2.3.0",
5855
"eslint": "^7.0.0",
5956
"lit-html": "^1.2.1",
57+
"prettier": "^2.0.5",
6058
"rimraf": "^3.0.2",
6159
"typescript-json-schema": "^0.42.0"
6260
}

src/formatters/xlb.ts

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {ProgramMessage, Message, Bundle, Placeholder} from '../messages';
2020
import {
2121
getOneElementByTagNameOrThrow,
2222
getNonEmptyAttributeOrThrow,
23-
formatXml,
2423
} from './xml-utils';
2524

2625
/**
@@ -147,12 +146,16 @@ class XlbFormatter implements Formatter {
147146
*/
148147
async writeOutput(sourceMessages: ProgramMessage[]): Promise<void> {
149148
const doc = new xmldom.DOMImplementation().createDocument('', '', null);
149+
const indent = (node: Element | Document, level = 0) =>
150+
node.appendChild(doc.createTextNode('\n' + Array(level + 1).join(' ')));
150151
doc.appendChild(
151152
doc.createProcessingInstruction('xml', 'version="1.0" encoding="UTF-8"')
152153
);
154+
indent(doc);
153155
const bundle = doc.createElement('localizationbundle');
154156
bundle.setAttribute('locale', this.config.sourceLocale);
155157
doc.appendChild(bundle);
158+
indent(bundle, 1);
156159
const messagesNode = doc.createElement('messages');
157160
bundle.appendChild(messagesNode);
158161
for (const {name, contents, descStack} of sourceMessages) {
@@ -161,6 +164,7 @@ class XlbFormatter implements Formatter {
161164
if (descStack.length > 0) {
162165
messageNode.setAttribute('desc', descStack.join(' / '));
163166
}
167+
indent(messagesNode, 2);
164168
messagesNode.appendChild(messageNode);
165169
for (const content of contents) {
166170
if (typeof content === 'string') {
@@ -173,57 +177,14 @@ class XlbFormatter implements Formatter {
173177
}
174178
}
175179
}
180+
indent(messagesNode, 1);
181+
indent(bundle);
182+
indent(doc);
176183
const serialized = new xmldom.XMLSerializer().serializeToString(doc);
177-
const formatted = formatXml(serialized);
178184
await fsExtra.writeFile(
179185
this.config.resolve(this.xlbConfig.outputFile),
180-
formatted,
186+
serialized,
181187
'utf8'
182188
);
183189
}
184190
}
185-
186-
/**
187-
* Generate an XLB XML file for the given messages. This file contains the
188-
* canonical set of messages that will be translatd.
189-
*/
190-
export function generateXlb(
191-
messages: ProgramMessage[],
192-
locale: Locale
193-
): string {
194-
const doc = new xmldom.DOMImplementation().createDocument('', '', null);
195-
doc.appendChild(
196-
doc.createProcessingInstruction('xml', 'version="1.0" encoding="UTF-8"')
197-
);
198-
doc.appendChild(doc.createTextNode('\n'));
199-
const bundle = doc.createElement('localizationbundle');
200-
bundle.setAttribute('locale', locale);
201-
doc.appendChild(bundle);
202-
bundle.appendChild(doc.createTextNode('\n '));
203-
const messagesNode = doc.createElement('messages');
204-
bundle.appendChild(messagesNode);
205-
for (const {name, contents, descStack} of messages) {
206-
messagesNode.appendChild(doc.createTextNode('\n '));
207-
const messageNode = doc.createElement('msg');
208-
messageNode.setAttribute('name', name);
209-
if (descStack.length > 0) {
210-
messageNode.setAttribute('desc', descStack.join(' / '));
211-
}
212-
messagesNode.appendChild(messageNode);
213-
for (const content of contents) {
214-
if (typeof content === 'string') {
215-
messageNode.appendChild(doc.createTextNode(content));
216-
} else {
217-
const {untranslatable} = content;
218-
const ph = doc.createElement('ph');
219-
ph.appendChild(doc.createTextNode(untranslatable));
220-
messageNode.appendChild(ph);
221-
}
222-
}
223-
}
224-
messagesNode.appendChild(doc.createTextNode('\n '));
225-
bundle.appendChild(doc.createTextNode('\n'));
226-
doc.appendChild(doc.createTextNode('\n'));
227-
const serialized = new xmldom.XMLSerializer().serializeToString(doc);
228-
return serialized;
229-
}

src/formatters/xliff.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {Bundle, Message, ProgramMessage, Placeholder} from '../messages';
2020
import {
2121
getOneElementByTagNameOrThrow,
2222
getNonEmptyAttributeOrThrow,
23-
formatXml,
2423
} from './xml-utils';
2524

2625
/**
@@ -187,10 +186,12 @@ export class XliffFormatter implements Formatter {
187186
}
188187

189188
const doc = new xmldom.DOMImplementation().createDocument('', '', null);
189+
const indent = (node: Element | Document, level = 0) =>
190+
node.appendChild(doc.createTextNode('\n' + Array(level + 1).join(' ')));
190191
doc.appendChild(
191192
doc.createProcessingInstruction('xml', 'version="1.0" encoding="UTF-8"')
192193
);
193-
doc.appendChild(doc.createTextNode('\n'));
194+
indent(doc);
194195

195196
// https://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#xliff
196197
const xliff = doc.createElement('xliff');
@@ -200,27 +201,33 @@ export class XliffFormatter implements Formatter {
200201
'xsi:schemaLocation',
201202
'urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-strict.xsd'
202203
);
204+
doc.appendChild(xliff);
205+
indent(xliff);
203206

204207
// https://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#file
205208
const file = doc.createElement('file');
206-
doc.appendChild(file);
209+
xliff.appendChild(file);
210+
file.setAttribute('target-language', targetLocale);
211+
file.setAttribute('source-language', this.config.sourceLocale);
207212
// TODO The spec requires the source filename in the "original" attribute,
208213
// but we don't currently track filenames.
209214
file.setAttribute('original', 'lit-localize-inputs');
210215
// Plaintext seems right, as opposed to HTML, since our translatable
211216
// message text is just text, and all HTML markup is encoded into <ph>
212217
// elements.
213218
file.setAttribute('datatype', 'plaintext');
214-
file.setAttribute('source-language', this.config.sourceLocale);
215-
file.setAttribute('target-language', targetLocale);
219+
indent(file);
216220

221+
// https://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#body
217222
const body = doc.createElement('body');
218223
file.appendChild(body);
224+
indent(body);
219225

220226
for (const {name, contents: sourceContents, descStack} of sourceMessages) {
221227
// https://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#trans-unit
222228
const transUnit = doc.createElement('trans-unit');
223229
body.appendChild(transUnit);
230+
indent(transUnit, 1);
224231
transUnit.setAttribute('id', name);
225232

226233
if (descStack.length > 0) {
@@ -244,12 +251,18 @@ export class XliffFormatter implements Formatter {
244251
for (const child of this.encodeContents(doc, translation.contents)) {
245252
target.appendChild(child);
246253
}
254+
indent(transUnit, 1);
247255
transUnit.appendChild(target);
248256
}
257+
indent(transUnit);
258+
indent(body);
249259
}
260+
indent(file);
261+
indent(xliff);
262+
indent(doc);
250263
const serializer = new xmldom.XMLSerializer();
251264
const xmlStr = serializer.serializeToString(doc);
252-
return formatXml(xmlStr);
265+
return xmlStr;
253266
}
254267

255268
/**

src/formatters/xml-utils.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
* rights grant found at http://polymer.github.io/PATENTS.txt
1010
*/
1111

12-
import * as prettier from 'prettier';
13-
1412
/**
1513
* Query the given parent element for a descendent with the given tag name and
1614
* return it, or throw if none or more than one are found.
@@ -46,15 +44,3 @@ export function getNonEmptyAttributeOrThrow(
4644
}
4745
return attribute;
4846
}
49-
50-
/**
51-
* Format the given serialized XML using Prettier.
52-
*/
53-
export function formatXml(xmlStr: string): string {
54-
// TODO(aomarks) Types for the xml-parser plugin.
55-
return prettier.format(xmlStr, ({
56-
plugins: ['@prettier/plugin-xml'],
57-
parser: 'xml',
58-
xmlWhitespaceSensitivity: 'ignore',
59-
} as unknown) as prettier.Options);
60-
}

testdata/xlb/goldens/xlb/en.xlb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
<?xml version="1.0" encoding="UTF-8" ?>
1+
<?xml version="1.0" encoding="UTF-8"?>
22
<localizationbundle locale="en">
33
<messages>
44
<msg name="string">Hello World!</msg>
5-
<msg name="lit">Hello <ph>&lt;b>&lt;i></ph>World!<ph
6-
>&lt;/i>&lt;/b></ph></msg>
5+
<msg name="lit">Hello <ph>&lt;b>&lt;i></ph>World!<ph>&lt;/i>&lt;/b></ph></msg>
76
<msg name="variables_1">Hello <ph>${name}</ph>!</msg>
8-
<msg name="lit_variables_1">Hello <ph>${name}</ph>, click <ph
9-
>&lt;a href="${url}"></ph>here<ph>&lt;/a></ph>!</msg>
7+
<msg name="lit_variables_1">Hello <ph>${name}</ph>, click <ph>&lt;a href="${url}"></ph>here<ph>&lt;/a></ph>!</msg>
108
<msg name="lit_variables_2"><ph>${x}</ph>y<ph>${x}</ph>y<ph>${x}</ph></msg>
119
<msg name="lit_variables_3"><ph>&lt;b>
1210
${x}

0 commit comments

Comments
 (0)