2020-01-21 01:14:10 +02:00
|
|
|
import LanguageDetector from "i18next-browser-languagedetector";
|
|
|
|
|
2020-01-31 21:06:06 +00:00
|
|
|
export const languages = [
|
|
|
|
{ lng: "en", label: "English", data: require("./locales/en.json") },
|
2020-03-19 22:12:47 +02:00
|
|
|
{ lng: "de-DE", label: "Deutsch", data: require("./locales/de-DE.json") },
|
|
|
|
{ lng: "es-ES", label: "Español", data: require("./locales/es-ES.json") },
|
2020-03-31 14:44:29 +03:00
|
|
|
{ lng: "el-GR", label: "Ελληνικά", data: require("./locales/el-GR.json") },
|
2020-03-19 22:12:47 +02:00
|
|
|
{ lng: "fr-FR", label: "Français", data: require("./locales/fr-FR.json") },
|
|
|
|
{
|
|
|
|
lng: "id-ID",
|
|
|
|
label: "Bahasa Indonesia",
|
|
|
|
data: require("./locales/id-ID.json"),
|
|
|
|
},
|
2020-03-27 14:12:27 +02:00
|
|
|
{ lng: "it-IT", label: "Italiano", data: require("./locales/it-IT.json") },
|
2020-03-22 10:41:05 -07:00
|
|
|
{ lng: "hu-HU", label: "Magyar", data: require("./locales/hu-HU.json") },
|
2020-04-03 16:06:30 +03:00
|
|
|
{ lng: "nl-NL", label: "Nederlands", data: require("./locales/nl-NL.json") },
|
2020-03-19 22:12:47 +02:00
|
|
|
{ lng: "no-No", label: "Norsk", data: require("./locales/no-NO.json") },
|
|
|
|
{ lng: "pl-PL", label: "Polski", data: require("./locales/pl-PL.json") },
|
|
|
|
{ lng: "pt-PT", label: "Português", data: require("./locales/pt-PT.json") },
|
|
|
|
{ lng: "ru-RU", label: "Русский", data: require("./locales/ru-RU.json") },
|
|
|
|
{ lng: "tr-TR", label: "Türkçe", data: require("./locales/tr-TR.json") },
|
|
|
|
{ lng: "ko-KR", label: "한국어", data: require("./locales/ko-KR.json") },
|
|
|
|
{ lng: "zh-TW", label: "繁體中文", data: require("./locales/zh-TW.json") },
|
2020-03-20 18:32:46 +02:00
|
|
|
{ lng: "zh-CN", label: "简体中文", data: require("./locales/zh-CN.json") },
|
2020-04-02 12:21:19 -04:00
|
|
|
{
|
|
|
|
lng: "ar-SA",
|
2020-04-03 13:40:01 +01:00
|
|
|
label: "العربية",
|
2020-04-02 12:21:19 -04:00
|
|
|
data: require("./locales/ar-SA.json"),
|
|
|
|
rtl: true,
|
|
|
|
},
|
2020-01-31 21:06:06 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let currentLanguage = languages[0];
|
|
|
|
const fallbackLanguage = languages[0];
|
|
|
|
|
|
|
|
export function setLanguage(newLng: string | undefined) {
|
|
|
|
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-01-31 21:06:06 +00:00
|
|
|
languageDetector.cacheUserLanguage(currentLanguage.lng);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getLanguage() {
|
2020-04-02 12:21:19 -04:00
|
|
|
return currentLanguage;
|
2020-01-31 21:06:06 +00:00
|
|
|
}
|
2020-01-22 16:25:04 +02:00
|
|
|
|
2020-01-31 21:06:06 +00:00
|
|
|
function findPartsForData(data: any, parts: string[]) {
|
|
|
|
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-01-22 16:25:04 +02:00
|
|
|
}
|
|
|
|
|
2020-01-31 21:06:06 +00:00
|
|
|
export function t(path: string, replacement?: { [key: string]: string }) {
|
|
|
|
const parts = path.split(".");
|
|
|
|
let translation =
|
|
|
|
findPartsForData(currentLanguage.data, parts) ||
|
|
|
|
findPartsForData(fallbackLanguage.data, parts);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const languageDetector = new LanguageDetector();
|
|
|
|
languageDetector.init({
|
|
|
|
languageUtils: {
|
2020-03-23 13:05:07 +02:00
|
|
|
formatLanguageCode: function (lng: string) {
|
2020-01-31 21:06:06 +00:00
|
|
|
return lng;
|
|
|
|
},
|
|
|
|
isWhitelisted: () => true,
|
|
|
|
},
|
|
|
|
checkWhitelist: false,
|
|
|
|
});
|
2020-01-22 16:25:04 +02:00
|
|
|
|
2020-01-31 21:06:06 +00:00
|
|
|
setLanguage(languageDetector.detect());
|