mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
Add i18n-sync script
This commit is contained in:
parent
978fae31c9
commit
b3b7ee4f6e
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,5 +1,4 @@
|
||||
.vscode/
|
||||
builder/node/
|
||||
builder/dotnet/Properties
|
||||
.node_modules/
|
||||
package-lock.json
|
||||
|
||||
38
builder/node/i18n-sync/i18n-sync.js
Normal file
38
builder/node/i18n-sync/i18n-sync.js
Normal 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);
|
||||
});
|
||||
48
builder/node/i18n-sync/i18n-sync.ts
Normal file
48
builder/node/i18n-sync/i18n-sync.ts
Normal 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)
|
||||
})
|
||||
9
builder/node/i18n-sync/tsconfig.json
Normal file
9
builder/node/i18n-sync/tsconfig.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ESNext",
|
||||
"module": "commonjs",
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": true,
|
||||
"sourceMap": false,
|
||||
}
|
||||
}
|
||||
7
i18n-sync.json
Normal file
7
i18n-sync.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"folder": "utils/i18n",
|
||||
"mainLanguage": "ja-JP",
|
||||
"targetLanguages": [
|
||||
"en-US"
|
||||
]
|
||||
}
|
||||
@ -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
Loading…
Reference in New Issue
Block a user