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

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) => ({
...a,
...c,
...Object.entries(a)
.filter(([k]) => c && typeof c[k] === 'object')
.reduce((a, [k, v]) => (a[k] = merge(v, c[k]), a), {})
}), {});
const merge = (...args) =>
args.reduce(
(a, c) => ({
...a,
...c,
...Object.entries(a)
.filter(([k]) => c && typeof c[k] === "object")
.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] = (() => {
switch (k) { const [lang] = k.split("-");
case 'ja-JP': return v; switch (k) {
case 'ja-KS': case "ja-JP":
case 'en-US': return merge(locales['ja-JP'], v); return v;
default: return merge( case "ja-KS":
locales['ja-JP'], case "en-US":
locales['en-US'], return merge(locales["ja-JP"], v);
locales[`${lang}-${primaries[lang]}`] || {}, default:
v return merge(
); locales["ja-JP"],
} locales["en-US"],
})(), a), {}); locales[`${lang}-${primaries[lang]}`] || {},
v,
);
}
})()),
a
),
{},
);