2021-01-04 02:21:52 +05:30
|
|
|
import fallbackLangData from "./locales/en.json";
|
2020-06-30 19:28:19 +01:00
|
|
|
import percentages from "./locales/percentages.json";
|
2021-02-08 15:43:51 -05:00
|
|
|
import { ENV } from "./constants";
|
2020-06-29 19:22:27 +01:00
|
|
|
|
2020-12-25 12:15:34 +02:00
|
|
|
const COMPLETION_THRESHOLD = 85;
|
2020-06-30 19:28:19 +01:00
|
|
|
|
2021-01-04 02:21:52 +05:30
|
|
|
export interface Language {
|
|
|
|
code: string;
|
2020-06-30 19:28:19 +01:00
|
|
|
label: string;
|
|
|
|
rtl?: boolean;
|
|
|
|
}
|
|
|
|
|
2021-01-07 21:39:57 +02:00
|
|
|
export const defaultLang = { code: "en", label: "English" };
|
|
|
|
|
2020-06-30 19:28:19 +01:00
|
|
|
const allLanguages: Language[] = [
|
2021-01-04 02:21:52 +05:30
|
|
|
{ code: "ar-SA", label: "العربية", rtl: true },
|
|
|
|
{ code: "bg-BG", label: "Български" },
|
2021-01-07 11:30:22 +02:00
|
|
|
{ code: "ca-ES", label: "Català" },
|
2021-01-04 02:21:52 +05:30
|
|
|
{ code: "de-DE", label: "Deutsch" },
|
|
|
|
{ code: "el-GR", label: "Ελληνικά" },
|
|
|
|
{ code: "es-ES", label: "Español" },
|
|
|
|
{ code: "fa-IR", label: "فارسی", rtl: true },
|
|
|
|
{ code: "fi-FI", label: "Suomi" },
|
|
|
|
{ code: "fr-FR", label: "Français" },
|
|
|
|
{ code: "he-IL", label: "עברית", rtl: true },
|
|
|
|
{ code: "hi-IN", label: "हिन्दी" },
|
|
|
|
{ code: "hu-HU", label: "Magyar" },
|
|
|
|
{ code: "id-ID", label: "Bahasa Indonesia" },
|
|
|
|
{ code: "it-IT", label: "Italiano" },
|
|
|
|
{ code: "ja-JP", label: "日本語" },
|
2021-02-02 11:35:52 +02:00
|
|
|
{ code: "kab-KAB", label: "Taqbaylit" },
|
2021-01-04 02:21:52 +05:30
|
|
|
{ code: "ko-KR", label: "한국어" },
|
|
|
|
{ code: "my-MM", label: "Burmese" },
|
|
|
|
{ code: "nb-NO", label: "Norsk bokmål" },
|
|
|
|
{ code: "nl-NL", label: "Nederlands" },
|
|
|
|
{ code: "nn-NO", label: "Norsk nynorsk" },
|
2021-01-07 11:30:22 +02:00
|
|
|
{ code: "pa-IN", label: "ਪੰਜਾਬੀ" },
|
2021-01-04 02:21:52 +05:30
|
|
|
{ code: "pl-PL", label: "Polski" },
|
|
|
|
{ code: "pt-BR", label: "Português Brasileiro" },
|
|
|
|
{ code: "pt-PT", label: "Português" },
|
|
|
|
{ code: "ro-RO", label: "Română" },
|
|
|
|
{ code: "ru-RU", label: "Русский" },
|
|
|
|
{ code: "sk-SK", label: "Slovenčina" },
|
|
|
|
{ code: "sv-SE", label: "Svenska" },
|
|
|
|
{ code: "tr-TR", label: "Türkçe" },
|
|
|
|
{ code: "uk-UA", label: "Українська" },
|
|
|
|
{ code: "zh-CN", label: "简体中文" },
|
|
|
|
{ code: "zh-TW", label: "繁體中文" },
|
2021-01-07 21:39:57 +02:00
|
|
|
].concat([defaultLang]);
|
2021-01-04 02:21:52 +05:30
|
|
|
|
2021-01-07 21:39:57 +02:00
|
|
|
export const languages: Language[] = allLanguages
|
|
|
|
.sort((left, right) => (left.label > right.label ? 1 : -1))
|
2020-06-30 19:28:19 +01:00
|
|
|
.filter(
|
|
|
|
(lang) =>
|
2021-01-04 02:21:52 +05:30
|
|
|
(percentages as Record<string, number>)[lang.code] >=
|
|
|
|
COMPLETION_THRESHOLD,
|
2020-06-30 19:28:19 +01:00
|
|
|
);
|
|
|
|
|
2021-02-08 15:43:51 -05:00
|
|
|
const TEST_LANG_CODE = "__test__";
|
|
|
|
if (process.env.NODE_ENV === ENV.DEVELOPMENT) {
|
|
|
|
languages.unshift(
|
|
|
|
{ code: TEST_LANG_CODE, label: "test language" },
|
|
|
|
{
|
|
|
|
code: `${TEST_LANG_CODE}.rtl`,
|
|
|
|
label: "\u{202a}test language (rtl)\u{202c}",
|
|
|
|
rtl: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-04 02:21:52 +05:30
|
|
|
let currentLang: Language = defaultLang;
|
|
|
|
let currentLangData = {};
|
2020-01-31 21:06:06 +00:00
|
|
|
|
2021-01-04 02:21:52 +05:30
|
|
|
export const setLanguage = async (lang: Language) => {
|
|
|
|
currentLang = lang;
|
|
|
|
document.documentElement.dir = currentLang.rtl ? "rtl" : "ltr";
|
2021-02-08 11:38:38 +01:00
|
|
|
document.documentElement.lang = currentLang.code;
|
2020-04-02 12:21:19 -04:00
|
|
|
|
2021-02-08 15:43:51 -05:00
|
|
|
if (lang.code.startsWith(TEST_LANG_CODE)) {
|
|
|
|
currentLangData = {};
|
|
|
|
} else {
|
|
|
|
currentLangData = await import(
|
|
|
|
/* webpackChunkName: "i18n-[request]" */ `./locales/${currentLang.code}.json`
|
|
|
|
);
|
|
|
|
}
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-01-31 21:06:06 +00:00
|
|
|
|
2021-01-04 02:21:52 +05:30
|
|
|
export const getLanguage = () => currentLang;
|
2020-01-22 16:25:04 +02:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
const findPartsForData = (data: any, parts: string[]) => {
|
2020-11-29 18:32:51 +02:00
|
|
|
for (let index = 0; index < parts.length; ++index) {
|
|
|
|
const part = parts[index];
|
2020-01-31 21:06:06 +00:00
|
|
|
if (data[part] === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
data = data[part];
|
2020-01-22 16:25:04 +02:00
|
|
|
}
|
2020-01-31 21:06:06 +00:00
|
|
|
if (typeof data !== "string") {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return data;
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-01-22 16:25:04 +02:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const t = (path: string, replacement?: { [key: string]: string }) => {
|
2021-02-08 15:43:51 -05:00
|
|
|
if (currentLang.code.startsWith(TEST_LANG_CODE)) {
|
|
|
|
const name = replacement
|
|
|
|
? `${path}(${JSON.stringify(replacement).slice(1, -1)})`
|
|
|
|
: path;
|
|
|
|
return `\u{202a}[[${name}]]\u{202c}`;
|
|
|
|
}
|
|
|
|
|
2020-01-31 21:06:06 +00:00
|
|
|
const parts = path.split(".");
|
|
|
|
let translation =
|
2021-01-04 02:21:52 +05:30
|
|
|
findPartsForData(currentLangData, parts) ||
|
|
|
|
findPartsForData(fallbackLangData, parts);
|
2020-01-31 21:06:06 +00:00
|
|
|
if (translation === undefined) {
|
2020-02-02 20:04:35 +02:00
|
|
|
throw new Error(`Can't find translation for ${path}`);
|
2020-01-31 21:06:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (replacement) {
|
2020-11-29 18:32:51 +02:00
|
|
|
for (const key in replacement) {
|
2020-02-02 20:04:35 +02:00
|
|
|
translation = translation.replace(`{{${key}}}`, replacement[key]);
|
2020-01-31 21:06:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return translation;
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|