Add i18n-sync script

This commit is contained in:
the1812 2019-06-06 00:10:30 +08:00
parent 978fae31c9
commit b3b7ee4f6e
8 changed files with 1325 additions and 1224 deletions

1
.gitignore vendored
View File

@ -1,5 +1,4 @@
.vscode/
builder/node/
builder/dotnet/Properties
.node_modules/
package-lock.json

View File

@ -0,0 +1,38 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const path_1 = require("path");
class LanguageKeys {
constructor(mapKeys, regexKeys) {
this.mapKeys = mapKeys;
this.regexKeys = regexKeys;
}
diff(other) {
return new LanguageKeys(this.mapKeys.filter(it => !other.mapKeys.includes(it)), this.regexKeys.filter(it => !other.regexKeys.includes(it)));
}
}
const readFile = (filename) => fs_1.readFileSync(filename, { encoding: 'utf-8' });
const config = JSON.parse(readFile('i18n-sync.json'));
const extractKeys = (language) => {
let temp = null;
const mapKeys = [];
const mapRegex = /\[`(.+?)`,/g;
while (temp = mapRegex.exec(language)) {
mapKeys.push(temp[1]);
}
const regexKeys = [];
const regexRegex = /\[(\/.*\/[a-z]*),/g;
while (temp = regexRegex.exec(language)) {
regexKeys.push(temp[1]);
}
return new LanguageKeys(mapKeys, regexKeys);
};
const mainLanguageKey = extractKeys(readFile(path_1.join(config.folder, `i18n.${config.mainLanguage}.js`)));
const targetLanguages = config.targetLanguages.map(l => readFile(path_1.join(config.folder, `i18n.${l}.js`)));
targetLanguages.forEach(language => {
const diff = mainLanguageKey.diff(extractKeys(language));
const output = language
.replace(/([\s\n]*\[`\*`)/, `\n ${diff.mapKeys.map(k => '[`' + k + '`, `TODO:`],').join('\n ')}$1`)
.replace(/(\]\);?[\s\n]*export default)/, ` ${diff.regexKeys.map(k => '[' + k + ', `TODO:`],').join('\n ')}\n$1`);
console.log(output);
});

View File

@ -0,0 +1,48 @@
import { readFileSync } from 'fs';
import { join } from 'path';
interface Config {
folder: string
mainLanguage: string
targetLanguages: string[]
}
class LanguageKeys {
constructor(
public mapKeys: string[],
public regexKeys: string[]
) { }
diff(other: LanguageKeys) {
return new LanguageKeys(
this.mapKeys.filter(it => !other.mapKeys.includes(it)),
this.regexKeys.filter(it => !other.regexKeys.includes(it))
)
}
}
const readFile = (filename: string) => readFileSync(filename, { encoding: 'utf-8' })
const config = JSON.parse(readFile('i18n-sync.json')) as Config
const extractKeys = (language: string) => {
let temp = null
const mapKeys = []
const mapRegex = /\[`(.+?)`,/g
while (temp = mapRegex.exec(language)) {
mapKeys.push(temp[1])
}
const regexKeys = []
const regexRegex = /\[(\/.*\/[a-z]*),/g
while (temp = regexRegex.exec(language)) {
regexKeys.push(temp[1])
}
return new LanguageKeys(mapKeys, regexKeys)
}
const mainLanguageKey = extractKeys(readFile(join(config.folder, `i18n.${config.mainLanguage}.js`)))
const targetLanguages = config.targetLanguages.map(l => readFile(join(config.folder, `i18n.${l}.js`)))
targetLanguages.forEach(language => {
const diff = mainLanguageKey.diff(extractKeys(language))
const output = language
.replace(/([\s\n]*\[`\*`)/, `\n ${diff.mapKeys.map(k => '[`' + k + '`, `TODO:`],').join('\n ')}$1`)
.replace(/(\]\);?[\s\n]*export default)/, ` ${diff.regexKeys.map(k => '[' + k + ', `TODO:`],').join('\n ')}\n$1`)
console.log(output)
})

View File

@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "commonjs",
"noImplicitAny": true,
"strictNullChecks": true,
"sourceMap": false,
}
}

7
i18n-sync.json Normal file
View File

@ -0,0 +1,7 @@
{
"folder": "utils/i18n",
"mainLanguage": "ja-JP",
"targetLanguages": [
"en-US"
]
}

View File

@ -1,12 +1,12 @@
export const map = new Map([
/* Your translation here */
[`*`, [
/* CSS translation here */
]],
/* Your translation here */
[`*`, [
/* CSS translation here */
]],
]);
export const regex = new Map([
/* Regex translation here */
/* Regex translation here */
]);
export default {
export: { map, regex },
export: { map, regex },
};

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff