-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
612 additions
and
274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,4 +23,5 @@ dist-ssr | |
*.sln | ||
*.sw? | ||
|
||
public/monaco-editor | ||
public/monaco-editor | ||
src/i18n/*.json |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import fs from "fs"; | ||
import { writeFile } from "fs/promises"; | ||
import path from "path"; | ||
import url from "url"; | ||
import { parse } from "csv-parse"; | ||
|
||
const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); | ||
const importFile = path.join(__dirname, "../src/i18n/languages.csv"); | ||
const exportDir = path.join(__dirname, "../src/i18n"); | ||
|
||
let languages; | ||
const langMap = new Map(); | ||
const init = (arr) => { | ||
const [_key, ...langs] = arr; | ||
languages = langs.map((langName) => { | ||
const lang = {}; | ||
langMap.set(langName, lang); | ||
return lang; | ||
}); | ||
}; | ||
const output = () => { | ||
// 保存为json文件 | ||
const langList = []; | ||
const arr = []; | ||
langMap.forEach((json, lang) => { | ||
langList.push({ lang, label: json.language }); | ||
arr.push( | ||
writeFile( | ||
path.join(exportDir, `${lang}.json`), | ||
JSON.stringify(json, undefined, 4) | ||
) | ||
); | ||
}); | ||
arr.push( | ||
writeFile( | ||
path.join(exportDir, `languages.json`), | ||
JSON.stringify(langList, undefined, 4) | ||
) | ||
); | ||
Promise.all(arr).then(() => console.log("done")); | ||
}; | ||
|
||
const setValue = (obj, key, value) => { | ||
if (!obj || !key) return; | ||
const keys = key.split("."); | ||
let _obj = obj; | ||
while (keys.length > 1) { | ||
const _key = keys.shift(); | ||
if (_obj[_key] === undefined) { | ||
_obj = _obj[_key] = {}; | ||
} else if (typeof _obj[_key] === "object") { | ||
_obj = _obj[_key]; | ||
} else { | ||
console.error(`Key [${key}] error!`); | ||
} | ||
} | ||
const _key = keys[0]; | ||
if (_obj[_key] !== undefined) { | ||
console.error(`Key [${key}] repeat!`); | ||
} | ||
_obj[_key] = value || ""; | ||
}; | ||
|
||
function main(){ | ||
const csvp = parse(); | ||
csvp.on("end", output); | ||
csvp.on("data", function (chunk) { | ||
// 解析language.csv | ||
if (!languages) return init(chunk); // 第一行为表头 | ||
const [key, ...values] = chunk; | ||
values.forEach((value, index) => { | ||
setValue(languages[index], key, value); | ||
}); | ||
}); | ||
fs.createReadStream(importFile).pipe(csvp); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.