Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mean-cars-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lit/localize-tools': patch
---

Fix an issue where running `extract` on an existing translation target would rewrite the "id" for placeholders signifying the expression index, which breaks translation targets where the expressions need to be reordered.
5 changes: 2 additions & 3 deletions packages/localize-tools/src/formatters/xlb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,13 @@ class XlbFormatter implements Formatter {
}
indent(messagesNode, 2);
messagesNode.appendChild(messageNode);
let phIdx = 0;
for (const content of contents) {
if (typeof content === 'string') {
messageNode.appendChild(doc.createTextNode(content));
} else {
const {untranslatable} = content;
const {untranslatable, index} = content;
const ph = doc.createElement('ph');
ph.setAttribute('name', String(phIdx++));
ph.setAttribute('name', String(index));
ph.appendChild(doc.createTextNode(untranslatable));
messageNode.appendChild(ph);
}
Expand Down
11 changes: 5 additions & 6 deletions packages/localize-tools/src/formatters/xliff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,25 +278,24 @@ export class XliffFormatter implements Formatter {
*/
private encodeContents(doc: Document, contents: Message['contents']): Node[] {
const nodes = [];
// We need a unique ID within each source for each placeholder. The index
// will do.
let phIdx = 0;
for (const content of contents) {
if (typeof content === 'string') {
nodes.push(doc.createTextNode(content));
} else {
nodes.push(this.createPlaceholder(doc, String(phIdx++), content));
nodes.push(this.createPlaceholder(doc, content));
}
}
return nodes;
}

private createPlaceholder(
doc: Document,
id: string,
{untranslatable}: Placeholder
{untranslatable, index}: Placeholder
): HTMLElement {
const style = this.xliffConfig.placeholderStyle ?? 'x';
// We need a unique ID within each source for each placeholder. The index
// will do.
const id = String(index);
if (style === 'x') {
// https://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#x
const el = doc.createElement('x');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import {msg, str} from '@lit/localize';

const who = 'World';
const kind = 'Small';
msg(str`I am translated. Hello ${who}.`, {desc: 'Description of translated'});
msg(str`I am translated with swapped order. Hello ${kind} ish ${who}.`);
msg('I am not translated', {desc: 'Description of not translated'});
msg('I am translated with a note', {desc: 'Happy note'});
msg('My note needs to be migrated', {desc: 'Existing note'});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
<target state="translated">Estoy traducido. Hola <ph id="0">${who}</ph>.</target>
<note from="lit-localize">Description of translated</note>
</trans-unit>
<trans-unit id="s648aae3daabd8fcc">
<source>I am translated with swapped order. Hello <ph id="0">${kind}</ph> ish <ph id="1">${who}</ph>.</source>
<target state="translated">Estoy traducido con orden intercambiada. Hola <ph id="1">${who}</ph> más o menos <ph id="0">${kind}</ph>.</target>
</trans-unit>
<trans-unit id="s18fa8e4d6470399d">
<source foo:attribute="bar">I am translated with a note</source>
<target state="translated">Estoy traducido con una nota</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import {msg, str} from '@lit/localize';

const who = 'World';
const kind = 'Small';
msg(str`I am translated. Hello ${who}.`, {desc: 'Description of translated'});
msg(str`I am translated with swapped order. Hello ${kind} ish ${who}.`);
msg('I am not translated', {desc: 'Description of not translated'});
msg('I am translated with a note', {desc: 'Happy note'});
msg('My note needs to be migrated', {desc: 'Existing note'});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<source>I was translated. Hello <ph id="0">${who}</ph>.</source>
<target state="translated">Estoy traducido. Hola <ph id="0">${who}</ph>.</target>
</trans-unit>
<trans-unit id="s648aae3daabd8fcc">
<source>I am translated with swapped order. Hello <ph id="0">${kind}</ph> ish <ph id="1">${who}</ph>.</source>
<target state="translated">Estoy traducido con orden intercambiada. Hola <ph id="1">${who}</ph> más o menos <ph id="0">${kind}</ph>.</target>
</trans-unit>
<trans-unit id="s18fa8e4d6470399d">
<source foo:attribute="bar">I am translated with a note</source>
<target state="translated">Estoy traducido con una nota</target>
Expand Down