Skip to content

Commit

Permalink
special handling for sizeRestriction attribute in xliff v2
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Oct 20, 2023
1 parent d707ee2 commit 73087ac
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 267 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 6.2.0

- special handling for sizeRestriction attribute in xliff v2

### 6.1.0

- introduce captureSpacesBetweenElements option
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2022 locize
Copyright (c) 2023 locize

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
28 changes: 25 additions & 3 deletions lib/js2xliff.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const js2xliffClb = (obj, opt, cb) => {
srcLang: obj.sourceLanguage,
trgLang: obj.targetLanguage
}

const hasSizeRestriction = !!Object.keys(obj.resources).find((nsName) => Object.keys(obj.resources[nsName]).find((k) => obj.resources[nsName][k].additionalAttributes && (obj.resources[nsName][k].additionalAttributes.sizeRestriction !== undefined || obj.resources[nsName][k].additionalAttributes['slr:sizeRestriction'] !== undefined)))
if (hasSizeRestriction) rootAttributes['xmlns:slr'] = 'urn:oasis:names:tc:xliff:sizerestriction:2.0'

const root = makeElement('xliff', rootAttributes, true)

Object.keys(obj.resources).forEach((nsName) => {
Expand All @@ -39,13 +43,27 @@ const js2xliffClb = (obj, opt, cb) => {
}

function createUnitTags (unitElements) {
return Object.keys(unitElements).map((key) => {
const hasSizeRestriction = !!Object.keys(unitElements).find((k) => unitElements[k].additionalAttributes && (unitElements[k].additionalAttributes.sizeRestriction !== undefined || unitElements[k].additionalAttributes['slr:sizeRestriction'] !== undefined))
const preElements = []
if (hasSizeRestriction) {
preElements.push({
type: 'element',
name: 'slr:profiles',
attributes: { generalProfile: 'xliff:codepoints', storageProfile: 'xliff:utf8' },
elements: [{
type: 'element',
name: 'slr:normalization',
attributes: { general: 'nfc', storage: 'nfc' }
}]
})
}
return preElements.concat(Object.keys(unitElements).map((key) => {
if (unitElements[key].groupUnits) {
return createGroupUnitTag(key, unitElements[key])
} else {
return createUnitTag(key, unitElements[key])
}
})
}))
}

function createGroupUnitTag (id, group) {
Expand All @@ -67,7 +85,11 @@ function createUnitTag (id, unit) {
})
subEle.unshift(makeElement('notes', null, noteElms))
}
const additionalAttributes = unit.additionalAttributes != null ? unit.additionalAttributes : {}
const additionalAttributes = unit.additionalAttributes != null ? { ...unit.additionalAttributes } : {}
if (additionalAttributes.sizeRestriction) {
additionalAttributes['slr:sizeRestriction'] = additionalAttributes.sizeRestriction + ''
delete additionalAttributes.sizeRestriction
}
return makeElement('unit', Object.assign({ id: escape(id) }, additionalAttributes), subEle)
}

