From 686b1d156539c6de814b0513291c5238b4a71859 Mon Sep 17 00:00:00 2001 From: hongvan Date: Tue, 12 Jun 2018 14:51:35 +0700 Subject: [PATCH 1/2] Add options to add more attribute to xliff template --- README.md | 25 +++++++++++++++++++++++++ xliff-conv.js | 14 ++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/README.md b/README.md index 05a3b9c..93eab73 100644 --- a/README.md +++ b/README.md @@ -354,6 +354,31 @@ XLIFF to/from JSON converter for Polymer [i18n-behavior](https://github.com/t2ym ``` +- Added new attribute to transUnitTemplate +``` + xliffConv.parseJSON(bundles, { + srcLanguage: srcLanguage, + destLanguage: destLanguage, + addNewAttr: { + newAttrName: labelArrayWithUniqueId + } + }, function (output) { + fs.writeFile(path.join(xliffPath, 'bundle.' + destLanguage + '.xlf'), output, resolve); + }); +``` +#### Note: +labelArrayWithUniqueId is array contain new attribute for each id +``` +{ + "Factory_audit_address": "ckv7ymf07ahqog4lur12bwobg1z3dsxzkqkdwxan", + "alert_info_when_update_config_preferences": "ybsqyempsolypcf4poq1wdxxl8c04oam03ei27bc", + "application_title": "rj7rtcdbefchcbrq9itw6sewjifd2v3c5dn99969", + "back": "48gtruuew3ndd7pnj26lttt0kbgnlv2iyhtti99v", + "barcode_section": "i2d0t2y11b5zlrlhbn5it8qkbxbp7ub0bdgxy7tr", + "cancel_title": "bbzgu18z7wl6thj0eh9p83nlcrz4znyfox4khjuq", + "client_initial_2_letter": "ilttwryn5jccb4wnhfu3nq9z72ds21m2ho7fnsgs" + } + ``` - callback: Function, callback(output) with output XLIFF as a string #### Notes: diff --git a/xliff-conv.js b/xliff-conv.js index 4b30bd6..f849bff 100755 --- a/xliff-conv.js +++ b/xliff-conv.js @@ -674,6 +674,20 @@ Copyright (c) 2016, Tetsuya Mori . All rights reserved. 'export' : true }); // update state + + // --Customise id and add new attribute -- + + if (options.addNewAttr) { + transUnit.setAttribute('id', id); + var newAttrArr = Object.getOwnPropertyNames(options.addNewAttr); + for (var item in newAttrArr) { + var valueOfNewAttr = options.addNewAttr[newAttrArr[item]]; + transUnit.setAttribute(newAttrArr[item], valueOfNewAttr[id]); + } + } + + // ---End--- + state = this.xliffStates[op][0]; if (op === 'default' && !transUnit.hasAttribute('approved')) { transUnit.setAttribute('approved', 'yes'); From c518af2f357b34ab732adc597400772d2068b7af Mon Sep 17 00:00:00 2001 From: hongvan Date: Wed, 20 Jun 2018 16:14:27 +0700 Subject: [PATCH 2/2] Update xliff and add test --- test/custom-test.js | 330 ++++++++++++++++++++++++++++++++++++++++++++ test/test.js | 3 + xliff-conv.js | 9 +- 3 files changed, 336 insertions(+), 6 deletions(-) create mode 100644 test/custom-test.js diff --git a/test/custom-test.js b/test/custom-test.js new file mode 100644 index 0000000..35aa831 --- /dev/null +++ b/test/custom-test.js @@ -0,0 +1,330 @@ +/* + @license https://github.com/t2ym/xliff-conv/blob/master/LICENSE.md + Copyright (c) 2016, Tetsuya Mori . All rights reserved. + */ +(function (root, factory) { + + 'use strict'; + + if (typeof define === 'function' && define.amd) { + // AMD. Register as an anonymous module. + define([], function () { + return (root.XliffConvCustomTest = factory()); + }); + } else if (typeof exports === 'object') { + // Node. Does not work with strict CommonJS, but + // only CommonJS-like enviroments that support module.exports, + // like Node. + module.exports = factory(); + } else { + // Browser globals + root.XliffConvCustomTest = factory(); + } + +}(this, function () { +// UMD Definition above, do not remove this line + 'use strict'; + + var XliffConvCustomTest = function () {}; + + XliffConvCustomTest.prototype.run = function (XliffConv, chai) { + var assert = chai.assert; + + var bundles = { + '': { + 'app': { + "Factory_audit_address": "Address", + "alert_info_when_update_config_preferences": "Please use the arrow buttons above the columns to reposition the columns in the table. Click the Save button to save your changes.", + "application_title": "Pivot88", + "back": "Back", + "barcode_section": "Barcode Section", + "cancel_title": "Cancel", + "close_preference": "Close Preferences", + "completed": "Completed", + "currentTranslationAtLoading": "Current Translation", + "currentTranslationTag": "Translation Tag", + "docview": "Document View", + "edit_section": "Edit", + "editio_of_doc_create_new_form": "Create New Form" + } + }, + 'en_US': { + 'app': { + "Factory_audit_address": "Address", + "alert_info_when_update_config_preferences": "Please use the arrow buttons above the columns to reposition the columns in the table. Click the Save button to save your changes.", + "application_title": "Pivot88", + "back": "Back", + "barcode_section": "Barcode Section", + "cancel_title": "Cancel", + "close_preference": "Close Preferences", + "completed": "Completed", + "currentTranslationAtLoading": "Current Translation", + "currentTranslationTag": "Translation Tag", + "docview": "Document View", + "edit_section": "Edit", + "editio_of_doc_create_new_form": "Create New Form" + } + }, + 'es_ES': { + 'app': { + "Factory_audit_address": "Address", + "alert_info_when_update_config_preferences": "Please use the arrow buttons above the columns to reposition the columns in the table. Click the Save button to save your changes.", + "application_title": "Pivot88", + "back": "Back", + "barcode_section": "Barcode Section", + "cancel_title": "Cancel", + "close_preference": "Close Preferences", + "completed": "Completed", + "currentTranslationAtLoading": "Current Translation", + "currentTranslationTag": "Translation Tag", + "docview": "Document View", + "edit_section": "Edit", + "editio_of_doc_create_new_form": "Create New Form" + } + }, + 'fr_CA': { + 'app': { + "Factory_audit_address": "Adresse", + "alert_info_when_update_config_preferences": "Utilisez les boutons fléchés situés au-dessus des colonnes pour repositionner les colonnes de la table. Cliquez sur le bouton Enregistrer pour enregistrer vos modifications.", + "application_title": "Pivot88", + "back": "Arrière", + "barcode_section": "Section des codes à barres", + "cancel_title": "Annuler", + "close_preference": "Fermer les préférences", + "completed": "Complété", + "currentTranslationAtLoading": "Traduction en cours", + "currentTranslationTag": "Tag de traduction", + "docview": "Vue du document", + "edit_section": "Modifier", + "editio_of_doc_create_new_form": "Créer un Nouveau formulaire" + } + }, + 'zh_CN': { + 'app': { + "Factory_audit_address": "地址", + "alert_info_when_update_config_preferences": "请使用列上方的箭头按钮重新定位表中的列。单击保存按钮保存更改。", + "application_title": "Pivot88", + "back": "背部", + "barcode_section": "条码部分", + "cancel_title": "取消", + "close_preference": "CN_Close Preferences", + "completed": "CN_Completed", + "currentTranslationAtLoading": "当前翻译", + "currentTranslationTag": "翻译标签", + "docview": "文档视图", + "edit_section": "编辑", + "editio_of_doc_create_new_form": "Create New Form" + } + }, + 'ja_JP': { + 'app': { + "Factory_audit_address": "Address", + "alert_info_when_update_config_preferences": "Please use the arrow buttons above the columns to reposition the columns in the table. Click the Save button to save your changes.", + "application_title": "Pivot88", + "back": "Back", + "barcode_section": "Barcode Section", + "cancel_title": "Cancel", + "close_preference": "Close Preferences", + "completed": "Completed", + "currentTranslationAtLoading": "Current Translation", + "currentTranslationTag": "Translation Tag", + "docview": "Document View", + "edit_section": "Edit", + "editio_of_doc_create_new_form": "Create New Form" + } + }, + 'vi_VN': { + 'app': { + "Factory_audit_address": "Địa chỉ", + "alert_info_when_update_config_preferences": "Please use the arrow buttons above the columns to reposition the columns in the table. Click the Save button to save your changes.", + "application_title": "Pivot88", + "back": "Quay lại", + "barcode_section": "Barcode Section", + "cancel_title": "Hủy bỏ", + "close_preference": "Close Preferences", + "completed": "Hoàn thành", + "currentTranslationAtLoading": "Phiên dịch", + "currentTranslationTag": "Thẻ phiên dịch", + "docview": "Xem bản ghi", + "edit_section": "Chỉnh sửa", + "editio_of_doc_create_new_form": "Tạo bản mới" + } + }, + 'km_KH': { + 'app': { + "Factory_audit_address": "Address", + "alert_info_when_update_config_preferences": "Please use the arrow buttons above the columns to reposition the columns in the table. Click the Save button to save your changes.", + "application_title": "Pivot88", + "back": "Back", + "barcode_section": "Barcode Section", + "cancel_title": "Cancel", + "close_preference": "Close Preferences", + "completed": "Completed", + "currentTranslationAtLoading": "Current Translation", + "currentTranslationTag": "Translation Tag", + "docview": "Document View", + "edit_section": "Edit", + "editio_of_doc_create_new_form": "Create New Form" + } + }, + 'ar_SA': { + 'app': { + "Factory_audit_address": "Address", + "alert_info_when_update_config_preferences": "Please use the arrow buttons above the columns to reposition the columns in the table. Click the Save button to save your changes.", + "application_title": "Pivot88", + "back": "Back", + "barcode_section": "Barcode Section", + "cancel_title": "Cancel", + "close_preference": "Close Preferences", + "completed": "Completed", + "currentTranslationAtLoading": "Current Translation", + "currentTranslationTag": "Translation Tag", + "docview": "Document View", + "edit_section": "Edit", + "editio_of_doc_create_new_form": "Create New Form" + } + } + }; + + var labelArrayWithUniqueId = { + "Factory_audit_address": "ckv7ymf07ahqog4lur12bwobg1z3dsxzkqkdwxan", + "alert_info_when_update_config_preferences": "ybsqyempsolypcf4poq1wdxxl8c04oam03ei27bc", + "application_title": "rj7rtcdbefchcbrq9itw6sewjifd2v3c5dn99969", + "back": "48gtruuew3ndd7pnj26lttt0kbgnlv2iyhtti99v", + "barcode_section": "i2d0t2y11b5zlrlhbn5it8qkbxbp7ub0bdgxy7tr", + "cancel_title": "bbzgu18z7wl6thj0eh9p83nlcrz4znyfox4khjuq", + "close_preference": "6cq3j5vhf8cxuabhkk00vfy75yyfuuirrfdaz2p2", + "completed": "ao6els6irzw86l0vkb0p4xy1prn4kx31hbhmhlkf", + "currentTranslationAtLoading": "65bwg3bhawcu41lrkdqb4hcgitonsjsozrp9tlbb", + "currentTranslationTag": "eao45l68agv6ci72et2ok9ojgx9y0zrez1bw1xdb", + "docview": "40wdqmuqbzozk4qgp6cdcxugpn9h5vwqlvapnari", + "edit_section": "8opyi6bhov3muf1obbr1szgq0rc5jcrdtyfsztxq", + "editio_of_doc_create_new_form": "l5w3jfx0kgmjonxpaw4srjwjtbheve4xu69eulbr" + }; + + var xliff_expected = + '\n' + + '\n' + + '\n' + + ' \n' + + '
\n' + + ' \n' + + '
\n' + + ' \n' + + ' \n' + + ' Address\n' + + ' Adresse\n' + + ' \n' + + ' \n' + + ' Please use the arrow buttons above the columns to reposition the columns in the table. Click the Save button to save your changes.\n' + + ' Utilisez les boutons fléchés situés au-dessus des colonnes pour repositionner les colonnes de la table. Cliquez sur le bouton Enregistrer pour enregistrer vos modifications.\n' + + ' \n' + + ' \n' + + ' Pivot88\n' + + ' Pivot88\n' + + ' \n' + + ' \n' + + ' Back\n' + + ' Arrière\n' + + ' \n' + + ' \n' + + ' Barcode Section\n' + + ' Section des codes à barres\n' + + ' \n' + + ' \n' + + ' Cancel\n' + + ' Annuler\n' + + ' \n' + + ' \n' + + ' Close Preferences\n' + + ' Fermer les préférences\n' + + ' \n' + + ' \n' + + ' Completed\n' + + ' Complété\n' + + ' \n' + + ' \n' + + ' Current Translation\n' + + ' Traduction en cours\n' + + ' \n' + + ' \n' + + ' Translation Tag\n' + + ' Tag de traduction\n' + + ' \n' + + ' \n' + + ' Document View\n' + + ' Vue du document\n' + + ' \n' + + ' \n' + + ' Edit\n' + + ' Modifier\n' + + ' \n' + + ' \n' + + ' Create New Form\n' + + ' Créer un Nouveau formulaire\n' + + ' \n' + + ' \n' + + '
\n' + + '
'; + + var srcLanguage = 'en_US'; + var destLanguage = 'fr_CA'; + var logs = []; + var warnLogs = []; + var errorLogs = []; + + describe('xliff-conv with customise id and add new attribute', function () { + + var xliffConv = new XliffConv({ + logger: function (log) { logs.push(log); }, + warnLogger: function (log) { warnLogs.push(log); }, + errorLogger: function (log) { errorLogs.push(log); }, + date: new Date(0), + xliffStates: { + 'add': [ 'new' ], + 'replace': [ 'needs-translation', 'needs-adaptation', 'needs-l10n', '[invalid===expression]', '[!state&&!approved&&source==target]', '' ], + 'review' : [ 'needs-review-translation', 'needs-review-adaptation', 'needs-review-l10n', '[!state&&!approved&&source!=target]' ], + 'empty' : [ '""', + '[export&&source~=tags&&file.target-language~=de&&translate:=no&&target.state:=""&&trans-unit.approved:=yes&&target:=replaced value&&inexistent:=value&&inexistent.attr:=value]' ], + 'default': [ 'translated', 'signed-off', 'final', + '[state==new&&source~=annotations]', '[approved]' ] + } + }); + + it('parseJSON with customise id and add new attribute SUCCESSFUL', function (callback) { + xliffConv.parseJSON(bundles, { + srcLanguage: srcLanguage, + destLanguage: destLanguage, + addNewAttr: { + resname: labelArrayWithUniqueId + } + }, function (output) { + assert.equal( + output + // patch output without affecting XML semantics + .replace('', + '') + .replace('', + '') + .replace('', + '') + .replace(/approved="yes" translate="no"/g, 'translate="no" approved="yes"') + .replace(/<a>/g, '<a>'), + xliff_expected + .replace(/tool-version="[.0-9]*"/g, 'tool-version="' + xliffConv.toolVersion + '"')); + callback(); + }) + }); + + it('parseJSON with invalid arguments', function () { + assert.throws(function () { xliffConv.parseJSON(1, {}, function (output) {}); }, 'invalid argument'); + assert.throws(function () { xliffConv.parseJSON({}, 1, function (output) {}); }, 'invalid argument'); + assert.throws(function () { xliffConv.parseJSON({}, {}, 1); }, 'invalid argument'); + }); + + }); + + }; + + return XliffConvCustomTest; +})); diff --git a/test/test.js b/test/test.js index 49e6bca..fdb7bfa 100644 --- a/test/test.js +++ b/test/test.js @@ -8,6 +8,9 @@ var chai = require('chai'); var XliffConv = require('../xliff-conv'); var XliffConvBasicTest = require('./basic-test.js'); +var XliffConvCustomTest = require('./custom-test'); var basicTest = new XliffConvBasicTest(); +var customTest = new XliffConvCustomTest(); basicTest.run(XliffConv, chai); +customTest.run(XliffConv, chai); diff --git a/xliff-conv.js b/xliff-conv.js index f849bff..f0b2690 100755 --- a/xliff-conv.js +++ b/xliff-conv.js @@ -678,12 +678,9 @@ Copyright (c) 2016, Tetsuya Mori . All rights reserved. // --Customise id and add new attribute -- if (options.addNewAttr) { - transUnit.setAttribute('id', id); - var newAttrArr = Object.getOwnPropertyNames(options.addNewAttr); - for (var item in newAttrArr) { - var valueOfNewAttr = options.addNewAttr[newAttrArr[item]]; - transUnit.setAttribute(newAttrArr[item], valueOfNewAttr[id]); - } + var newAttrName = Object.getOwnPropertyNames(options.addNewAttr); + transUnit.setAttribute('id', options.addNewAttr[newAttrName][id]); + transUnit.setAttribute(newAttrName, id); } // ---End---