Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
fetchOne: function(lng, ns, callback) {
var source = this.functions.applyReplacement(this.options.resGetPath, {lng: lng, ns: ns});
var self = this;
converter.addTextDomain(lng, source, {}, function(err, data) {
// console.log(data);
if (err) {
callback(err);
return;
}
converter.parseJSON(lng, data, {}, function(err, json) {
if (err) {
callback(err);
} else {
self.functions.log('loaded file: ' + source);
callback(null, json);
}
});
});
},
converter.addTextDomain(lng, source, {}, function(err, data) {
// console.log(data);
if (err) {
callback(err);
return;
}
converter.parseJSON(lng, data, {}, function(err, json) {
if (err) {
callback(err);
} else {
self.functions.log('loaded file: ' + source);
callback(null, json);
}
});
});
},
module.exports = function (source) {
this.cacheable && this.cacheable()
var callback = this.async()
// Parse the language out of the file
var language = source.match(/Language: ([\w]*)/)
language = language ? language[1] : 'en'
// Convert to i18next format (already stringified)
converter.gettextToI18next(language, source, options).then(function (data) {
callback(null, "module.exports = " + data + ";")
})
}
let translationId = path.join(
lang,
path.basename(id, '.po')
);
/**
* Translation asset data.
* @type {TranslationAsset}
*/
let translation = this.translations[translationId] = this.translations[translationId] || {
asset: this.emitAsset(`${translationId}.json`),
data: {},
};
// convert .po file into JSON
let json = await gettextToI18next(lang, fs.readFileSync(id));
// merge with previous data
translation.data = Object.assign({}, translation.data, JSON.parse(json));
// just export the asset url
return `export default import.meta.ROLLUP_ASSET_URL_${translation.asset};`;
},
const convertToFlatFormat = (opt, data, lng, cb) => {
if (!cb) {
cb = lng;
lng = undefined;
}
try {
if (opt.format === 'json' || opt.format === 'flat') {
cb(null, flatten(JSON.parse(data.toString())));
return;
}
if (opt.format === 'po' || opt.format === 'gettext') {
try {
gettextToI18next(opt.referenceLanguage, data.toString(), {
persistMsgIdPlural: true,
ignoreCtx: true
}).then((ret) => {
try {
cb(null, flatten(JSON.parse(ret.toString())));
} catch (err) {
cb(err);
}
}, cb);
} catch (err) {
cb(err);
}
return;
}
if (opt.format === 'po_i18next' || opt.format === 'gettext_i18next') {
try {
if (opt.format === 'flat') {
cb(null, JSON.stringify(flatten(data), null, 2));
return;
}
if (opt.format === 'po' || opt.format === 'gettext') {
const flatData = flatten(data);
const gettextOpt = {
project: 'locize',
language: lng,
potCreationDate: lastModified,
poRevisionDate: lastModified,
ctxSeparator: '_ is default but we set it to something that is never found!!!',
persistMsgIdPlural: true
};
i18nextToPo(lng, JSON.stringify(flatData), gettextOpt).then((ret) => {
cb(null, ret.toString());
}, cb);
return;
}
if (opt.format === 'po_i18next' || opt.format === 'gettext_i18next') {
i18nextToPo(lng, JSON.stringify(flatten(data)), {
project: 'locize',
language: lng,
potCreationDate: lastModified,
poRevisionDate: lastModified
}).then((ret) => {
cb(null, ret.toString());
}, cb);
return;
}
if (opt.format === 'csv') {
const gettextOpt = {
project: 'locize',
language: lng,
potCreationDate: lastModified,
poRevisionDate: lastModified,
ctxSeparator: '_ is default but we set it to something that is never found!!!',
persistMsgIdPlural: true
};
i18nextToPo(lng, JSON.stringify(flatData), gettextOpt).then((ret) => {
cb(null, ret.toString());
}, cb);
return;
}
if (opt.format === 'po_i18next' || opt.format === 'gettext_i18next') {
i18nextToPo(lng, JSON.stringify(flatten(data)), {
project: 'locize',
language: lng,
potCreationDate: lastModified,
poRevisionDate: lastModified
}).then((ret) => {
cb(null, ret.toString());
}, cb);
return;
}
if (opt.format === 'csv') {
opt.getNamespace(opt, opt.referenceLanguage, namespace, (err, refNs) => {
if (err) return cb(err);
const js2CsvData = Object.keys(flatten(data)).reduce((mem, k) => {
const value = data[k] || '';
const line = {