Expand Down
6 changes: 6 additions & 0 deletions lib/xliff2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ function createUnits (parent, initValues) {
const additionalAttributes = unit.attributes
delete additionalAttributes.id

if (additionalAttributes['slr:sizeRestriction'] !== undefined) {
additionalAttributes.sizeRestriction = additionalAttributes['slr:sizeRestriction']
if (typeof additionalAttributes.sizeRestriction === 'string') additionalAttributes.sizeRestriction = parseInt(additionalAttributes.sizeRestriction)
delete additionalAttributes['slr:sizeRestriction']
}

switch (unit.name) {
case 'unit':
file[key] = createUnit(unit, initValues)
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,22 @@
"xml-js": "1.6.11"
},
"devDependencies": {
"@babel/cli": "7.14.5",
"@babel/core": "7.14.5",
"@babel/preset-env": "7.14.5",
"@babel/cli": "7.23.0",
"@babel/core": "7.23.2",
"@babel/preset-env": "7.23.2",
"babel-plugin-add-module-exports": "1.0.4",
"browserify": "17.0.0",
"cross-env": "7.0.3",
"eslint": "7.28.0",
"eslint-config-standard": "16.0.3",
"eslint-plugin-import": "2.23.4",
"eslint": "8.51.0",
"eslint-config-standard": "17.1.0",
"eslint-plugin-import": "2.28.1",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-promise": "5.1.0",
"eslint-plugin-promise": "6.1.1",
"eslint-plugin-require-path-exists": "1.1.9",
"eslint-plugin-standard": "4.1.0",
"expect.js": "0.3.1",
"mocha": "9.0.0",
"uglify-js": "3.13.9"
"mocha": "10.2.0",
"uglify-js": "3.17.4"
},
"scripts": {
"lint": "eslint .",
Expand Down
23 changes: 23 additions & 0 deletions test/fixtures/example_sizeRestriction.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"resources": {
"namespace1": {
"key1": {
"source": "Hello",
"target": "Hallo"
},
"key2": {
"source": "An application to manipulate and process XLIFF documents",
"target": "Eine Applikation um XLIFF Dokumente zu manipulieren und verarbeiten",
"additionalAttributes": {
"sizeRestriction": 60
}
},
"key.nested": {
"source": "XLIFF Data Manager",
"target": "XLIFF Daten Manager"
}
}
},
"sourceLanguage": "en-US",
"targetLanguage": "de-CH"
}
25 changes: 25 additions & 0 deletions test/fixtures/example_sizeRestriction.xliff
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en-US" trgLang="de-CH" xmlns:slr="urn:oasis:names:tc:xliff:sizerestriction:2.0">
<file id="namespace1">
<slr:profiles generalProfile="xliff:codepoints" storageProfile="xliff:utf8">
<slr:normalization general="nfc" storage="nfc"/>
</slr:profiles>
<unit id="key1">
<segment>
<source>Hello</source>
<target>Hallo</target>
</segment>
</unit>
<unit id="key2" slr:sizeRestriction="60">
<segment>
<source>An application to manipulate and process XLIFF documents</source>
<target>Eine Applikation um XLIFF Dokumente zu manipulieren und verarbeiten</target>
</segment>
</unit>
<unit id="key.nested">
<segment>
<source>XLIFF Data Manager</source>
<target>XLIFF Daten Manager</target>
</segment>
</unit>
</file>
</xliff>
4 changes: 4 additions & 0 deletions test/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,9 @@ module.exports = {
},
example_empty:{
xliff12: fixNewLines(fs.readFileSync(path.join(__dirname, 'example_empty12.xliff')).toString())
},
example_sizeRestriction: {
js: require('./example_sizeRestriction'),
xliff: fixNewLines(fs.readFileSync(path.join(__dirname, 'example_sizeRestriction.xliff')).toString())
}
}
19 changes: 19 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,25 @@ describe('with additional attributes', () => {
})
})

describe('with sizeRestriction attribute', () => {
describe('xliff 2.0', () => {
test('xliff2js', (fn) => (done) => {
fn(fixtures.example_sizeRestriction.xliff, (err, res) => {
expect(err).not.to.be.ok()
expect(res).to.eql(fixtures.example_sizeRestriction.js)
done()
})
})
test('js2xliff', (fn) => (done) => {
fn(fixtures.example_sizeRestriction.js, (err, res) => {
expect(err).not.to.be.ok()
expect(res).to.eql(fixtures.example_sizeRestriction.xliff)
done()
})
})
})
})

describe('Google Actions Xliff', () => {
test('xliff12ToJs', (fn) => (done) => {
fn(fixtures.example_google.xliff12, { xmlLangAttr: true }, (err, res) => {
Expand Down
Loading

0 comments on commit 73087ac

Please sign in to comment.