|
|
|
@ -2,6 +2,70 @@ const typescriptTransform = require('i18next-scanner-typescript');
|
|
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
const chalk = require('chalk');
|
|
|
|
|
const eol = require('eol');
|
|
|
|
|
const path = require('path');
|
|
|
|
|
const VirtualFile = require('vinyl');
|
|
|
|
|
|
|
|
|
|
function flush(done) {
|
|
|
|
|
const { parser } = this;
|
|
|
|
|
const { options } = parser;
|
|
|
|
|
|
|
|
|
|
// Flush to resource store
|
|
|
|
|
const resStore = parser.get({ sort: options.sort });
|
|
|
|
|
const { jsonIndent } = options.resource;
|
|
|
|
|
const lineEnding = String(options.resource.lineEnding).toLowerCase();
|
|
|
|
|
|
|
|
|
|
Object.keys(resStore).forEach((lng) => {
|
|
|
|
|
const namespaces = resStore[lng];
|
|
|
|
|
|
|
|
|
|
Object.keys(namespaces).forEach((ns) => {
|
|
|
|
|
const resPath = parser.formatResourceSavePath(lng, ns);
|
|
|
|
|
let resContent;
|
|
|
|
|
try {
|
|
|
|
|
resContent = JSON.parse(
|
|
|
|
|
fs.readFileSync(
|
|
|
|
|
fs.realpathSync(path.join('public', 'locales', resPath))
|
|
|
|
|
).toString('utf-8')
|
|
|
|
|
);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log("no previous translation found!")
|
|
|
|
|
resContent = {};
|
|
|
|
|
}
|
|
|
|
|
const obj = { ...namespaces[ns], ...resContent };
|
|
|
|
|
let text = JSON.stringify(obj, null, jsonIndent) + '\n';
|
|
|
|
|
|
|
|
|
|
if (lineEnding === 'auto') {
|
|
|
|
|
text = eol.auto(text);
|
|
|
|
|
} else if (lineEnding === '\r\n' || lineEnding === 'crlf') {
|
|
|
|
|
text = eol.crlf(text);
|
|
|
|
|
} else if (lineEnding === '\n' || lineEnding === 'lf') {
|
|
|
|
|
text = eol.lf(text);
|
|
|
|
|
} else if (lineEnding === '\r' || lineEnding === 'cr') {
|
|
|
|
|
text = eol.cr(text);
|
|
|
|
|
} else { // Defaults to LF
|
|
|
|
|
text = eol.lf(text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let contents = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// "Buffer.from(string[, encoding])" is added in Node.js v5.10.0
|
|
|
|
|
contents = Buffer.from(text);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// Fallback to "new Buffer(string[, encoding])" which is deprecated since Node.js v6.0.0
|
|
|
|
|
contents = new Buffer(text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.push(new VirtualFile({
|
|
|
|
|
path: resPath,
|
|
|
|
|
contents: contents
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
input: [
|
|
|
|
@ -10,7 +74,6 @@ module.exports = {
|
|
|
|
|
'!client/i18n/**',
|
|
|
|
|
'!**/node_modules/**',
|
|
|
|
|
],
|
|
|
|
|
output: './client/public/locales',
|
|
|
|
|
options: {
|
|
|
|
|
debug: true,
|
|
|
|
|
func: {
|
|
|
|
@ -45,8 +108,8 @@ module.exports = {
|
|
|
|
|
return ''; // Return empty string for other languages
|
|
|
|
|
},
|
|
|
|
|
resource: {
|
|
|
|
|
loadPath: './{{lng}}/{{ns}}.json',
|
|
|
|
|
savePath: './{{lng}}/{{ns}}.json',
|
|
|
|
|
loadPath: './client/public/locales/{{lng}}/{{ns}}.json',
|
|
|
|
|
savePath: './client/public/locales/{{lng}}/{{ns}}.json',
|
|
|
|
|
jsonIndent: 2,
|
|
|
|
|
lineEnding: '\n'
|
|
|
|
|
},
|
|
|
|
|