FoundKey/locales/index.js

72 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-07-06 03:17:38 +00:00
/**
* Languages Loader
*/
const fs = require('fs');
const yaml = require('js-yaml');
2019-03-06 14:28:50 +00:00
const merge = (...args) => args.reduce((a, c) => ({
...a,
...c,
// this is necessary to merge "sub-objects" correctly instead of overwriting them
2019-03-06 14:28:50 +00:00
...Object.entries(a)
.filter(([k]) => c && typeof c[k] === 'object')
.reduce((a, [k, v]) => (a[k] = merge(v, c[k]), a), {})
}), {});
2018-07-06 03:17:38 +00:00
// For a language to be generated as a locale it has to be listed here.
2019-03-06 14:28:50 +00:00
const languages = [
2020-05-29 23:00:02 +00:00
'ar-SA',
'bn-BD',
'ca-ES',
2020-11-07 03:32:08 +00:00
'cs-CZ',
'de-DE',
2019-03-06 14:28:50 +00:00
'en-US',
'es-ES',
'fr-FR',
'id-ID',
2021-10-31 14:18:55 +00:00
'it-IT',
2019-03-06 14:28:50 +00:00
'ja-JP',
'ja-KS',
2020-11-07 03:32:08 +00:00
'kab-KAB',
'kn-IN',
2019-03-06 14:28:50 +00:00
'ko-KR',
2020-11-07 03:32:08 +00:00
'nl-NL',
'pl-PL',
'pt-PT',
'ru-RU',
'sk-SK',
'sv-SE',
'tr-TR',
2020-11-07 03:32:08 +00:00
'uk-UA',
2022-07-13 10:24:53 +00:00
'vi-VN',
2020-02-10 11:29:44 +00:00
'zh-CN',
'zh-TW',
2019-03-06 14:28:50 +00:00
];
// Load the locales listed above.
const locales = languages.reduce(
(acc, lang) => {
acc[lang] = yaml.load(
fs.readFileSync(`${__dirname}/${lang}.yml`, 'utf-8')
// Remove backspace characters, which for some reason can get mixed in with the string and break the YAML.
.replaceAll('\b', '')
) || {};
return acc;
},
{} // initial accumulator
);
2019-03-06 14:28:50 +00:00
module.exports = Object.entries(locales)
.reduce((acc, [lang, strings]) => {
if (lang == 'en-US') {
acc[lang] = strings;
} else {
// all other locales fall back to en-US
acc[lang] = merge(locales['en-US'], strings);
}
return acc;
},
{} // initial accumulator
);