excalidraw/src/i18n.ts

126 lines
3.8 KiB
TypeScript
Raw Normal View History

import LanguageDetector from "i18next-browser-languagedetector";
import fallbackLanguageData from "./locales/en.json";
Add script to calculate percentage of translation (#1826) * add script to calculate percentage of translation * test translation change * change translation * test * change translation * Calculate percentages of each translation file * test * Calculate percentages of each translation file * change translation * test * test * Calculate percentages of each translation file * test * Calculate percentages of each translation file * fix workflow * test * test again * Calculate percentages of each translation file * Calculate percentages of each translation file * test * refactor * change build logic * fix types, move English first * docs added * test translation file * test * test * test * test * test * test * test * test * test * test * test * test * Calculate percentages of each translation file * let this be the final test please * Calculate percentages of each translation file * test * test * Test * Calculate percentages of each translation file * test * Calculate percentages of each translation file * test * Calculate percentages of each translation file * test * Auto commit: Calculate translation coverage * test * test * test * test * Auto commit: Calculate translation coverage * test * only on master * test * test * Auto commit: Calculate translation coverage * switch to master branch Co-authored-by: i18n automation <runner@fv-az76.2iswp1o5zimezclxzdlwqia2gf.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az129.idlktykl4ure3gqe2lnji05orb.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az76.pjgcdo5npjpenpqz2nk0ztqvxd.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az33.senarqq4ucbulg04aytwntvgah.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az51.icvemaqob4xunfekbtdiz2tu2c.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az78.gikxu4m3dpiulftj3bftpuu3ee.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az121.cqdewbghluceforu5pkvpnveec.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az139.jsbds1i2htye3fh1bzwbe4ugmf.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az50.0bg2cysi0dkefjvuua0a0kbd1h.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az51.nhi3in4tbx4ehjtltcwuwbwsua.cx.internal.cloudapp.net>
2020-06-30 19:28:19 +01:00
import percentages from "./locales/percentages.json";
Add script to calculate percentage of translation (#1826) * add script to calculate percentage of translation * test translation change * change translation * test * change translation * Calculate percentages of each translation file * test * Calculate percentages of each translation file * change translation * test * test * Calculate percentages of each translation file * test * Calculate percentages of each translation file * fix workflow * test * test again * Calculate percentages of each translation file * Calculate percentages of each translation file * test * refactor * change build logic * fix types, move English first * docs added * test translation file * test * test * test * test * test * test * test * test * test * test * test * test * Calculate percentages of each translation file * let this be the final test please * Calculate percentages of each translation file * test * test * Test * Calculate percentages of each translation file * test * Calculate percentages of each translation file * test * Calculate percentages of each translation file * test * Auto commit: Calculate translation coverage * test * test * test * test * Auto commit: Calculate translation coverage * test * only on master * test * test * Auto commit: Calculate translation coverage * switch to master branch Co-authored-by: i18n automation <runner@fv-az76.2iswp1o5zimezclxzdlwqia2gf.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az129.idlktykl4ure3gqe2lnji05orb.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az76.pjgcdo5npjpenpqz2nk0ztqvxd.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az33.senarqq4ucbulg04aytwntvgah.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az51.icvemaqob4xunfekbtdiz2tu2c.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az78.gikxu4m3dpiulftj3bftpuu3ee.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az121.cqdewbghluceforu5pkvpnveec.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az139.jsbds1i2htye3fh1bzwbe4ugmf.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az50.0bg2cysi0dkefjvuua0a0kbd1h.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az51.nhi3in4tbx4ehjtltcwuwbwsua.cx.internal.cloudapp.net>
2020-06-30 19:28:19 +01:00
const COMPLETION_THRESHOLD_TO_EXCEED = 85;
interface Language {
lng: string;
label: string;
rtl?: boolean;
}
const allLanguages: Language[] = [
{ lng: "bg-BG", label: "Български" },
{ lng: "de-DE", label: "Deutsch" },
{ lng: "es-ES", label: "Español" },
{ lng: "ca-ES", label: "Catalan" },
{ lng: "el-GR", label: "Ελληνικά" },
{ lng: "fr-FR", label: "Français" },
{ lng: "id-ID", label: "Bahasa Indonesia" },
{ lng: "it-IT", label: "Italiano" },
{ lng: "hu-HU", label: "Magyar" },
{ lng: "nl-NL", label: "Nederlands" },
{ lng: "nb-NO", label: "Norsk bokmål" },
{ lng: "nn-NO", label: "Norsk nynorsk" },
{ lng: "pl-PL", label: "Polski" },
{ lng: "pt-PT", label: "Português" },
{ lng: "ru-RU", label: "Русский" },
{ lng: "uk-UA", label: "Українська" },
{ lng: "fi-FI", label: "Suomi" },
{ lng: "tr-TR", label: "Türkçe" },
{ lng: "ja-JP", label: "日本語" },
{ lng: "ko-KR", label: "한국어" },
{ lng: "zh-TW", label: "繁體中文" },
{ lng: "zh-CN", label: "简体中文" },
{ lng: "ar-SA", label: "العربية", rtl: true },
{ lng: "he-IL", label: "עברית", rtl: true },
{ lng: "hi-IN", label: "हिन्दी" },
{ lng: "ta-IN", label: "தமிழ்" },
{ lng: "gl-ES", label: "Galego" },
{ lng: "vi-VN", label: "Tiếng Việt" },
{ lng: "ro-RO", label: "Română" },
New Crowdin updates (#2130) * New translations en.json (Japanese) * Auto commit: Calculate translation coverage * New translations en.json (Japanese) * Auto commit: Calculate translation coverage * New translations en.json (Japanese) * Auto commit: Calculate translation coverage * New translations en.json (Japanese) * Auto commit: Calculate translation coverage * New translations en.json (Turkish) * New translations en.json (Japanese) * Auto commit: Calculate translation coverage * New translations en.json (Japanese) * New translations en.json (French) * Auto commit: Calculate translation coverage * New translations en.json (Polish) * Auto commit: Calculate translation coverage * New translations en.json (Italian) * Auto commit: Calculate translation coverage * New translations en.json (French) * New translations en.json (Arabic) * New translations en.json (Arabic) * New translations en.json (Romanian) * Auto commit: Calculate translation coverage * New translations en.json (Indonesian) * Auto commit: Calculate translation coverage * New translations en.json (Chinese Simplified) * New translations en.json (French) * New translations en.json (French) * New translations en.json (Nepali) * Auto commit: Calculate translation coverage * New translations en.json (Norwegian Nynorsk) * Auto commit: Calculate translation coverage * New translations en.json (Norwegian Nynorsk) * Auto commit: Calculate translation coverage * New translations en.json (Polish) * Auto commit: Calculate translation coverage * New translations en.json (Polish) * Auto commit: Calculate translation coverage * New translations en.json (Hindi) * Auto commit: Calculate translation coverage * New translations en.json (Hindi) * Auto commit: Calculate translation coverage * add nepali to language picker * New translations en.json (Swedish) * Auto commit: Calculate translation coverage * New translations en.json (Swedish) * Auto commit: Calculate translation coverage * New translations en.json (Swedish) * Auto commit: Calculate translation coverage * New translations en.json (Swedish) * Auto commit: Calculate translation coverage * New translations en.json (Japanese) * New translations en.json (Swedish) * Auto commit: Calculate translation coverage * New translations en.json (Swedish) * Auto commit: Calculate translation coverage * New translations en.json (Swedish) * Auto commit: Calculate translation coverage * New translations en.json (Swedish) * Auto commit: Calculate translation coverage * New translations en.json (Japanese) * Auto commit: Calculate translation coverage Co-authored-by: Kostas Bariotis <konmpar@gmail.com> Co-authored-by: dwelle <luzar.david@gmail.com>
2020-09-13 20:27:49 +03:00
{ lng: "ne-NP", label: "नेपाली" },
];
export const languages: Language[] = [{ lng: "en", label: "English" }]
Add script to calculate percentage of translation (#1826) * add script to calculate percentage of translation * test translation change * change translation * test * change translation * Calculate percentages of each translation file * test * Calculate percentages of each translation file * change translation * test * test * Calculate percentages of each translation file * test * Calculate percentages of each translation file * fix workflow * test * test again * Calculate percentages of each translation file * Calculate percentages of each translation file * test * refactor * change build logic * fix types, move English first * docs added * test translation file * test * test * test * test * test * test * test * test * test * test * test * test * Calculate percentages of each translation file * let this be the final test please * Calculate percentages of each translation file * test * test * Test * Calculate percentages of each translation file * test * Calculate percentages of each translation file * test * Calculate percentages of each translation file * test * Auto commit: Calculate translation coverage * test * test * test * test * Auto commit: Calculate translation coverage * test * only on master * test * test * Auto commit: Calculate translation coverage * switch to master branch Co-authored-by: i18n automation <runner@fv-az76.2iswp1o5zimezclxzdlwqia2gf.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az129.idlktykl4ure3gqe2lnji05orb.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az76.pjgcdo5npjpenpqz2nk0ztqvxd.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az33.senarqq4ucbulg04aytwntvgah.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az51.icvemaqob4xunfekbtdiz2tu2c.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az78.gikxu4m3dpiulftj3bftpuu3ee.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az121.cqdewbghluceforu5pkvpnveec.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az139.jsbds1i2htye3fh1bzwbe4ugmf.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az50.0bg2cysi0dkefjvuua0a0kbd1h.cx.internal.cloudapp.net> Co-authored-by: i18n automation <runner@fv-az51.nhi3in4tbx4ehjtltcwuwbwsua.cx.internal.cloudapp.net>
2020-06-30 19:28:19 +01:00
.concat(
allLanguages.sort((left, right) => (left.label > right.label ? 1 : -1)),
)
.filter(
(lang) =>
(percentages as Record<string, number>)[lang.lng] >
COMPLETION_THRESHOLD_TO_EXCEED,
);
let currentLanguage = languages[0];
let currentLanguageData = {};
const fallbackLanguage = languages[0];
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.lng}.json`);
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.lng}.json`);
languageDetector.cacheUserLanguage(currentLanguage.lng);
};
export const getLanguage = () => currentLanguage;
const 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];
}
if (typeof data !== "string") {
return undefined;
}
return data;
};
export const t = (path: string, replacement?: { [key: string]: string }) => {
const parts = path.split(".");
let translation =
findPartsForData(currentLanguageData, parts) ||
findPartsForData(fallbackLanguageData, parts);
if (translation === undefined) {
throw new Error(`Can't find translation for ${path}`);
}
if (replacement) {
for (var key in replacement) {
translation = translation.replace(`{{${key}}}`, replacement[key]);
}
}
return translation;
};
const languageDetector = new LanguageDetector();
languageDetector.init({
languageUtils: {
formatLanguageCode: (lng: string) => lng,
isWhitelisted: () => true,
},
checkWhitelist: false,
});