bigbuffet-rw/app/soapbox/locales/messages.ts

103 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-04-21 10:19:33 -07:00
type MessageJson = Record<string, string>;
type MessageModule = { default: MessageJson };
/** Import custom messages */
const importCustom = (locale: string): Promise<MessageModule> => {
2022-03-03 20:45:22 -08:00
return import(/* webpackChunkName: "locale_[request]" */`custom/locales/${locale}.json`)
2022-04-21 10:19:33 -07:00
.catch(() => ({ default: {} }));
2020-06-04 16:34:28 -07:00
};
2022-04-21 10:19:33 -07:00
/** Import git-checked messages */
const importMessages = (locale: string): Promise<MessageModule> => {
return import(/* webpackChunkName: "locale_[request]" */`./${locale}.json`);
};
2022-04-21 10:19:33 -07:00
/** Override custom messages */
const importMessagesWithCustom = (locale: string): Promise<MessageJson> => {
return Promise.all([
importMessages(locale),
importCustom(locale),
]).then(messages => {
const [native, custom] = messages;
return Object.assign(native.default, custom.default);
2022-03-03 20:45:22 -08:00
}).catch(error => {
console.error(error);
throw error;
});
};
2022-04-21 10:19:33 -07:00
const locales: string[] = [
'ar',
'ast',
'bg',
'bn',
'br',
'ca',
'co',
'cs',
'cy',
'da',
'de',
'el',
'en',
'en-Shaw',
'eo',
'es-AR',
'es',
'et',
'eu',
'fa',
'fi',
'fr',
'ga',
'gl',
'he',
'hi',
'hr',
'hu',
'hy',
'id',
'io',
2022-05-01 14:30:29 -07:00
'is',
'it',
'ja',
'ka',
'kk',
'ko',
'lt',
'lv',
'mk',
'ms',
'nl',
'nn',
'no',
'oc',
'pl',
'pt-BR',
'pt',
'ro',
'ru',
'sk',
'sl',
'sq',
'sr',
'sr-Latn',
'sv',
'ta',
'te',
'th',
'tr',
'uk',
'zh-CN',
'zh-HK',
'zh-TW',
];
2022-04-21 10:19:33 -07:00
/** Soapbox locales map */
const messages = locales.reduce((acc, locale) => {
acc[locale] = () => importMessagesWithCustom(locale);
return acc;
2022-04-21 10:19:33 -07:00
}, {} as Record<string, () => Promise<MessageJson>>);
export default messages;