Skip to content

Commit

Permalink
fix when source and target has different keys
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Feb 28, 2022
1 parent f111177 commit ee83c5b
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 6.0.3

- fix when source and target has different keys

### 6.0.2

- do not error on empty groups [#53](https://github.com/locize/xliff/issues/53)
Expand Down
13 changes: 9 additions & 4 deletions lib/createjs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function setSegment (category, srcObj, trgObj, ntObj, key) {
const srcValue = srcObj[key] || ''
const trgValue = trgObj[key] || ''
const srcValue = srcObj[key]
const trgValue = trgObj[key]

if (typeof srcValue === 'object' && typeof trgValue === 'object') {
category[key] = { groupUnits: {} }
Expand Down Expand Up @@ -36,19 +36,24 @@ const createjsClb = (srcLng, trgLng, srcKeys, trgKeys, ntKeys, ns, cb) => {

trgKeys = trgKeys || {}

const keys = Object.keys(srcKeys)
Object.keys(trgKeys).forEach((k) => {
if (keys.indexOf(k) < 0) keys.push(k)
})

if (ns && typeof ns === 'string') {
js.resources[ns] = {}
const nsObj = js.resources[ns]

Object.keys(srcKeys).forEach((srcKey) => {
keys.forEach((srcKey) => {
setSegment(nsObj, srcKeys, trgKeys, ntKeys, srcKey)
})

if (cb) cb(null, js)
return js
}

Object.keys(srcKeys).forEach((ns) => {
keys.forEach((ns) => {
js.resources[ns] = {}

Object.keys(srcKeys[ns]).forEach((srcKey) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/xliff12ToJs.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function createTransUnitTag (transUnit) {
}

return unit
}, { source: '' })
}, { /* source: '' */ })

return addAdditionalAttributes(jsUnit, transUnit.attributes)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/xliff2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const xliffToJsClb = (str, options, cb) => {
result.resources = xliffRoot.elements.reduce((resources, file) => {
const namespace = options.namespace || file.attributes.id

const initValues = { source: '', target: '' }
const initValues = { /* source: '', target: '' */ }
if (!result.targetLanguage) delete initValues.target

// namespace
Expand Down
33 changes: 33 additions & 0 deletions test/fixtures/example12_diff.xliff
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<xliff xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 http://docs.oasis-open.org/xliff/v1.2/os/xliff-core-1.2-strict.xsd" xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file original="namespace1" datatype="plaintext" source-language="en-US" target-language="ar">
<body>
<trans-unit id="key1">
<source>Hello</source>
<target>Hello (AR)</target>
</trans-unit>
<trans-unit id="key2_one">
<source>singular</source>
<target>singular (AR)</target>
</trans-unit>
<trans-unit id="key2_three">
<source>three</source>
</trans-unit>
<trans-unit id="key2_other">
<source>other</source>
<target>other (AR)</target>
</trans-unit>
<trans-unit id="key2_zero">
<target>zero (AR)</target>
</trans-unit>
<trans-unit id="key2_two">
<target>two (AR)</target>
</trans-unit>
<trans-unit id="key2_few">
<target>few (AR)</target>
</trans-unit>
<trans-unit id="key2_many">
<target>many (AR)</target>
</trans-unit>
</body>
</file>
</xliff>
1 change: 0 additions & 1 deletion test/fixtures/example_comments.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"resources": {
"file/path/wooo": {
"foo": {
"source": "",
"target": "woosh"
}
}
Expand Down
35 changes: 35 additions & 0 deletions test/fixtures/example_diff.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"resources": {
"namespace1": {
"key1": {
"source": "Hello",
"target": "Hello (AR)"
},
"key2_one": {
"source": "singular",
"target": "singular (AR)"
},
"key2_three": {
"source": "three"
},
"key2_other": {
"source": "other",
"target": "other (AR)"
},
"key2_zero": {
"target": "zero (AR)"
},
"key2_two": {
"target": "two (AR)"
},
"key2_few": {
"target": "few (AR)"
},
"key2_many": {
"target": "many (AR)"
}
}
},
"sourceLanguage": "en-US",
"targetLanguage": "ar"
}
47 changes: 47 additions & 0 deletions test/fixtures/example_diff.xliff
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en-US" trgLang="ar">
<file id="namespace1">
<unit id="key1">
<segment>
<source>Hello</source>
<target>Hello (AR)</target>
</segment>
</unit>
<unit id="key2_one">
<segment>
<source>singular</source>
<target>singular (AR)</target>
</segment>
</unit>
<unit id="key2_three">
<segment>
<source>three</source>
</segment>
</unit>
<unit id="key2_other">
<segment>
<source>other</source>
<target>other (AR)</target>
</segment>
</unit>
<unit id="key2_zero">
<segment>
<target>zero (AR)</target>
</segment>
</unit>
<unit id="key2_two">
<segment>
<target>two (AR)</target>
</segment>
</unit>
<unit id="key2_few">
<segment>
<target>few (AR)</target>
</segment>
</unit>
<unit id="key2_many">
<segment>
<target>many (AR)</target>
</segment>
</unit>
</file>
</xliff>
6 changes: 6 additions & 0 deletions test/fixtures/example_diff_source.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"key1": "Hello",
"key2_one": "singular",
"key2_three": "three",
"key2_other": "other"
}
9 changes: 9 additions & 0 deletions test/fixtures/example_diff_target.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"key1": "Hello (AR)",
"key2_zero": "zero (AR)",
"key2_one": "singular (AR)",
"key2_two": "two (AR)",
"key2_few": "few (AR)",
"key2_many": "many (AR)",
"key2_other": "other (AR)"
}
7 changes: 7 additions & 0 deletions test/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ module.exports = {
js_input_notes: require('./example_input_note.json'),
js_output_notes: require('./example_output_note.json')
},
example_diff: {
js: require('./example_diff.json'),
xliff: fixNewLines(fs.readFileSync(path.join(__dirname, 'example_diff.xliff')).toString()),
xliff12: fixNewLines(fs.readFileSync(path.join(__dirname, 'example12_diff.xliff')).toString()),
js_source: require('./example_diff_source.json'),
js_target: require('./example_diff_target.json')
},
example_source_attr: {
// The JS should be the same as example.js
xliff12: fixNewLines(fs.readFileSync(path.join(__dirname, 'example_source_attr12.xliff')).toString())
Expand Down
49 changes: 49 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,55 @@ describe('single', () => {
}, "with notes")
})

describe('xliff with different keys', () => {
test('createxliff12', (fn) => (done) => {
fn(
fixtures.example_diff.js.sourceLanguage,
fixtures.example_diff.js.targetLanguage,
fixtures.example_diff.js_source,
fixtures.example_diff.js_target,
'namespace1',
(err, res) => {
expect(err).not.to.be.ok()
console.log(res)
expect(res).to.eql(fixtures.example_diff.xliff12)
done()
}
)
})

test('createxliff', (fn) => (done) => {
fn(
fixtures.example_diff.js.sourceLanguage,
fixtures.example_diff.js.targetLanguage,
fixtures.example_diff.js_source,
fixtures.example_diff.js_target,
'namespace1',
(err, res) => {
expect(err).not.to.be.ok()
expect(res).to.eql(fixtures.example_diff.xliff)
done()
}
)
})

test('xliff2js', (fn) => (done) => {
fn(fixtures.example_diff.xliff, (err, res) => {
expect(err).not.to.be.ok()
expect(res).to.eql(fixtures.example_diff.js)
done()
})
})

test('xliff12ToJs', (fn) => (done) => {
fn(fixtures.example_diff.xliff12, (err, res) => {
expect(err).not.to.be.ok()
expect(res).to.eql(fixtures.example_diff.js)
done()
})
})
})

describe('xliff 1.2 source/target attributes', () => {
test('xliff12ToJs', (fn) => (done) => {
fn(fixtures.example_source_attr.xliff12, (err, res) => {
Expand Down
21 changes: 10 additions & 11 deletions xliff.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ exports.default = void 0;
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function setSegment(category, srcObj, trgObj, ntObj, key) {
var srcValue = srcObj[key] || '';
var trgValue = trgObj[key] || '';
var srcValue = srcObj[key];
var trgValue = trgObj[key];

if (_typeof(srcValue) === 'object' && _typeof(trgValue) === 'object') {
category[key] = {
Expand Down Expand Up @@ -49,18 +49,22 @@ var createjsClb = function createjsClb(srcLng, trgLng, srcKeys, trgKeys, ntKeys,
}

trgKeys = trgKeys || {};
var keys = Object.keys(srcKeys);
Object.keys(trgKeys).forEach(function (k) {
if (keys.indexOf(k) < 0) keys.push(k);
});

if (ns && typeof ns === 'string') {
js.resources[ns] = {};
var nsObj = js.resources[ns];
Object.keys(srcKeys).forEach(function (srcKey) {
keys.forEach(function (srcKey) {
setSegment(nsObj, srcKeys, trgKeys, ntKeys, srcKey);
});
if (cb) cb(null, js);
return js;
}

Object.keys(srcKeys).forEach(function (ns) {
keys.forEach(function (ns) {
js.resources[ns] = {};
Object.keys(srcKeys[ns]).forEach(function (srcKey) {
setSegment(js.resources[ns], srcKeys[ns], trgKeys[ns], ntKeys && ntKeys[ns], srcKey);
Expand Down Expand Up @@ -968,9 +972,7 @@ function createTransUnitTag(transUnit) {
}

return unit;
}, {
source: ''
});
}, {});
return addAdditionalAttributes(jsUnit, transUnit.attributes);
}

Expand Down Expand Up @@ -1070,10 +1072,7 @@ var xliffToJsClb = function xliffToJsClb(str, options, cb) {
});
result.resources = xliffRoot.elements.reduce(function (resources, file) {
var namespace = options.namespace || file.attributes.id;
var initValues = {
source: '',
target: ''
};
var initValues = {};
if (!result.targetLanguage) delete initValues.target;
file.elements = file.elements || [];
file.elements = file.elements.filter(function (child) {
Expand Down
2 changes: 1 addition & 1 deletion xliff.min.js

Large diffs are not rendered by default.

0 comments on commit ee83c5b

Please sign in to comment.