refactor: 🎨 locale loader

This commit is contained in:
ThatOneCalculator 2023-06-16 14:14:47 -07:00
parent 0127cc6ac3
commit 3d6d90a137
No known key found for this signature in database
GPG Key ID: 8703CACD01000000
1 changed files with 74 additions and 43 deletions

View File

@ -2,59 +2,90 @@
* Languages Loader * Languages Loader
*/ */
const fs = require('fs'); const fs = require("fs");
const yaml = require('js-yaml'); const yaml = require("js-yaml");
let languages = [] const languages = [];
let languages_custom = [] const languages_custom = [];
const merge = (...args) => args.reduce((a, c) => ({ const merge = (...args) =>
args.reduce(
(a, c) => ({
...a, ...a,
...c, ...c,
...Object.entries(a) ...Object.entries(a)
.filter(([k]) => c && typeof c[k] === 'object') .filter(([k]) => c && typeof c[k] === "object")
.reduce((a, [k, v]) => (a[k] = merge(v, c[k]), a), {}) .reduce((a, [k, v]) => ((a[k] = merge(v, c[k])), a), {}),
}), {}); }),
{},
);
fs.readdirSync(__dirname).forEach((file) => { fs.readdirSync(__dirname).forEach((file) => {
if (file.includes('.yml')){ if (file.includes(".yml")) {
file = file.slice(0, file.indexOf('.')) locale = file.slice(0, file.indexOf("."));
languages.push(file); languages.push(locale);
} }
}) });
fs.readdirSync(__dirname + '/../custom/locales').forEach((file) => { fs.readdirSync(__dirname + "/../custom/locales").forEach((file) => {
if (file.includes('.yml')){ if (file.includes(".yml")) {
file = file.slice(0, file.indexOf('.')) customLocale = file.slice(0, file.indexOf("."));
languages_custom.push(file); languages_custom.push(customLocale);
} }
}) });
const primaries = { const primaries = {
'en': 'US', en: "US",
'ja': 'JP', ja: "JP",
'zh': 'CN', zh: "CN",
}; };
// 何故か文字列にバックスペース文字が混入することがあり、YAMLが壊れるので取り除く // 何故か文字列にバックスペース文字が混入することがあり、YAMLが壊れるので取り除く
const clean = (text) => text.replace(new RegExp(String.fromCodePoint(0x08), 'g'), ''); const clean = (text) =>
text.replace(new RegExp(String.fromCodePoint(0x08), "g"), "");
const locales = languages.reduce((a, c) => (a[c] = yaml.load(clean(fs.readFileSync(`${__dirname}/${c}.yml`, 'utf-8'))) || {}, a), {}); const locales = languages.reduce(
const locales_custom = languages_custom.reduce((a, c) => (a[c] = yaml.load(clean(fs.readFileSync(`${__dirname}/../custom/locales/${c}.yml`, 'utf-8'))) || {}, a), {}); (a, c) => (
Object.assign(locales, locales_custom) (a[c] =
yaml.load(clean(fs.readFileSync(`${__dirname}/${c}.yml`, "utf-8"))) ||
{}),
a
),
{},
);
const locales_custom = languages_custom.reduce(
(a, c) => (
(a[c] =
yaml.load(
clean(
fs.readFileSync(`${__dirname}/../custom/locales/${c}.yml`, "utf-8"),
),
) || {}),
a
),
{},
);
Object.assign(locales, locales_custom);
module.exports = Object.entries(locales) module.exports = Object.entries(locales).reduce(
.reduce((a, [k ,v]) => (a[k] = (() => { (a, [k, v]) => (
const [lang] = k.split('-'); (a[k] = (() => {
const [lang] = k.split("-");
switch (k) { switch (k) {
case 'ja-JP': return v; case "ja-JP":
case 'ja-KS': return v;
case 'en-US': return merge(locales['ja-JP'], v); case "ja-KS":
default: return merge( case "en-US":
locales['ja-JP'], return merge(locales["ja-JP"], v);
locales['en-US'], default:
return merge(
locales["ja-JP"],
locales["en-US"],
locales[`${lang}-${primaries[lang]}`] || {}, locales[`${lang}-${primaries[lang]}`] || {},
v v,
); );
} }
})(), a), {}); })()),
a
),
{},
);