i18next-conv
Advanced tools
Comparing version
@@ -36,2 +36,4 @@ function regexIndexOf(value, regex, startpos) { | ||
var isPlural = pluralIndex > -1; | ||
if (options.ignorePlurals) | ||
isPlural = false; | ||
var number; | ||
@@ -38,0 +40,0 @@ if (isPlural && key.indexOf('_plural') < 0) { |
var Gettext = require("node-gettext") | ||
, GettextParser = require("gettext-parser") | ||
, plurals = require("./plurals") | ||
@@ -9,4 +10,2 @@ , flatten = require("./flatten") | ||
var gt = new Gettext(); | ||
module.exports = { | ||
@@ -23,3 +22,3 @@ | ||
if (ext === '.mo' || ext === '.po') { | ||
if (ext === '.mo' || ext === '.po' || ext === '.pot') { | ||
return this.gettextToI18next(domain, source, target, options, callback); | ||
@@ -73,3 +72,3 @@ } | ||
self.i18nextToGettextData(domain, source, target, options, function(err, data) { | ||
self.writeFile(target, data, options, callback); | ||
self.writeFile(target, data + '\n', options, callback); | ||
}); | ||
@@ -87,8 +86,32 @@ }, | ||
self.flattenI18nextJSON(source, options, function(err, flat) { | ||
self.parseGettext(domain, flat, options, function(err, gt) { | ||
var data = path.extname(target) === '.po' ? gt.compilePO(domain) : gt.compileMO(domain); | ||
var f = function(err, data) { | ||
var res = (path.extname(target) === '.po' || path.extname(target) === '.pot') ? GettextParser.po.compile(data) : GettextParser.mo.compile(data); | ||
if (typeof callback === 'function') { | ||
callback(err, data); | ||
callback(err, res); | ||
} | ||
}); | ||
}; | ||
if (options.base) { | ||
self.flattenI18nextJSON(options.base, options, function(err, bflat) { | ||
Object.keys(bflat).forEach(function(key) { | ||
if (flat[key]) { | ||
if (flat[key].plurals) { | ||
var ext = plurals.rules[domain.replace('_', '-').split('-')[0]]; | ||
var pArray = []; | ||
for (var i = 0, len = flat[key].plurals.length; i < len; i++) { | ||
var plural = flat[key].plurals[i]; | ||
pArray.splice(self.getGettextPluralPosition(ext, plural.pluralNumber), 0, plural.value); | ||
} | ||
pArray.splice(self.getGettextPluralPosition(ext, flat[key].pluralNumber), 0, flat[key].value); | ||
bflat[key].translated_value = pArray; | ||
} else { | ||
bflat[key].translated_value = flat[key].value; | ||
} | ||
} | ||
}); | ||
self.parseGettext(domain, bflat, options, f); | ||
}); | ||
} else { | ||
self.parseGettext(domain, flat, options, f); | ||
} | ||
}); | ||
@@ -122,9 +145,27 @@ }, | ||
parseGettext: function(domain, data, options, callback) { | ||
var gt = new Gettext(); | ||
gt.addTextdomain(domain, ''); | ||
var out = { | ||
"charset": "utf-8", | ||
"headers": { | ||
"project-id-version": options.project || "i18next-conv", | ||
"mime-version": "1.0", | ||
"content-type": "text/plain; charset=utf-8", | ||
"content-transfer-encoding": "8bit", | ||
}, | ||
"translations": {} | ||
}; | ||
var ext = plurals.rules[domain.replace('_', '-').split('-')[0]]; | ||
var trans = {}; | ||
var plural_func = '' + ext.plurals; | ||
out['headers']['plural-forms'] = 'nplurals=' + ext.numbers.length + '; plural=' + plural_func.replace(/^function \(n\) \{ return Number/, '').replace(/; }$/, ''); | ||
if (!options.noDate) { | ||
out['headers']['pot-creation-date'] = new Date().toISOString(); | ||
out['headers']['po-revision-date'] = new Date().toISOString(); | ||
} | ||
if (options.language) | ||
out['headers']['language'] = options.language; | ||
if (!options.quiet) console.log('\n <-> parsing data to gettext format'.cyan); | ||
var delkeys = []; | ||
for (var m in data) { | ||
@@ -142,9 +183,46 @@ var kv = data[m]; | ||
gt.setTranslation(domain, kv.context, kv.key, pArray); | ||
if (typeof trans[kv.context] !== "object") trans[kv.context] = {} | ||
if (options.keyasareference) { | ||
if (typeof trans[kv.context][kv.value] === "object") { | ||
// same context and msgid. this could theorically be merged. | ||
trans[kv.context][kv.value].comments.reference.push(kv.key); | ||
} else { | ||
trans[kv.context][kv.value] = {"msgctxt": kv.context, "msgid": pArray[0], "msgid_plural": pArray.slice(1, pArray.length), "msgstr": kv.translated_value, "comments": {reference: [kv.key]}}; | ||
} | ||
if (kv.key !== kv.value) | ||
delkeys.push([kv.context, kv.key]); | ||
} else { | ||
trans[kv.context][kv.key] = {"msgctxt": kv.context, "msgid": kv.key, "msgid_plural": kv.key, "msgstr": pArray}; | ||
} | ||
} else { | ||
gt.setTranslation(domain, kv.context, kv.key, kv.value); | ||
if (typeof trans[kv.context] !== "object") trans[kv.context] = {} | ||
if (options.keyasareference) { | ||
if (typeof trans[kv.context][kv.value] === "object") { | ||
// same context and msgid. this could theorically be merged. | ||
trans[kv.context][kv.value].comments.reference.push(kv.key); | ||
} else { | ||
trans[kv.context][kv.value] = {"msgctxt": kv.context, "msgid": kv.value, "msgstr": kv.translated_value, "comments": {reference: [kv.key]}}; | ||
} | ||
if (kv.key != kv.value) | ||
delkeys.push([kv.context, kv.key]); | ||
} else { | ||
trans[kv.context][kv.key] = {"msgctxt": kv.context, "msgid": kv.key, "msgstr": kv.value}; | ||
} | ||
} | ||
} | ||
delkeys.forEach(function(a) { | ||
var c = a[0]; | ||
var k = a[1]; | ||
delete trans[c][k]; | ||
}); | ||
// re-format reference comments to be able to compile with gettext-parser... | ||
Object.keys(trans).forEach(function(ctxt) { | ||
Object.keys(trans[ctxt]).forEach(function(id) { | ||
if (trans[ctxt][id].comments && trans[ctxt][id].comments.reference) | ||
trans[ctxt][id].comments.reference = trans[ctxt][id].comments.reference.join("\n"); | ||
}); | ||
}); | ||
out["translations"] = trans; | ||
if (typeof callback === 'function') { | ||
callback(null, gt); | ||
callback(null, out); | ||
} | ||
@@ -224,2 +302,25 @@ }, | ||
self.addTextDomain(domain, source, options, function(err, data) { | ||
if (options.keyasareference) { | ||
var keys = []; | ||
Object.keys(data).forEach(function(ctxt) { | ||
Object.keys(data[ctxt]).forEach(function(key) { | ||
if (data[ctxt][key].comments && data[ctxt][key].comments.reference) { | ||
data[ctxt][key].comments.reference.split(/\r?\n|\r/).forEach(function(id) { | ||
var x = data[ctxt][key]; | ||
data[ctxt][id] = x; | ||
x.msgid = id; | ||
if (id !== key) | ||
keys.push([ctxt, key]); | ||
}); | ||
} | ||
}); | ||
}); | ||
keys.forEach(function(a) { | ||
var c = a[0]; | ||
var k = a[1]; | ||
delete data[c][k]; | ||
}); | ||
} | ||
self.parseJSON(domain, data, options, function(err, json) { | ||
@@ -238,2 +339,4 @@ var jsonData = JSON.stringify(json, null, 4); | ||
addTextDomain: function(domain, source, options, callback) { | ||
var gt = new Gettext(); | ||
if (!options.quiet) console.log(('\n --> reading file from: ' + source)); | ||
@@ -250,3 +353,4 @@ | ||
gt.addTextdomain(domain, body); | ||
if (body.length > 0) | ||
gt.addTextdomain(domain, body); | ||
@@ -257,3 +361,3 @@ if (options.filter) { | ||
if (typeof callback === 'function'){ | ||
callback(null, gt._domains[domain]._translationTable); | ||
callback(null, gt.domains[gt._normalizeDomain(domain)] && gt.domains[gt._normalizeDomain(domain)].translations); | ||
} | ||
@@ -286,2 +390,8 @@ } | ||
if (key.length === 0) { | ||
// delete if msgid is empty. | ||
// this might be the header. | ||
delete context[key] | ||
continue; | ||
} | ||
if (key.indexOf(separator) > -1) { | ||
@@ -302,3 +412,3 @@ var keys = key.split(separator); | ||
var values = context[key]; | ||
var values = context[key].msgstr; | ||
@@ -305,0 +415,0 @@ if (m !== '') targetKey = targetKey + '_' + m; |
@@ -9,3 +9,3 @@ { | ||
], | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"private": false, | ||
@@ -24,4 +24,5 @@ "preferGlobal": "true", | ||
"commander": "^2.5.0", | ||
"gettext-parser": "^1.1.2", | ||
"mkdirp": "^0.5.0", | ||
"node-gettext": "^0.2.14" | ||
"node-gettext": "^1.0.1" | ||
}, | ||
@@ -28,0 +29,0 @@ "devDependencies": { |
@@ -29,2 +29,4 @@ #!/usr/bin/env node | ||
.option('-t, --target [path]', 'Specify path to write to', '') | ||
.option('-b, --base [path]', 'Sepcify path for the base language file. only take effect with -K option', '') | ||
.option('-K, --keyasareference', 'Deal with the reference comment as a key', false) | ||
.option('-l, --language [domain]', 'Specify the language code, eg. \'en\'') | ||
@@ -36,2 +38,4 @@ .option('-ks, --keyseparator [path]', 'Specify keyseparator you want to use, defaults to ##', '##') | ||
.option('--splitNewLine', 'Silence output', false) | ||
.option('--ctxSeparator [sep]', 'Specify the context separator', '_') | ||
.option('--ignorePlurals', 'Do not process the plurals') | ||
.parse(process.argv); | ||
@@ -42,5 +46,10 @@ | ||
keyseparator: program.keyseparator, | ||
base: program.base, | ||
language: program.language, | ||
keyasareference: program.keyasareference, | ||
plurals: program.plurals, | ||
ignorePlurals: program.ignorePlurals, | ||
quiet: program.quiet, | ||
splitNewLine: program.splitNewLine | ||
ctxSeparator: program.ctxSeparator, | ||
splitNewLine: program.splitNewLine | ||
}; | ||
@@ -47,0 +56,0 @@ |
@@ -9,28 +9,32 @@ var fs = require('fs') | ||
var testFiles = { | ||
en: { | ||
utf8: './test/_testfiles/en/translation.utf8.po', | ||
utf8_expected: './test/_testfiles/en/translation.utf8.json', | ||
latin13: './test/_testfiles/en/translation.latin13.po', | ||
latin13_expected: './test/_testfiles/en/translation.latin13.json', | ||
unfiltered: './test/_testfiles/en/translation.unfiltered.po', | ||
unfiltered_no_comments: './test/_testfiles/en/translation.unfiltered_no_comments.po', | ||
unfiltered_no_match: './test/_testfiles/en/translation.unfiltered_no_match.po', | ||
filtered: './test/_testfiles/en/translation.filtered.json', | ||
filtered_no_comments: './test/_testfiles/en/translation.filtered_no_comments.json', | ||
empty: './test/_testfiles/en/translation.empty.po', | ||
missing: './test/_testfiles/en/translation.missing.po', | ||
bad_format: './test/_testfiles/en/translation.bad_format.po.js' | ||
}, | ||
en: { | ||
utf8: './test/_testfiles/en/translation.utf8.po', | ||
utf8_expected: './test/_testfiles/en/translation.utf8.json', | ||
utf8_msgid: './test/_testfiles/en/translation.utf8_msgid.po', | ||
utf8_msgid_expected: './test/_testfiles/en/translation.utf8_msgid.json', | ||
latin13: './test/_testfiles/en/translation.latin13.po', | ||
latin13_expected: './test/_testfiles/en/translation.latin13.json', | ||
unfiltered: './test/_testfiles/en/translation.unfiltered.po', | ||
unfiltered_no_comments: './test/_testfiles/en/translation.unfiltered_no_comments.po', | ||
unfiltered_no_match: './test/_testfiles/en/translation.unfiltered_no_match.po', | ||
filtered: './test/_testfiles/en/translation.filtered.json', | ||
filtered_no_comments: './test/_testfiles/en/translation.filtered_no_comments.json', | ||
empty: './test/_testfiles/en/translation.empty.po', | ||
missing: './test/_testfiles/en/translation.missing.po', | ||
bad_format: './test/_testfiles/en/translation.bad_format.po.js' | ||
}, | ||
de: { | ||
utf8: './test/_testfiles/de/translation.utf8.po', | ||
utf8_expected: './test/_testfiles/de/translation.utf8.json' | ||
}, | ||
de: { | ||
utf8: './test/_testfiles/de/translation.utf8.po', | ||
utf8_expected: './test/_testfiles/de/translation.utf8.json', | ||
utf8_msgid: './test/_testfiles/de/translation.utf8_msgid.po', | ||
utf8_msgid_expected: './test/_testfiles/de/translation.utf8_msgid.json', | ||
}, | ||
ru: { | ||
utf8: './test/_testfiles/ru/translation.utf8.po', | ||
utf8_expected: './test/_testfiles/ru/translation.utf8.json', | ||
utf8_2: './test/_testfiles/ru/translation2.utf8.po', | ||
utf8_2_expected: './test/_testfiles/ru/translation2.utf8.json' | ||
} | ||
ru: { | ||
utf8: './test/_testfiles/ru/translation.utf8.po', | ||
utf8_expected: './test/_testfiles/ru/translation.utf8.json', | ||
utf8_2: './test/_testfiles/ru/translation2.utf8.po', | ||
utf8_2_expected: './test/_testfiles/ru/translation2.utf8.json' | ||
} | ||
}; | ||
@@ -49,15 +53,18 @@ | ||
function _filter(gt, domain, callback) { | ||
var clientSideSource = '/frontend/'; | ||
var err; | ||
var clientSideSource = '/frontend/'; | ||
var err; | ||
var normalized_domain = gt._normalizeDomain(domain); | ||
gt.listKeys(domain).forEach(function(key) { | ||
var comment = gt.getComment(domain, '', key); | ||
if (comment) { | ||
if (comment.code && comment.code.indexOf(clientSideSource) === -1) { | ||
gt.deleteTranslation(domain, '', key); | ||
} | ||
Object.keys(gt.domains[normalized_domain].translations).forEach(function(ctxt) { | ||
Object.keys(gt.domains[normalized_domain].translations[ctxt]).forEach(function(key) { | ||
var comment = gt.getComment(domain, '', key); | ||
if (comment) { | ||
if (comment.reference && comment.reference.indexOf(clientSideSource) === -1) { | ||
delete gt.domains[normalized_domain].translations[ctxt][key]; | ||
} | ||
} | ||
}); | ||
}); | ||
callback(err, gt._domains[domain]._translationTable); | ||
callback(err, gt.domains[normalized_domain].translations); | ||
} | ||
@@ -67,210 +74,265 @@ | ||
describe('toJSON processing', function() { | ||
describe('toJSON processing', function() { | ||
it('should convert a utf8 PO files to JSON', function(done) { | ||
var tests = []; | ||
it('should convert a utf8 PO files to JSON', function(done) { | ||
var tests = []; | ||
// EN | ||
tests.push(function(next) { | ||
var output = './test/_tmp/en.utf8.json'; | ||
wrapper.gettextToI18next('en', testFiles.en.utf8, output, {quiet: true}, function(){ | ||
var result = require(path.join('..', output)); | ||
var expected = require(path.join('..', testFiles.en.utf8_expected)); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
// EN | ||
tests.push(function(next) { | ||
var output = './test/_tmp/en.utf8.json'; | ||
wrapper.gettextToI18next('en', testFiles.en.utf8, output, {quiet: true}, function(){ | ||
var result = require(path.join('..', output)); | ||
var expected = require(path.join('..', testFiles.en.utf8_expected)); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
// EN, using locale en_us | ||
tests.push(function(next) { | ||
var output = './test/_tmp/en_us.utf8.json'; | ||
wrapper.gettextToI18next('en_us', testFiles.en.utf8, output, {quiet: true, splitNewLine: true}, function(){ | ||
var result = require(path.join('..', output)); | ||
var expected = require(path.join('..', testFiles.en.utf8_expected)); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
// EN, using locale en_us | ||
tests.push(function(next) { | ||
var output = './test/_tmp/en_us.utf8.json'; | ||
wrapper.gettextToI18next('en_us', testFiles.en.utf8, output, {quiet: true, splitNewLine: true}, function(){ | ||
var result = require(path.join('..', output)); | ||
var expected = require(path.join('..', testFiles.en.utf8_expected)); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
// DE | ||
tests.push(function(next) { | ||
var output = './test/_tmp/de.utf8.json'; | ||
wrapper.gettextToI18next('de', testFiles.de.utf8, output, {quiet: true, splitNewLine: true}, function(){ | ||
var result = require(path.join('..', output)); | ||
var expected = require(path.join('..', testFiles.de.utf8_expected)); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
// DE | ||
tests.push(function(next) { | ||
var output = './test/_tmp/de.utf8.json'; | ||
wrapper.gettextToI18next('de', testFiles.de.utf8, output, {quiet: true, splitNewLine: true}, function(){ | ||
var result = require(path.join('..', output)); | ||
var expected = require(path.join('..', testFiles.de.utf8_expected)); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
// RU | ||
tests.push(function(next) { | ||
var output = './test/_tmp/ru.utf8.json'; | ||
wrapper.gettextToI18next('ru', testFiles.ru.utf8, output, {quiet: true, splitNewLine: true}, function(){ | ||
var result = require(path.join('..', output)); | ||
var expected = require(path.join('..', testFiles.ru.utf8_expected)); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
async.series(tests, done); | ||
// RU | ||
tests.push(function(next) { | ||
var output = './test/_tmp/ru.utf8.json'; | ||
wrapper.gettextToI18next('ru', testFiles.ru.utf8, output, {quiet: true, splitNewLine: true}, function(){ | ||
var result = require(path.join('..', output)); | ||
var expected = require(path.join('..', testFiles.ru.utf8_expected)); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
it('should convert a latin13 PO files to JSON, for a given domain', function(next) { | ||
var output = './test/_tmp/en.latin13.json'; | ||
async.series(tests, done); | ||
}); | ||
// EN | ||
wrapper.gettextToI18next('en', testFiles.en.latin13, output, {quiet: true, splitNewLine: true}, function(){ | ||
var result = require(path.join('..', output)); | ||
var expected = require(path.join('..', testFiles.en.latin13_expected)); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
it('should convert a latin13 PO files to JSON, for a given domain', function(next) { | ||
var output = './test/_tmp/en.latin13.json'; | ||
it('should filter incoming PO translations if a filter function is passed to options', function(next) { | ||
var output = './test/_tmp/translation.filtered.json'; | ||
// EN | ||
wrapper.gettextToI18next('en', testFiles.en.latin13, output, {quiet: true, splitNewLine: true}, function(){ | ||
var result = require(path.join('..', output)); | ||
var expected = require(path.join('..', testFiles.en.latin13_expected)); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
var options = { | ||
quiet: true, | ||
filter: _filter | ||
}; | ||
it('should filter incoming PO translations if a filter function is passed to options', function(next) { | ||
var output = './test/_tmp/translation.filtered.json'; | ||
// Should filter all but the col* keys | ||
wrapper.gettextToI18next('en', testFiles.en.unfiltered, output, options, function(){ | ||
var result = require(path.join('..', output)); | ||
var expected = require(path.join('..', testFiles.en.filtered)); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
var options = { | ||
quiet: true, | ||
filter: _filter | ||
}; | ||
it('should pass all keys unfiltered, when the PO has no comments', function(next) { | ||
var output = './test/_tmp/translation.nocomments.json'; | ||
// Should filter all but the col* keys | ||
wrapper.gettextToI18next('en', testFiles.en.unfiltered, output, options, function(){ | ||
var result = require(path.join('..', output)); | ||
var expected = require(path.join('..', testFiles.en.filtered)); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
var options = { | ||
quiet: true, | ||
splitNewLine: true, | ||
filter: _filter | ||
}; | ||
it('should pass all keys unfiltered, when the PO has no comments', function(next) { | ||
var output = './test/_tmp/translation.nocomments.json'; | ||
// Should filter none of the keys | ||
wrapper.gettextToI18next('en', testFiles.en.unfiltered_no_comments, output, options, function(){ | ||
var result = require(path.join('..', output)); | ||
var expected = require(path.join('..', testFiles.en.filtered_no_comments)); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
var options = { | ||
quiet: true, | ||
splitNewLine: true, | ||
filter: _filter | ||
}; | ||
it('should return an empty JSON file if nothing matches the given filter', function(next) { | ||
var output = './test/_tmp/translation.nomatch.json'; | ||
// Should filter none of the keys | ||
wrapper.gettextToI18next('en', testFiles.en.unfiltered_no_comments, output, options, function(){ | ||
var result = require(path.join('..', output)); | ||
var expected = require(path.join('..', testFiles.en.filtered_no_comments)); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
var options = { | ||
quiet: true, | ||
splitNewLine: true, | ||
filter: _filter | ||
}; | ||
it('should return an empty JSON file if nothing matches the given filter', function(next) { | ||
var output = './test/_tmp/translation.nomatch.json'; | ||
// Should filter all the keys | ||
wrapper.gettextToI18next('en', testFiles.en.unfiltered_no_match, output, options, function(){ | ||
var result = require(path.join('..', output)); | ||
expect(result).to.deep.equal({}); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
var options = { | ||
quiet: true, | ||
splitNewLine: true, | ||
filter: _filter | ||
}; | ||
// Should filter all the keys | ||
wrapper.gettextToI18next('en', testFiles.en.unfiltered_no_match, output, options, function(){ | ||
var result = require(path.join('..', output)); | ||
expect(result).to.deep.equal({}); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
it('should convert a utf8 PO file with msgid as an original string to a JSON file', function(done) { | ||
var tests = []; | ||
// EN | ||
tests.push(function(next) { | ||
var output = './test/_tmp/en.utf8_msgid.json'; | ||
wrapper.gettextToI18next('en', testFiles.en.utf8_msgid, output, {quiet: true, splitNewLine: true, keyasareference: true}, function() { | ||
var result = require(path.join('..', output)); | ||
var expected = require(path.join('..', testFiles.en.utf8_msgid_expected)); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
// DE | ||
tests.push(function(next) { | ||
var output = './test/_tmp/de.utf8_msgid.json'; | ||
wrapper.gettextToI18next('de', testFiles.de.utf8_msgid, output, {quiet: true, splitNewLine: true, keyasareference: true}, function() { | ||
var result = require(path.join('..', output)); | ||
var expected = require(path.join('..', testFiles.de.utf8_msgid_expected)); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
async.series(tests, done); | ||
}); | ||
// -- Error States & Invalid Data -- | ||
// -- Error States & Invalid Data -- | ||
describe('error states', function() { | ||
describe('error states', function() { | ||
it('should output an empty JSON file if the given PO does not exist', function(next) { | ||
var output = './test/_tmp/translation.missing.json'; | ||
it('should output an empty JSON file if the given PO does not exist', function(next) { | ||
var output = './test/_tmp/translation.missing.json'; | ||
wrapper.gettextToI18next('en', testFiles.en.missing, output, {quiet: true, splitNewLine: true}, function(){ | ||
var result = require(path.join('..', output)); | ||
expect(result).to.deep.equal({}); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
wrapper.gettextToI18next('en', testFiles.en.missing, output, {quiet: true, splitNewLine: true}, function(){ | ||
var result = require(path.join('..', output)); | ||
expect(result).to.deep.equal({}); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
it('should output an empty JSON file if the given PO exists but is empty', function(next) { | ||
var output = './test/_tmp/translation.empty.json'; | ||
it('should output an empty JSON file if the given PO exists but is empty', function(next) { | ||
var output = './test/_tmp/translation.empty.json'; | ||
wrapper.gettextToI18next('en', testFiles.en.empty, output, {quiet: true, splitNewLine: true}, function(){ | ||
var result = require(path.join('..', output)); | ||
expect(result).to.deep.equal({}); | ||
wrapper.gettextToI18next('en', testFiles.en.empty, output, {quiet: true, splitNewLine: true}, function(){ | ||
var result = require(path.join('..', output)); | ||
expect(result).to.deep.equal({}); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
it('should output an empty JSON file if passed something other than a PO', function(next) { | ||
var output = './test/_tmp/translation.bad_format.json'; | ||
it('should output an empty JSON file if passed something other than a PO', function(next) { | ||
var output = './test/_tmp/translation.bad_format.json'; | ||
wrapper.gettextToI18next('en', testFiles.en.bad_format, output, {quiet: true, splitNewLine: true}, function(){ | ||
var result = require(path.join('..', output)); | ||
expect(result).to.deep.equal({}); | ||
wrapper.gettextToI18next('en', testFiles.en.bad_format, output, {quiet: true, splitNewLine: true}, function(){ | ||
var result = require(path.join('..', output)); | ||
expect(result).to.deep.equal({}); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('toPO processing', function() { | ||
it('should convert a JSON file to utf8 PO', function(done) { | ||
var tests = []; | ||
describe('toPO processing', function() { | ||
it('should convert a JSON file to utf8 PO', function(done) { | ||
var tests = []; | ||
// EN | ||
tests.push(function(next) { | ||
var output = './test/_tmp/en.utf8.po'; | ||
wrapper.i18nextToGettext('en', testFiles.en.utf8_expected, output, {quiet: true, splitNewLine: true}, function(){ | ||
var result = fs.readFileSync(output); | ||
var expected = fs.readFileSync(testFiles.en.utf8); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
// EN | ||
tests.push(function(next) { | ||
var output = './test/_tmp/en.utf8.po'; | ||
wrapper.i18nextToGettext('en', testFiles.en.utf8_expected, output, {quiet: true, splitNewLine: true, noDate: true}, function(){ | ||
var result = fs.readFileSync(output); | ||
var expected = fs.readFileSync(testFiles.en.utf8); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
// DE | ||
tests.push(function(next) { | ||
var output = './test/_tmp/de.utf8.po'; | ||
wrapper.i18nextToGettext('de', testFiles.de.utf8_expected, output, {quiet: true, splitNewLine: true}, function(){ | ||
var result = fs.readFileSync(output); | ||
var expected = fs.readFileSync(testFiles.de.utf8); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
// DE | ||
tests.push(function(next) { | ||
var output = './test/_tmp/de.utf8.po'; | ||
wrapper.i18nextToGettext('de', testFiles.de.utf8_expected, output, {quiet: true, splitNewLine: true, noDate: true}, function(){ | ||
var result = fs.readFileSync(output); | ||
var expected = fs.readFileSync(testFiles.de.utf8); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
// RU | ||
tests.push(function(next) { | ||
var output = './test/_tmp/ru.utf8.po'; | ||
wrapper.i18nextToGettext('ru', testFiles.ru.utf8_2_expected, output, {quiet: true, splitNewLine: true}, function(){ | ||
var result = fs.readFileSync(output); | ||
var expected = fs.readFileSync(testFiles.ru.utf8_2); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
// RU | ||
tests.push(function(next) { | ||
var output = './test/_tmp/ru.utf8.po'; | ||
wrapper.i18nextToGettext('ru', testFiles.ru.utf8_2_expected, output, {quiet: true, splitNewLine: true, noDate: true}, function(){ | ||
var result = fs.readFileSync(output); | ||
var expected = fs.readFileSync(testFiles.ru.utf8_2); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
async.series(tests, done); | ||
async.series(tests, done); | ||
}); | ||
it('should convert a JSON file to utf8 PO with msgid as an original string', function(done) { | ||
var tests = []; | ||
// EN | ||
tests.push(function(next) { | ||
var output = './test/_tmp/en.utf8.po'; | ||
wrapper.i18nextToGettext('en', testFiles.en.utf8_msgid_expected, output, {quiet: true, splitNewLine: true, noDate: true, base: testFiles.en.utf8_msgid_expected, keyasareference: true}, function(){ | ||
var result = fs.readFileSync(output); | ||
var expected = fs.readFileSync(testFiles.en.utf8_msgid); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}) | ||
}); | ||
// DE | ||
tests.push(function(next) { | ||
var output = './test/_tmp/de.utf8.po'; | ||
wrapper.i18nextToGettext('de', testFiles.de.utf8_msgid_expected, output, {quiet: true, splitNewLine: true, noDate: true, base: testFiles.en.utf8_msgid_expected, keyasareference: true}, function(){ | ||
var result = fs.readFileSync(output); | ||
var expected = fs.readFileSync(testFiles.de.utf8_msgid); | ||
expect(result).to.deep.equal(expected); | ||
fs.unlinkSync(output); | ||
next(); | ||
}); | ||
}); | ||
async.series(tests, done); | ||
}); | ||
}) | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
123163
9.05%36
12.5%2938
7.58%5
25%27
17.39%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
Updated