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") },
|
|
|
|
{ lng: "de", label: "Deutsch", data: require("./locales/de.json") },
|
|
|
|
{ lng: "es", label: "Español", data: require("./locales/es.json") },
|
|
|
|
{ lng: "fr", label: "Français", data: require("./locales/fr.json") },
|
2020-03-07 01:01:01 +02:00
|
|
|
{ lng: "id", label: "Bahasa Indonesia", data: require("./locales/id.json") },
|
2020-02-21 23:56:06 +01:00
|
|
|
{ lng: "no", label: "Norsk", data: require("./locales/no.json") },
|
2020-02-07 11:37:33 +00:00
|
|
|
{ lng: "pl", label: "Polski", data: require("./locales/pl.json") },
|
2020-01-31 21:06:06 +00:00
|
|
|
{ lng: "pt", label: "Português", data: require("./locales/pt.json") },
|
|
|
|
{ lng: "ru", label: "Русский", data: require("./locales/ru.json") },
|
2020-02-21 18:14:06 +03:00
|
|
|
{ lng: "tr", label: "Türkçe", data: require("./locales/tr.json") },
|
2020-03-18 19:59:48 +02:00
|
|
|
{ lng: "ko", label: "한국어", data: require("./locales/ko.json") },
|
2020-01-31 21:06:06 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let currentLanguage = languages[0];
|
|
|
|
const fallbackLanguage = languages[0];
|
|
|
|
|
|
|
|
export function setLanguage(newLng: string | undefined) {
|
|
|
|
currentLanguage =
|
|
|
|
languages.find(language => language.lng === newLng) || fallbackLanguage;
|
|
|
|
|
|
|
|
languageDetector.cacheUserLanguage(currentLanguage.lng);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getLanguage() {
|
|
|
|
return currentLanguage.lng;
|
|
|
|
}
|
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: {
|
|
|
|
formatLanguageCode: function(lng: string) {
|
|
|
|
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());
|