revite/src/context/Locale.tsx

344 lines
10 KiB
TypeScript
Raw Normal View History

import dayJS from "dayjs";
2021-07-05 06:23:23 -04:00
import calendar from "dayjs/plugin/calendar";
import format from "dayjs/plugin/localizedFormat";
import update from "dayjs/plugin/updateLocale";
2021-06-23 09:52:33 -04:00
import defaultsDeep from "lodash.defaultsdeep";
2021-12-11 06:56:33 -05:00
import { observer } from "mobx-react-lite";
2021-07-05 06:23:23 -04:00
import { IntlProvider } from "preact-i18n";
2021-08-05 09:47:00 -04:00
import { useCallback, useEffect, useState } from "preact/hooks";
2021-07-05 06:23:23 -04:00
2021-12-11 06:56:33 -05:00
import { useApplicationState } from "../mobx/State";
2021-07-05 06:23:23 -04:00
import definition from "../../external/lang/en.json";
2021-07-06 14:29:27 -04:00
export const dayjs = dayJS;
dayjs.extend(calendar);
dayjs.extend(format);
dayjs.extend(update);
export enum Language {
2021-07-05 06:25:20 -04:00
ENGLISH = "en",
ARABIC = "ar",
AZERBAIJANI = "az",
BELARUSIAN = "be",
2021-08-15 09:16:36 -04:00
BULGARIAN = "bg",
BENGALI = "bn",
CATALONIAN = "ca",
2021-07-05 06:25:20 -04:00
CZECH = "cs",
DANISH = "da",
2021-07-05 06:25:20 -04:00
GERMAN = "de",
GREEK = "el",
2021-07-05 06:25:20 -04:00
SPANISH = "es",
ESTONIAN = "et",
2021-07-05 06:25:20 -04:00
FINNISH = "fi",
FILIPINO = "fil",
2021-07-05 06:25:20 -04:00
FRENCH = "fr",
IRISH = "ga",
2021-07-05 06:25:20 -04:00
HINDI = "hi",
CROATIAN = "hr",
HUNGARIAN = "hu",
INDONESIAN = "id",
ITALIAN = "it",
JAPANESE = "ja",
KOREAN = "ko",
LUXEMBOURGISH = "lb",
2021-07-05 06:25:20 -04:00
LITHUANIAN = "lt",
MACEDONIAN = "mk",
MALAY = "ms",
NORWEGIAN_BOKMAL = "nb_NO",
2021-07-05 06:25:20 -04:00
DUTCH = "nl",
2021-12-07 11:44:17 -05:00
PERSIAN = "fa",
2021-07-05 06:25:20 -04:00
POLISH = "pl",
PORTUGUESE_BRAZIL = "pt_BR",
2021-12-07 11:44:17 -05:00
PORTUGUESE_PORTUGAL = "pt_PT",
2021-07-05 06:25:20 -04:00
ROMANIAN = "ro",
RUSSIAN = "ru",
SLOVAK = "sk",
SLOVENIAN = "sl",
2021-07-05 06:25:20 -04:00
SERBIAN = "sr",
2021-12-07 11:44:17 -05:00
SINHALESE = "si",
2021-07-05 06:25:20 -04:00
SWEDISH = "sv",
TAMIL = "ta",
2021-08-15 09:16:36 -04:00
THAI = "th",
2021-07-05 06:25:20 -04:00
TURKISH = "tr",
UKRANIAN = "uk",
VIETNAMESE = "vi",
2021-07-05 06:25:20 -04:00
CHINESE_SIMPLIFIED = "zh_Hans",
2021-08-15 09:16:36 -04:00
TOKIPONA = "tokipona",
ESPERANTO = "esperanto",
2021-08-15 09:16:36 -04:00
2021-07-05 06:25:20 -04:00
OWO = "owo",
PIRATE = "pr",
BOTTOM = "bottom",
2021-08-16 13:16:49 -04:00
LEET = "leet",
2021-07-05 06:25:20 -04:00
PIGLATIN = "piglatin",
2021-09-04 10:50:58 -04:00
ENCHANTMENT_TABLE = "enchantment",
}
export interface LanguageEntry {
2021-07-05 06:25:20 -04:00
display: string;
emoji: string;
i18n: string;
dayjs?: string;
rtl?: boolean;
2021-08-04 07:31:51 -04:00
cat?: "const" | "alt";
}
export const Languages: { [key in Language]: LanguageEntry } = {
2021-07-05 06:25:20 -04:00
en: {
display: "English (Traditional)",
emoji: "🇬🇧",
i18n: "en",
dayjs: "en-gb",
},
ar: { display: "عربي", emoji: "🇸🇦", i18n: "ar", rtl: true },
az: { display: "Azərbaycan dili", emoji: "🇦🇿", i18n: "az" },
be: { display: "беларуская", emoji: "🇧🇾", i18n: "be" },
2021-08-15 09:16:36 -04:00
bg: { display: "български", emoji: "🇧🇬", i18n: "bg" },
bn: { display: "বাংলা", emoji: "🇧🇩", i18n: "bn" },
ca: { display: "Català", emoji: "🇪🇸", i18n: "ca" },
2021-07-05 06:25:20 -04:00
cs: { display: "Čeština", emoji: "🇨🇿", i18n: "cs" },
da: { display: "Danskers", emoji: "🇩🇰", i18n: "da" },
2021-07-05 06:25:20 -04:00
de: { display: "Deutsch", emoji: "🇩🇪", i18n: "de" },
el: { display: "Ελληνικά", emoji: "🇬🇷", i18n: "el" },
2021-07-05 06:25:20 -04:00
es: { display: "Español", emoji: "🇪🇸", i18n: "es" },
et: { display: "eesti", emoji: "🇪🇪", i18n: "et" },
fi: { display: "suomi", emoji: "🇫🇮", i18n: "fi" },
fil: { display: "Pilipino", emoji: "🇵🇭", i18n: "fil", dayjs: "tl-ph" },
2021-07-05 06:25:20 -04:00
fr: { display: "Français", emoji: "🇫🇷", i18n: "fr" },
ga: { display: "Gaeilge", emoji: "🇮🇪", i18n: "ga" },
2021-07-05 06:25:20 -04:00
hi: { display: "हिन्दी", emoji: "🇮🇳", i18n: "hi" },
hr: { display: "Hrvatski", emoji: "🇭🇷", i18n: "hr" },
hu: { display: "magyar", emoji: "🇭🇺", i18n: "hu" },
2021-07-05 06:25:20 -04:00
id: { display: "bahasa Indonesia", emoji: "🇮🇩", i18n: "id" },
it: { display: "Italiano", emoji: "🇮🇹", i18n: "it" },
2021-09-06 11:10:11 -04:00
ja: { display: "日本語", emoji: "🇯🇵", i18n: "ja" },
2021-08-30 10:01:15 -04:00
ko: { display: "한국어", emoji: "🇰🇷", i18n: "ko" },
lb: { display: "Lëtzebuergesch", emoji: "🇱🇺", i18n: "lb" },
2021-07-05 06:25:20 -04:00
lt: { display: "Lietuvių", emoji: "🇱🇹", i18n: "lt" },
mk: { display: "Македонски", emoji: "🇲🇰", i18n: "mk" },
ms: { display: "Melayu", emoji: "🇲🇾", i18n: "ms" },
nb_NO: { display: "Norsk bokmål", emoji: "🇳🇴", i18n: "nb_NO", dayjs: "nb" },
2021-07-05 06:25:20 -04:00
nl: { display: "Nederlands", emoji: "🇳🇱", i18n: "nl" },
2021-12-07 11:44:17 -05:00
fa: { display: "فارسی", emoji: "🇮🇷", i18n: "fa" },
2021-07-05 06:25:20 -04:00
pl: { display: "Polski", emoji: "🇵🇱", i18n: "pl" },
pt_BR: {
display: "Português (do Brasil)",
emoji: "🇧🇷",
i18n: "pt_BR",
dayjs: "pt-br",
},
2021-12-07 11:44:17 -05:00
pt_PT: {
display: "Português (Portugal)",
emoji: "🇵🇹",
i18n: "pt_PT",
dayjs: "pt",
},
2021-07-05 06:25:20 -04:00
ro: { display: "Română", emoji: "🇷🇴", i18n: "ro" },
ru: { display: "Русский", emoji: "🇷🇺", i18n: "ru" },
sk: { display: "Slovensky", emoji: "🇸🇰", i18n: "sk" },
sl: { display: "Slovenščina", emoji: "🇸🇮", i18n: "sl" },
2021-07-05 06:25:20 -04:00
sr: { display: "Српски", emoji: "🇷🇸", i18n: "sr" },
2021-12-07 11:44:17 -05:00
si: { display: "සිංහල", emoji: "🇱🇰", i18n: "si" },
2021-07-05 06:25:20 -04:00
sv: { display: "Svenska", emoji: "🇸🇪", i18n: "sv" },
ta: { display: "தமிழ்", emoji: "🇮🇳", i18n: "ta" },
2021-08-15 09:16:36 -04:00
th: { display: "ไทย", emoji: "🇹🇭", i18n: "th" },
2021-07-05 06:25:20 -04:00
tr: { display: "Türkçe", emoji: "🇹🇷", i18n: "tr" },
uk: { display: "Українська", emoji: "🇺🇦", i18n: "uk" },
vi: { display: "Tiếng Việt", emoji: "🇻🇳", i18n: "vi" },
2021-07-05 06:25:20 -04:00
zh_Hans: {
display: "中文 (简体)",
emoji: "🇨🇳",
i18n: "zh_Hans",
dayjs: "zh",
},
2021-08-04 07:31:51 -04:00
tokipona: {
display: "Toki Pona",
emoji: "🙂",
i18n: "tokipona",
dayjs: "en-gb",
cat: "const",
},
esperanto: {
display: "Esperanto",
emoji: "EO",
i18n: "eo",
dayjs: "en-gb",
cat: "const",
},
2021-08-04 07:31:51 -04:00
2021-07-05 06:25:20 -04:00
owo: {
display: "OwO",
2021-09-04 10:50:58 -04:00
emoji: "😸",
2021-07-05 06:25:20 -04:00
i18n: "owo",
dayjs: "en-gb",
2021-08-04 07:31:51 -04:00
cat: "alt",
2021-07-05 06:25:20 -04:00
},
pr: {
display: "Pirate",
emoji: "🏴‍☠️",
i18n: "pr",
dayjs: "en-gb",
2021-08-04 07:31:51 -04:00
cat: "alt",
2021-07-05 06:25:20 -04:00
},
bottom: {
display: "Bottom",
emoji: "🥺",
i18n: "bottom",
dayjs: "en-gb",
2021-08-04 07:31:51 -04:00
cat: "alt",
2021-07-05 06:25:20 -04:00
},
2021-08-16 13:16:49 -04:00
leet: {
display: "1337",
emoji: "💾",
i18n: "leet",
dayjs: "en-gb",
cat: "alt",
},
2021-09-04 10:50:58 -04:00
enchantment: {
display: "Enchantment Table",
emoji: "🪄",
i18n: "enchantment",
dayjs: "en-gb",
cat: "alt",
},
2021-07-05 06:25:20 -04:00
piglatin: {
display: "Pig Latin",
emoji: "🐖",
i18n: "piglatin",
dayjs: "en-gb",
2021-08-04 07:31:51 -04:00
cat: "alt",
2021-07-05 06:25:20 -04:00
},
};
2021-06-18 10:18:10 -04:00
interface Props {
2021-07-05 06:25:20 -04:00
children: JSX.Element | JSX.Element[];
2021-06-18 10:18:10 -04:00
}
export interface Dictionary {
dayjs?: {
defaults?: {
twelvehour?: "yes" | "no";
separator?: string;
date?: "traditional" | "simplified" | "ISO8601";
};
timeFormat?: string;
};
[key: string]:
| Record<string, Omit<Dictionary, "dayjs">>
| string
| undefined;
}
2021-12-11 06:56:33 -05:00
export default observer(({ children }: Props) => {
const locale = useApplicationState().locale;
const [definitions, setDefinition] = useState<Dictionary>(
2021-08-05 09:47:00 -04:00
definition as Dictionary,
);
2021-12-11 06:56:33 -05:00
const lang = locale.getLanguage();
const source = Languages[lang];
2021-07-05 06:25:20 -04:00
2021-08-05 09:47:00 -04:00
const loadLanguage = useCallback(
(locale: string) => {
if (locale === "en") {
// If English, make sure to restore everything to defaults.
// Use what we already have.
const defn = transformLanguage(definition as Dictionary);
2021-07-05 06:25:20 -04:00
setDefinition(defn);
2021-08-05 09:47:00 -04:00
dayjs.locale("en");
dayjs.updateLocale("en", { calendar: defn.dayjs });
return;
}
2021-12-11 06:56:33 -05:00
import(`../../external/lang/${source.i18n}.json`).then(
2021-08-05 09:47:00 -04:00
async (lang_file) => {
// Transform the definitions data.
const defn = transformLanguage(lang_file.default);
// Determine and load dayjs locales.
2021-12-11 06:56:33 -05:00
const target = source.dayjs ?? source.i18n;
2021-08-05 09:47:00 -04:00
const dayjs_locale = await import(
`../../node_modules/dayjs/esm/locale/${target}.js`
);
// Load dayjs locales.
dayjs.locale(target, dayjs_locale.default);
if (defn.dayjs) {
// Override dayjs calendar locales with our own.
dayjs.updateLocale(target, { calendar: defn.dayjs });
}
// Apply definition to app.
setDefinition(defn);
},
);
},
2021-12-11 06:56:33 -05:00
[source.dayjs, source.i18n],
2021-08-05 09:47:00 -04:00
);
2021-12-11 06:56:33 -05:00
useEffect(() => loadLanguage(lang), [lang, source, loadLanguage]);
2021-07-05 06:25:20 -04:00
useEffect(() => {
// Apply RTL language format.
2021-12-11 06:56:33 -05:00
document.body.style.direction = source.rtl ? "rtl" : "";
}, [source.rtl]);
return <IntlProvider definition={definitions}>{children}</IntlProvider>;
});
/**
* Apply defaults and process dayjs entries for a langauge.
* @param source Dictionary definition to transform
* @returns Transformed dictionary definition
*/
function transformLanguage(source: Dictionary) {
// Fallback untranslated strings to English (UK)
const obj = defaultsDeep(source, definition);
// Take relevant objects out, dayjs and defaults
// should exist given we just took defaults above.
const { dayjs } = obj;
const { defaults } = dayjs;
// Determine whether we are using 12-hour clock.
const twelvehour = defaults?.twelvehour
? defaults.twelvehour === "yes"
: false;
// Determine what date separator we are using.
const separator: string = defaults?.date_separator ?? "/";
// Determine what date format we are using.
const date: "traditional" | "simplified" | "ISO8601" =
defaults?.date_format ?? "traditional";
// Available date formats.
const DATE_FORMATS = {
traditional: `DD${separator}MM${separator}YYYY`,
simplified: `MM${separator}DD${separator}YYYY`,
ISO8601: "YYYY-MM-DD",
};
2021-07-05 06:25:20 -04:00
2021-12-11 06:56:33 -05:00
// Replace data in dayjs object, make sure to provide fallbacks.
dayjs["sameElse"] = DATE_FORMATS[date] ?? DATE_FORMATS.traditional;
dayjs["timeFormat"] = twelvehour ? "hh:mm A" : "HH:mm";
2021-12-11 06:56:33 -05:00
// Replace {{time}} format string in dayjs strings with the time format.
Object.keys(dayjs)
.filter((k) => typeof dayjs[k] === "string")
.forEach(
(k) =>
(dayjs[k] = dayjs[k].replace(/{{time}}/g, dayjs["timeFormat"])),
);
return obj;
}