mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
Dynamicaly import locales (#1793)
* dynamicly import locales * fix tests * reformat languages
This commit is contained in:
parent
5970bb7ee9
commit
0a3fb70ec7
7 changed files with 85 additions and 53 deletions
87
src/i18n.ts
87
src/i18n.ts
|
@ -1,55 +1,58 @@
|
|||
import LanguageDetector from "i18next-browser-languagedetector";
|
||||
|
||||
export const languages = [
|
||||
{ lng: "en", label: "English", data: require("./locales/en.json") },
|
||||
{ lng: "bg-BG", label: "Български", data: require("./locales/bg-BG.json") },
|
||||
{ lng: "nb-No", label: "Bokmål", data: require("./locales/nb-NO.json") },
|
||||
{ lng: "de-DE", label: "Deutsch", data: require("./locales/de-DE.json") },
|
||||
{ lng: "es-ES", label: "Español", data: require("./locales/es-ES.json") },
|
||||
{ lng: "ca-ES", label: "Catalan", data: require("./locales/ca-ES.json") },
|
||||
{ lng: "el-GR", label: "Ελληνικά", data: require("./locales/el-GR.json") },
|
||||
{ lng: "fr-FR", label: "Français", data: require("./locales/fr-FR.json") },
|
||||
{
|
||||
lng: "id-ID",
|
||||
label: "Bahasa Indonesia",
|
||||
data: require("./locales/id-ID.json"),
|
||||
},
|
||||
{ lng: "it-IT", label: "Italiano", data: require("./locales/it-IT.json") },
|
||||
{ lng: "hu-HU", label: "Magyar", data: require("./locales/hu-HU.json") },
|
||||
{ lng: "nl-NL", label: "Nederlands", data: require("./locales/nl-NL.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: "uk-UA", label: "Українська", data: require("./locales/uk-UA.json") },
|
||||
{ lng: "fi-FI", label: "Suomi", data: require("./locales/fi-FI.json") },
|
||||
{ lng: "tr-TR", label: "Türkçe", data: require("./locales/tr-TR.json") },
|
||||
{ lng: "ja-JP", label: "日本語", data: require("./locales/ja-JP.json") },
|
||||
{ lng: "ko-KR", label: "한국어", data: require("./locales/ko-KR.json") },
|
||||
{ lng: "zh-TW", label: "繁體中文", data: require("./locales/zh-TW.json") },
|
||||
{ lng: "zh-CN", label: "简体中文", data: require("./locales/zh-CN.json") },
|
||||
{
|
||||
lng: "ar-SA",
|
||||
label: "العربية",
|
||||
data: require("./locales/ar-SA.json"),
|
||||
rtl: true,
|
||||
},
|
||||
{
|
||||
lng: "he-IL",
|
||||
label: "עברית",
|
||||
data: require("./locales/he-IL.json"),
|
||||
rtl: true,
|
||||
},
|
||||
{ lng: "en", label: "English", data: "en.json" },
|
||||
{ lng: "bg-BG", label: "Български", data: "bg-BG.json" },
|
||||
{ lng: "de-DE", label: "Deutsch", data: "de-DE.json" },
|
||||
{ lng: "nb-No", label: "Bokmål", data: "nb-NO.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" },
|
||||
{ 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 },
|
||||
];
|
||||
|
||||
let currentLanguage = languages[0];
|
||||
let currentLanguageData = {};
|
||||
const fallbackLanguage = languages[0];
|
||||
const fallbackLanguageData = require("./locales/en.json");
|
||||
|
||||
export const setLanguage = (newLng: string | undefined) => {
|
||||
export const setLanguage = async (newLng: string | undefined) => {
|
||||
currentLanguage =
|
||||
languages.find((language) => language.lng === newLng) || fallbackLanguage;
|
||||
|
||||
document.documentElement.dir = currentLanguage.rtl ? "rtl" : "ltr";
|
||||
|
||||
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}`);
|
||||
|
||||
languageDetector.cacheUserLanguage(currentLanguage.lng);
|
||||
};
|
||||
|
||||
|
@ -72,8 +75,8 @@ const findPartsForData = (data: any, parts: string[]) => {
|
|||
export const t = (path: string, replacement?: { [key: string]: string }) => {
|
||||
const parts = path.split(".");
|
||||
let translation =
|
||||
findPartsForData(currentLanguage.data, parts) ||
|
||||
findPartsForData(fallbackLanguage.data, parts);
|
||||
findPartsForData(currentLanguageData, parts) ||
|
||||
findPartsForData(fallbackLanguageData, parts);
|
||||
if (translation === undefined) {
|
||||
throw new Error(`Can't find translation for ${path}`);
|
||||
}
|
||||
|
@ -94,5 +97,3 @@ languageDetector.init({
|
|||
},
|
||||
checkWhitelist: false,
|
||||
});
|
||||
|
||||
setLanguage(languageDetector.detect());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue