Update of extract.js to process translations in other languages.

This commit is contained in:
Tomas Bures 2019-05-13 00:35:19 +02:00
parent b1efb95315
commit 0e9d192b1f
3 changed files with 24 additions and 12 deletions

View file

@ -224,6 +224,7 @@ function parseJsxTrans(fragment) {
if (originalValue === undefined) {
value = convValue;
originalKey = undefined;
} else {
value = originalValue;
}
@ -232,20 +233,21 @@ function parseJsxTrans(fragment) {
const replacement = `${match[1] || ''}<Trans i18nKey="${key}">${jsxStr.substring(expr.openingElement.end, expr.closingElement.start)}</Trans>`;
return { key, originalKey, value, replacement, originalValue };
return { key, originalKey, value, replacement };
}
function parseHbsTranslate(fragment) {
const match = fragment.match(hbsTranslateMatcher);
const spec = parseSpec(match[2]);
const originalKey = match[4];
let originalKey = match[4];
let value;
const originalValue = findInDict(origResDict, originalKey);
if (originalValue === undefined) {
value = originalKey;
originalKey = undefined;
} else {
value = originalValue;
}
@ -260,7 +262,7 @@ function parseHbsTranslate(fragment) {
function parseT(fragment) {
const match = fragment.match(tMatcher);
const originalKey = match[5];
let originalKey = match[5];
const spec = parseSpec(match[3]);
if (spec.ignore) {
@ -272,6 +274,7 @@ function parseT(fragment) {
if (originalValue === undefined) {
value = originalKey;
originalKey = undefined;
} else {
value = originalValue;
}
@ -293,14 +296,17 @@ function processFile(file) {
for (const fragment of fragments) {
const parseStruct = parseFun(fragment);
if (parseStruct) {
const {key, originalKey, value, replacement, originalValue} = parseStruct;
const {key, originalKey, value, replacement} = parseStruct;
source = source.split(fragment).join(replacement);
setInDict(resDict, key, value);
let valueChanged = false;
if (originalKey === undefined || findInDict(prevResDict, originalKey) !== value) {
keysWithChangedValue.add(key);
anyUpdates = true;
}
const variants = originalKey ? findAllVariantsByPrefixInDict(origResDict, originalKey + '_') : [];
const variants = originalKey !== undefined ? findAllVariantsByPrefixInDict(origResDict, originalKey + '_') : [];
for (const variant of variants) {
const variantKey = originalKey + '_' + variant;
const variantValue = findInDict(origResDict, variantKey);
@ -314,7 +320,7 @@ function processFile(file) {
}
}
if (originalKey !== key && originalValue !== undefined) {
if (originalKey !== undefined && originalKey !== key) {
renamedKeys.set(key, originalKey);
for (const variant of variants) {
@ -360,9 +366,10 @@ function run() {
}
}
fsExtra.copySync(localeMain, localeMainPrevious);
if (anyUpdatesToResDict) {
console.log(`Updating ${localeMain}`);
fsExtra.copySync(localeMain, localeMainPrevious);
fs.writeFileSync(localeMain, JSON.stringify(resDict, null, 2));
}

View file

@ -29,10 +29,15 @@
"resolved": "https://registry.npmjs.org/ellipsize/-/ellipsize-0.1.0.tgz",
"integrity": "sha1-nUNoLUS5GtFuvYQmisEDFwplU/g="
},
"fast-deep-equal": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz",
"integrity": "sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk="
},
"fs-extra": {
"version": "8.0.0",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.0.0.tgz",
"integrity": "sha512-3hU7dzpfqG+A7gloPULls7j9mKDzpCyMOTBvQS0lZfqVfjA6/m0AkLXq0O1vnfP5roXjjL1DuUGKapr8vJWmQw==",
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-7.0.1.tgz",
"integrity": "sha512-YJDaCJZEnBmcbw13fvdAM9AwNOJwOzrE4pqMqBq5nFiEqXUqHwlK4B+3pUw6JNvfSPtX05xFHtYy/1ni01eGCw==",
"requires": {
"graceful-fs": "^4.1.2",
"jsonfile": "^4.0.0",

View file

@ -17,7 +17,7 @@
"deep-keys": "^0.5.0",
"ellipsize": "^0.1.0",
"fast-deep-equal": "^2.0.1",
"fs-extra": "^8.0.0",
"fs-extra": "^7.0.0",
"klaw-sync": "^6.0.0",
"slugify": "^1.3.3"
}