Skip to content

Commit

Permalink
fix: allow keys to be non-valid identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
tsvetomir committed Sep 18, 2017
1 parent 37fd49e commit 5c2d98f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 6 additions & 0 deletions sample/messages.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
<note priority="1" from="description">A goodbye message for the localized component</note>
<note priority="1" from="meaning">localized.component.goodbye</note>
</trans-unit>
<trans-unit id="b0459823058205380235820538108f51652e016a" datatype="html">
<source>One</source>
<target/>
<note priority="1" from="description">A message with non-alpha characters</note>
<note priority="1" from="meaning">localized.component.1</note>
</trans-unit>
<trans-unit id="225c24c98a62f1f8974e1c1206f6eac06d7e5b62" datatype="html">
<source>Créé par <x id="INTERPOLATION"/> le <x id="INTERPOLATION_1"/></source>
<target/>
Expand Down
8 changes: 5 additions & 3 deletions spec/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const lang = {
"localized": {
"component": {
"hello": "Bonjour!",
"goodbye": "Au Revoir!"
"goodbye": "Au Revoir!",
"1": "un"
}
}
};
Expand All @@ -31,6 +32,7 @@ describe("translate", function() {

expect(target(1)).toBe(lang.localized.component.hello);
expect(target(2)).toBe(lang.localized.component.goodbye);
expect(target(3)).toBe(lang.localized.component["1"]);
});

it("skips translated tagged units", function() {
Expand Down Expand Up @@ -58,13 +60,13 @@ describe("translate", function() {
it("skips units with interpolations", function() {
translate(messages, lang);

expect(target(3)).toBe('');
expect(target(4)).toBe('');
});

it("outputs interpolations", function() {
translate(messages, lang);

expect(units.eq(3).find('source').html()).toBe('Créé par <x id="INTERPOLATION"/> le <x id="INTERPOLATION_1"/>');
expect(units.eq(4).find('source').html()).toBe('Créé par <x id="INTERPOLATION"/> le <x id="INTERPOLATION_1"/>');
});

describe("with no target", function() {
Expand Down
3 changes: 2 additions & 1 deletion translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const translate = (doc, lang, force) => {
})
.filter(d => d.id && isKey(d.id))
.forEach(d => {
const query = '$.' + d.id;
const parts = d.id.split('.').map(part => `['${part}']`);
const query = '$' + parts.join('');
const match = jp.query(lang, query);

if (match.length === 1) {
Expand Down

0 comments on commit 5c2d98f

Please sign in to comment.