2020-01-21 01:14:10 +02:00
|
|
|
import LanguageDetector from "i18next-browser-languagedetector";
|
|
|
|
|
2020-06-29 19:22:27 +01:00
|
|
|
import fallbackLanguageData from "./locales/en.json";
|
2020-06-30 19:28:19 +01:00
|
|
|
import percentages from "./locales/percentages.json";
|
2020-06-29 19:22:27 +01:00
|
|
|
|
2020-06-30 19:28:19 +01:00
|
|
|
const COMPLETION_THRESHOLD_TO_EXCEED = 85;
|
|
|
|
|
|
|
|
interface Language {
|
|
|
|
lng: string;
|
|
|
|
label: string;
|
|
|
|
data: string;
|
|
|
|
rtl?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
const allLanguages: Language[] = [
|
2020-06-27 12:02:54 +01:00
|
|
|
{ lng: "bg-BG", label: "Български", data: "bg-BG.json" },
|
|
|
|
{ lng: "de-DE", label: "Deutsch", data: "de-DE.json" },
|
|
|
|
{ lng: "es-ES", label: "Español", data: "es-ES.json" },
|
|
|
|
{ lng: "ca-ES", label: "Catalan", data: "ca-ES.json" },
|
|
|
|
{ lng: "el-GR", label: "Ελληνικά", data: "el-GR.json" },
|
|
|
|
{ lng: "fr-FR", label: "Français", data: "fr-FR.json" },
|
|
|
|
{ lng: "id-ID", label: "Bahasa Indonesia", data: "id-ID.json" },
|
|
|
|
{ lng: "it-IT", label: "Italiano", data: "it-IT.json" },
|
|
|
|
{ lng: "hu-HU", label: "Magyar", data: "hu-HU.json" },
|
|
|
|
{ lng: "nl-NL", label: "Nederlands", data: "nl-NL.json" },
|
2020-06-30 14:51:25 +02:00
|
|
|
{ lng: "nb-NO", label: "Norsk bokmål", data: "nb-NO.json" },
|
|
|
|
{ lng: "nn-NO", label: "Norsk nynorsk", data: "nn-NO.json" },
|
2020-06-27 12:02:54 +01:00
|
|
|
{ lng: "pl-PL", label: "Polski", data: "pl-PL.json" },
|
|
|
|
{ lng: "pt-PT", label: "Português", data: "pt-PT.json" },
|
|
|
|
{ lng: "ru-RU", label: "Русский", data: "ru-RU.json" },
|
|
|
|
{ lng: "uk-UA", label: "Українська", data: "uk-UA.json" },
|
|
|
|
{ lng: "fi-FI", label: "Suomi", data: "fi-FI.json" },
|
|
|
|
{ lng: "tr-TR", label: "Türkçe", data: "tr-TR.json" },
|
|
|
|
{ lng: "ja-JP", label: "日本語", data: "ja-JP.json" },
|
|
|
|
{ lng: "ko-KR", label: "한국어", data: "ko-KR.json" },
|
|
|
|
{ lng: "zh-TW", label: "繁體中文", data: "zh-TW.json" },
|
|
|
|
{ lng: "zh-CN", label: "简体中文", data: "zh-CN.json" },
|
|
|
|
{ lng: "ar-SA", label: "العربية", data: "ar-SA.json", rtl: true },
|
|
|
|
{ lng: "he-IL", label: "עברית", data: "he-IL.json", rtl: true },
|
2020-07-01 22:50:27 +05:30
|
|
|
{ lng: "hi-IN", label: "हिन्दी", data: "hi-IN.json" },
|
2020-07-29 18:56:41 +03:00
|
|
|
{ lng: "ta-IN", label: "தமிழ்", data: "ta-IN.json" },
|
2020-01-31 21:06:06 +00:00
|
|
|
];
|
|
|
|
|
2020-06-30 19:28:19 +01:00
|
|
|
export const languages: Language[] = [
|
|
|
|
{ lng: "en", label: "English", data: "en.json" },
|
|
|
|
]
|
|
|
|
.concat(
|
|
|
|
allLanguages.sort((left, right) => (left.label > right.label ? 1 : -1)),
|
|
|
|
)
|
|
|
|
.filter(
|
|
|
|
(lang) =>
|
|
|
|
(percentages as Record<string, number>)[lang.lng] >
|
|
|
|
COMPLETION_THRESHOLD_TO_EXCEED,
|
|
|
|
);
|
|
|
|
|
2020-01-31 21:06:06 +00:00
|
|
|
let currentLanguage = languages[0];
|
2020-06-27 12:02:54 +01:00
|
|
|
let currentLanguageData = {};
|
2020-01-31 21:06:06 +00:00
|
|
|
const fallbackLanguage = languages[0];
|
|
|
|
|
2020-06-27 12:02:54 +01:00
|
|
|
export const setLanguage = async (newLng: string | undefined) => {
|
2020-01-31 21:06:06 +00:00
|
|
|
currentLanguage =
|
2020-03-23 13:05:07 +02:00
|
|
|
languages.find((language) => language.lng === newLng) || fallbackLanguage;
|
2020-01-31 21:06:06 +00:00
|
|
|
|
2020-04-02 12:21:19 -04:00
|
|
|
document.documentElement.dir = currentLanguage.rtl ? "rtl" : "ltr";
|
|
|
|
|
2020-06-27 12:02:54 +01:00
|
|
|
currentLanguageData = await import(`./locales/${currentLanguage.data}`);
|
|
|
|
|
|
|
|
languageDetector.cacheUserLanguage(currentLanguage.lng);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const setLanguageFirstTime = async () => {
|
|
|
|
const newLng: string | undefined = languageDetector.detect();
|
|
|
|
|
|
|
|
currentLanguage =
|
|
|
|
languages.find((language) => language.lng === newLng) || fallbackLanguage;
|
|
|
|
|
|
|
|
document.documentElement.dir = currentLanguage.rtl ? "rtl" : "ltr";
|
|
|
|
|
|
|
|
currentLanguageData = await import(`./locales/${currentLanguage.data}`);
|
|
|
|
|
2020-01-31 21:06:06 +00:00
|
|
|
languageDetector.cacheUserLanguage(currentLanguage.lng);
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-01-31 21:06:06 +00:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const getLanguage = () => currentLanguage;
|
2020-01-22 16:25:04 +02:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
const findPartsForData = (data: any, parts: string[]) => {
|
2020-01-31 21:06:06 +00:00
|
|
|
for (var i = 0; i < parts.length; ++i) {
|
|
|
|
const part = parts[i];
|
|
|
|
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 }) => {
|
2020-01-31 21:06:06 +00:00
|
|
|
const parts = path.split(".");
|
|
|
|
let translation =
|
2020-06-27 12:02:54 +01:00
|
|
|
findPartsForData(currentLanguageData, parts) ||
|
|
|
|
findPartsForData(fallbackLanguageData, 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) {
|
|
|
|
for (var 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
|
|
|
};
|
2020-01-31 21:06:06 +00:00
|
|
|
|
|
|
|
const languageDetector = new LanguageDetector();
|
|
|
|
languageDetector.init({
|
|
|
|
languageUtils: {
|
2020-05-20 16:21:37 +03:00
|
|
|
formatLanguageCode: (lng: string) => lng,
|
2020-01-31 21:06:06 +00:00
|
|
|
isWhitelisted: () => true,
|
|
|
|
},
|
|
|
|
checkWhitelist: false,
|
|
|
|
});
|