gettext-converter
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -8,181 +8,11 @@ "use strict"; | ||
var _i18next2js = _interopRequireDefault(require("./i18next2js.js")); | ||
var _poCompiler = _interopRequireDefault(require("./poCompiler.js")); | ||
var _flatten = _interopRequireDefault(require("./flatten.js")); | ||
var _plurals = _interopRequireDefault(require("./plurals.js")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
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); } | ||
var getGettextPluralPosition = function getGettextPluralPosition(ext, suffix) { | ||
if (ext) { | ||
for (var i = 0; i < ext.nplurals; i += 1) { | ||
if (i === suffix) return i; | ||
} | ||
} | ||
return -1; | ||
}; | ||
var getPluralArray = function getPluralArray(locale, translation) { | ||
var ext = _plurals.default[locale.toLowerCase()] || _plurals.default[locale.split(/_|-/)[0].toLowerCase()] || _plurals.default.dev; | ||
var pArray = []; | ||
for (var i = 0, len = translation.plurals.length; i < len; i += 1) { | ||
var plural = translation.plurals[i]; | ||
pArray.splice(getGettextPluralPosition(ext, plural.pluralNumber - 1), 0, plural.value); | ||
} | ||
pArray.splice(getGettextPluralPosition(ext, translation.pluralNumber - 1), 0, translation.value); | ||
return pArray; | ||
}; | ||
var parseGettext = function parseGettext(locale, data) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var out = { | ||
charset: 'utf-8', | ||
headers: { | ||
'project-id-version': options.project || 'gettext-converter', | ||
'mime-version': '1.0', | ||
'content-type': 'text/plain; charset=utf-8', | ||
'content-transfer-encoding': '8bit' | ||
}, | ||
translations: {} | ||
}; | ||
var ext = _plurals.default[locale.toLowerCase()] || _plurals.default[locale.split(/_|-/)[0].toLowerCase()] || _plurals.default.dev; | ||
var trans = {}; | ||
out.headers['plural-forms'] = "nplurals=".concat(ext.numbers.length, "; plural=").concat(ext.plurals); | ||
if (!options.noDate) { | ||
out.headers['pot-creation-date'] = new Date().toISOString(); | ||
out.headers['po-revision-date'] = new Date().toISOString(); | ||
if (options.potCreationDate && typeof options.potCreationDate.toISOString === 'function') out.headers['pot-creation-date'] = options.potCreationDate.toISOString(); | ||
if (options.poRevisionDate && typeof options.poRevisionDate.toISOString === 'function') out.headers['po-revision-date'] = options.poRevisionDate.toISOString(); | ||
} | ||
out.headers.language = locale; | ||
if (options.language) out.headers.language = options.language; | ||
var delkeys = []; | ||
Object.keys(data).forEach(function (m) { | ||
var kv = data[m]; | ||
if (kv.plurals) { | ||
var pArray = []; | ||
for (var i = 0, len = kv.plurals.length; i < len; i += 1) { | ||
var plural = kv.plurals[i]; | ||
pArray.splice(getGettextPluralPosition(ext, plural.pluralNumber - 1), 0, plural.value); | ||
} | ||
if (ext.numbers.length !== 1) { | ||
pArray.splice(getGettextPluralPosition(ext, kv.pluralNumber - 1), 0, kv.value); | ||
} | ||
if (_typeof(trans[kv.context]) !== 'object') trans[kv.context] = {}; | ||
if (options.keyasareference) { | ||
if (_typeof(trans[kv.context][kv.value]) === 'object') { | ||
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 { | ||
var msgid = kv.key; | ||
var msgid_plural = kv.key; | ||
if (kv.key.indexOf('|#|') > -1) { | ||
var p = kv.key.split('|#|'); | ||
msgid = p[0]; | ||
msgid_plural = p[1]; | ||
} | ||
trans[kv.context][kv.key] = { | ||
msgctxt: kv.context, | ||
msgid: msgid, | ||
msgid_plural: msgid_plural, | ||
msgstr: pArray | ||
}; | ||
} | ||
} else { | ||
if (_typeof(trans[kv.context]) !== 'object') trans[kv.context] = {}; | ||
if (options.keyasareference) { | ||
if (_typeof(trans[kv.context][kv.value]) === 'object') { | ||
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]; | ||
}); | ||
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; | ||
return out; | ||
}; | ||
function i18next2po(locale, body) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var data; | ||
var flat = (0, _flatten.default)(typeof body === 'string' ? JSON.parse(body) : body, options, locale); | ||
if (options.base) { | ||
var bflat = (0, _flatten.default)(JSON.parse(options.base), options); | ||
Object.keys(bflat).forEach(function (key) { | ||
if (flat[key]) { | ||
if (flat[key].plurals) { | ||
bflat[key].translated_value = getPluralArray(locale, flat[key]); | ||
} else { | ||
bflat[key].translated_value = flat[key].value; | ||
} | ||
} | ||
}); | ||
data = parseGettext(locale, bflat, options); | ||
} | ||
data = parseGettext(locale, flat, options); | ||
var data = (0, _i18next2js.default)(locale, body, options); | ||
return (0, _poCompiler.default)(data, options); | ||
@@ -189,0 +19,0 @@ } |
@@ -30,2 +30,8 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "i18next2js", { | ||
enumerable: true, | ||
get: function get() { | ||
return _i18next2js.default; | ||
} | ||
}); | ||
Object.defineProperty(exports, "i18next2po", { | ||
@@ -47,2 +53,4 @@ enumerable: true, | ||
var _i18next2js = _interopRequireDefault(require("./i18next2js.js")); | ||
var _i18next2po = _interopRequireDefault(require("./i18next2po.js")); | ||
@@ -57,4 +65,5 @@ | ||
po2i18next: _po2i18next.default, | ||
i18next2js: _i18next2js.default, | ||
i18next2po: _i18next2po.default | ||
}; | ||
exports.default = _default; |
@@ -1,174 +0,7 @@ | ||
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); } | ||
import i18next2js from './i18next2js.js'; | ||
import compile from './poCompiler.js'; | ||
import flatten from './flatten.js'; | ||
import plurals from './plurals.js'; | ||
var getGettextPluralPosition = function getGettextPluralPosition(ext, suffix) { | ||
if (ext) { | ||
for (var i = 0; i < ext.nplurals; i += 1) { | ||
if (i === suffix) return i; | ||
} | ||
} | ||
return -1; | ||
}; | ||
var getPluralArray = function getPluralArray(locale, translation) { | ||
var ext = plurals[locale.toLowerCase()] || plurals[locale.split(/_|-/)[0].toLowerCase()] || plurals.dev; | ||
var pArray = []; | ||
for (var i = 0, len = translation.plurals.length; i < len; i += 1) { | ||
var plural = translation.plurals[i]; | ||
pArray.splice(getGettextPluralPosition(ext, plural.pluralNumber - 1), 0, plural.value); | ||
} | ||
pArray.splice(getGettextPluralPosition(ext, translation.pluralNumber - 1), 0, translation.value); | ||
return pArray; | ||
}; | ||
var parseGettext = function parseGettext(locale, data) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var out = { | ||
charset: 'utf-8', | ||
headers: { | ||
'project-id-version': options.project || 'gettext-converter', | ||
'mime-version': '1.0', | ||
'content-type': 'text/plain; charset=utf-8', | ||
'content-transfer-encoding': '8bit' | ||
}, | ||
translations: {} | ||
}; | ||
var ext = plurals[locale.toLowerCase()] || plurals[locale.split(/_|-/)[0].toLowerCase()] || plurals.dev; | ||
var trans = {}; | ||
out.headers['plural-forms'] = "nplurals=".concat(ext.numbers.length, "; plural=").concat(ext.plurals); | ||
if (!options.noDate) { | ||
out.headers['pot-creation-date'] = new Date().toISOString(); | ||
out.headers['po-revision-date'] = new Date().toISOString(); | ||
if (options.potCreationDate && typeof options.potCreationDate.toISOString === 'function') out.headers['pot-creation-date'] = options.potCreationDate.toISOString(); | ||
if (options.poRevisionDate && typeof options.poRevisionDate.toISOString === 'function') out.headers['po-revision-date'] = options.poRevisionDate.toISOString(); | ||
} | ||
out.headers.language = locale; | ||
if (options.language) out.headers.language = options.language; | ||
var delkeys = []; | ||
Object.keys(data).forEach(function (m) { | ||
var kv = data[m]; | ||
if (kv.plurals) { | ||
var pArray = []; | ||
for (var i = 0, len = kv.plurals.length; i < len; i += 1) { | ||
var plural = kv.plurals[i]; | ||
pArray.splice(getGettextPluralPosition(ext, plural.pluralNumber - 1), 0, plural.value); | ||
} | ||
if (ext.numbers.length !== 1) { | ||
pArray.splice(getGettextPluralPosition(ext, kv.pluralNumber - 1), 0, kv.value); | ||
} | ||
if (_typeof(trans[kv.context]) !== 'object') trans[kv.context] = {}; | ||
if (options.keyasareference) { | ||
if (_typeof(trans[kv.context][kv.value]) === 'object') { | ||
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 { | ||
var msgid = kv.key; | ||
var msgid_plural = kv.key; | ||
if (kv.key.indexOf('|#|') > -1) { | ||
var p = kv.key.split('|#|'); | ||
msgid = p[0]; | ||
msgid_plural = p[1]; | ||
} | ||
trans[kv.context][kv.key] = { | ||
msgctxt: kv.context, | ||
msgid: msgid, | ||
msgid_plural: msgid_plural, | ||
msgstr: pArray | ||
}; | ||
} | ||
} else { | ||
if (_typeof(trans[kv.context]) !== 'object') trans[kv.context] = {}; | ||
if (options.keyasareference) { | ||
if (_typeof(trans[kv.context][kv.value]) === 'object') { | ||
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]; | ||
}); | ||
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; | ||
return out; | ||
}; | ||
export default function i18next2po(locale, body) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var data; | ||
var flat = flatten(typeof body === 'string' ? JSON.parse(body) : body, options, locale); | ||
if (options.base) { | ||
var bflat = flatten(JSON.parse(options.base), options); | ||
Object.keys(bflat).forEach(function (key) { | ||
if (flat[key]) { | ||
if (flat[key].plurals) { | ||
bflat[key].translated_value = getPluralArray(locale, flat[key]); | ||
} else { | ||
bflat[key].translated_value = flat[key].value; | ||
} | ||
} | ||
}); | ||
data = parseGettext(locale, bflat, options); | ||
} | ||
data = parseGettext(locale, flat, options); | ||
var data = i18next2js(locale, body, options); | ||
return compile(data, options); | ||
} |
@@ -5,4 +5,5 @@ import po2js from './po2js.js'; | ||
import po2i18next from './po2i18next.js'; | ||
import i18next2js from './i18next2js.js'; | ||
import i18next2po from './i18next2po.js'; | ||
export { po2js, js2po, js2i18next, po2i18next, i18next2po }; | ||
export { po2js, js2po, js2i18next, po2i18next, i18next2js, i18next2po }; | ||
export default { | ||
@@ -13,3 +14,4 @@ po2js: po2js, | ||
po2i18next: po2i18next, | ||
i18next2js: i18next2js, | ||
i18next2po: i18next2po | ||
}; |
// inspired by https://github.com/i18next/i18next-gettext-converter/blob/master/src/lib/json2gettext.js | ||
import i18next2js from './i18next2js.js' | ||
import compile from './poCompiler.js' | ||
import flatten from './flatten.js' | ||
import plurals from './plurals.js' | ||
const getGettextPluralPosition = (ext, suffix) => { | ||
if (ext) { | ||
for (let i = 0; i < ext.nplurals; i += 1) { | ||
if (i === suffix) return i | ||
} | ||
} | ||
return -1 | ||
} | ||
const getPluralArray = (locale, translation) => { | ||
const ext = plurals[locale.toLowerCase()] || plurals[locale.split(/_|-/)[0].toLowerCase()] || plurals.dev | ||
const pArray = [] | ||
for (let i = 0, len = translation.plurals.length; i < len; i += 1) { | ||
const plural = translation.plurals[i] | ||
pArray.splice(getGettextPluralPosition(ext, plural.pluralNumber - 1), 0, plural.value) | ||
} | ||
pArray.splice(getGettextPluralPosition(ext, translation.pluralNumber - 1), 0, translation.value) | ||
return pArray | ||
} | ||
const parseGettext = (locale, data, options = {}) => { | ||
const out = { | ||
charset: 'utf-8', | ||
headers: { | ||
'project-id-version': options.project || 'gettext-converter', | ||
'mime-version': '1.0', | ||
'content-type': 'text/plain; charset=utf-8', | ||
'content-transfer-encoding': '8bit' | ||
}, | ||
translations: {} | ||
} | ||
const ext = plurals[locale.toLowerCase()] || plurals[locale.split(/_|-/)[0].toLowerCase()] || plurals.dev | ||
const trans = {} | ||
out.headers['plural-forms'] = `nplurals=${ext.numbers.length}; plural=${ext.plurals}` | ||
if (!options.noDate) { | ||
out.headers['pot-creation-date'] = new Date().toISOString() | ||
out.headers['po-revision-date'] = new Date().toISOString() | ||
if (options.potCreationDate && typeof options.potCreationDate.toISOString === 'function') out.headers['pot-creation-date'] = options.potCreationDate.toISOString() | ||
if (options.poRevisionDate && typeof options.poRevisionDate.toISOString === 'function') out.headers['po-revision-date'] = options.poRevisionDate.toISOString() | ||
} | ||
out.headers.language = locale | ||
if (options.language) out.headers.language = options.language | ||
const delkeys = [] | ||
Object.keys(data).forEach((m) => { | ||
const kv = data[m] | ||
if (kv.plurals) { | ||
const pArray = [] | ||
for (let i = 0, len = kv.plurals.length; i < len; i += 1) { | ||
const plural = kv.plurals[i] | ||
pArray.splice(getGettextPluralPosition(ext, plural.pluralNumber - 1), 0, plural.value) | ||
} | ||
if (ext.numbers.length !== 1) { | ||
pArray.splice(getGettextPluralPosition(ext, kv.pluralNumber - 1), 0, 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: 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 { | ||
let msgid = kv.key // eslint-disable-next-line camelcase | ||
// eslint-disable-next-line camelcase | ||
let msgid_plural = kv.key | ||
if (kv.key.indexOf('|#|') > -1) { | ||
const p = kv.key.split('|#|') | ||
msgid = p[0] | ||
// eslint-disable-next-line camelcase | ||
msgid_plural = p[1] | ||
} | ||
trans[kv.context][kv.key] = { | ||
msgctxt: kv.context, | ||
msgid, | ||
msgid_plural, | ||
msgstr: pArray | ||
} | ||
} | ||
} else { | ||
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(a => { | ||
const c = a[0] | ||
const k = a[1] | ||
delete trans[c][k] | ||
}) // re-format reference comments to be able to compile with gettext-parser... | ||
Object.keys(trans).forEach(ctxt => { | ||
Object.keys(trans[ctxt]).forEach(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 | ||
return out | ||
} | ||
export default function i18next2po (locale, body, options = {}) { | ||
let data | ||
const flat = flatten(typeof body === 'string' ? JSON.parse(body) : body, options, locale) | ||
if (options.base) { | ||
const bflat = flatten(JSON.parse(options.base), options) | ||
Object.keys(bflat).forEach(key => { | ||
if (flat[key]) { | ||
if (flat[key].plurals) { | ||
bflat[key].translated_value = getPluralArray(locale, flat[key]) | ||
} else { | ||
bflat[key].translated_value = flat[key].value | ||
} | ||
} | ||
}) | ||
data = parseGettext(locale, bflat, options) | ||
} | ||
data = parseGettext(locale, flat, options) | ||
const data = i18next2js(locale, body, options) | ||
return compile(data, options) | ||
} |
@@ -5,4 +5,5 @@ import po2js from './po2js.js' | ||
import po2i18next from './po2i18next.js' | ||
import i18next2js from './i18next2js.js' | ||
import i18next2po from './i18next2po.js' | ||
export { po2js, js2po, js2i18next, po2i18next, i18next2po } | ||
export default { po2js, js2po, js2i18next, po2i18next, i18next2po } | ||
export { po2js, js2po, js2i18next, po2i18next, i18next2js, i18next2po } | ||
export default { po2js, js2po, js2i18next, po2i18next, i18next2js, i18next2po } |
{ | ||
"name": "gettext-converter", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "po2js and js2po and i18next2po and po2i18next converter gettext resource utils", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -106,9 +106,7 @@ [![travis](https://img.shields.io/travis/locize/gettext-converter.svg)](https://travis-ci.org/locize/gettext-converter) [![npm](https://img.shields.io/npm/v/gettext-converter.svg)](https://npmjs.org/package/gettext-converter) | ||
import i18next2po from 'gettext-converter/i18next2po' | ||
const res = i18next2po('en', i18nextJs, { compatibilityJSON: 'v4' }) | ||
import po2i18next from 'gettext-converter/po2i18next' | ||
const res = po2i18next(po, { compatibilityJSON: 'v4' }) | ||
// res is like i18nextJs | ||
import i18next2po from 'gettext-converter/i18next2po' | ||
const res = i18next2po('en', i18nextJs, { compatibilityJSON: 'v4' }) | ||
// res is like po | ||
``` |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
932753
45
12768
111