2020-02-18 08:57:00 +01:00
|
|
|
import Vue from "vue";
|
|
|
|
import VueI18n from "vue-i18n";
|
2020-10-13 20:39:59 +02:00
|
|
|
import { DateFnsPlugin } from "@/plugins/dateFns";
|
|
|
|
import en from "../i18n/en_US.json";
|
|
|
|
import langs from "../i18n/langs.json";
|
2020-11-30 17:57:08 +01:00
|
|
|
import { getLocaleData } from "./auth";
|
2021-03-02 16:11:00 +01:00
|
|
|
import pluralizationRules from "../i18n/pluralRules";
|
2020-10-13 20:39:59 +02:00
|
|
|
|
2020-10-14 12:43:52 +02:00
|
|
|
const DEFAULT_LOCALE = "en_US";
|
2019-10-13 13:56:24 +02:00
|
|
|
|
2020-11-27 19:27:44 +01:00
|
|
|
let language =
|
2020-11-30 17:57:08 +01:00
|
|
|
getLocaleData() || (document.documentElement.getAttribute("lang") as string);
|
|
|
|
|
2020-11-27 19:27:44 +01:00
|
|
|
language =
|
|
|
|
language ||
|
2020-11-30 10:24:11 +01:00
|
|
|
((window.navigator as any).userLanguage || window.navigator.language).replace(
|
|
|
|
/-/,
|
|
|
|
"_"
|
|
|
|
);
|
2020-11-30 17:57:08 +01:00
|
|
|
|
2020-10-13 20:39:59 +02:00
|
|
|
export const locale =
|
2020-11-27 19:27:44 +01:00
|
|
|
language && Object.prototype.hasOwnProperty.call(langs, language)
|
|
|
|
? language
|
|
|
|
: language.split("-")[0];
|
2019-10-13 13:56:24 +02:00
|
|
|
|
|
|
|
Vue.use(VueI18n);
|
|
|
|
|
|
|
|
export const i18n = new VueI18n({
|
2020-10-13 20:39:59 +02:00
|
|
|
locale: DEFAULT_LOCALE, // set locale
|
2020-10-14 12:43:52 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
messages: en, // set locale messages
|
|
|
|
fallbackLocale: DEFAULT_LOCALE,
|
2020-11-30 17:57:08 +01:00
|
|
|
formatFallbackMessages: true,
|
2021-03-02 16:11:00 +01:00
|
|
|
pluralizationRules,
|
2019-10-13 13:56:24 +02:00
|
|
|
});
|
2020-10-13 20:39:59 +02:00
|
|
|
|
2020-10-14 12:43:52 +02:00
|
|
|
const loadedLanguages = [DEFAULT_LOCALE];
|
2020-10-13 20:39:59 +02:00
|
|
|
|
|
|
|
function setI18nLanguage(lang: string): string {
|
|
|
|
i18n.locale = lang;
|
2021-07-27 19:47:54 +02:00
|
|
|
setLanguageInDOM(lang);
|
2020-10-13 20:39:59 +02:00
|
|
|
return lang;
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:47:54 +02:00
|
|
|
function setLanguageInDOM(lang: string): void {
|
2021-08-02 19:26:44 +02:00
|
|
|
const fixedLang = lang.replace(/_/g, "-");
|
2021-07-27 19:47:54 +02:00
|
|
|
const html = document.documentElement;
|
|
|
|
const documentLang = html.getAttribute("lang");
|
|
|
|
if (documentLang !== fixedLang) {
|
|
|
|
html.setAttribute("lang", fixedLang);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-14 12:43:52 +02:00
|
|
|
function fileForLanguage(matches: Record<string, string>, lang: string) {
|
2020-10-13 20:39:59 +02:00
|
|
|
if (Object.prototype.hasOwnProperty.call(matches, lang)) {
|
|
|
|
return matches[lang];
|
|
|
|
}
|
|
|
|
return lang;
|
|
|
|
}
|
|
|
|
|
2020-10-14 12:43:52 +02:00
|
|
|
function vueI18NfileForLanguage(lang: string) {
|
|
|
|
const matches: Record<string, string> = {
|
|
|
|
fr: "fr_FR",
|
|
|
|
en: "en_US",
|
|
|
|
};
|
|
|
|
return fileForLanguage(matches, lang);
|
|
|
|
}
|
|
|
|
|
|
|
|
function dateFnsfileForLanguage(lang: string) {
|
|
|
|
const matches: Record<string, string> = {
|
|
|
|
en_US: "en-US",
|
|
|
|
en: "en-US",
|
|
|
|
};
|
|
|
|
return fileForLanguage(matches, lang);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vue.use(DateFnsPlugin, { locale: dateFnsfileForLanguage(locale) });
|
|
|
|
|
2020-10-28 18:58:43 +01:00
|
|
|
export async function loadLanguageAsync(lang: string): Promise<string> {
|
2020-10-13 20:39:59 +02:00
|
|
|
// If the same language
|
|
|
|
if (i18n.locale === lang) {
|
|
|
|
return Promise.resolve(setI18nLanguage(lang));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the language was already loaded
|
|
|
|
if (loadedLanguages.includes(lang)) {
|
|
|
|
return Promise.resolve(setI18nLanguage(lang));
|
|
|
|
}
|
|
|
|
// If the language hasn't been loaded yet
|
2020-10-14 12:43:52 +02:00
|
|
|
const newMessages = await import(
|
2020-11-30 10:24:11 +01:00
|
|
|
/* webpackChunkName: "lang-[request]" */ `@/i18n/${vueI18NfileForLanguage(
|
|
|
|
lang
|
|
|
|
)}.json`
|
2020-10-13 20:39:59 +02:00
|
|
|
);
|
2020-10-14 12:43:52 +02:00
|
|
|
i18n.setLocaleMessage(lang, newMessages.default);
|
|
|
|
loadedLanguages.push(lang);
|
|
|
|
return setI18nLanguage(lang);
|
2020-10-13 20:39:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
loadLanguageAsync(locale);
|
2020-09-29 09:53:48 +02:00
|
|
|
|
|
|
|
export function formatList(list: string[]): string {
|
|
|
|
if (window.Intl && Intl.ListFormat) {
|
2020-11-30 10:24:11 +01:00
|
|
|
const formatter = new Intl.ListFormat(undefined, {
|
|
|
|
style: "long",
|
|
|
|
type: "conjunction",
|
|
|
|
});
|
2020-09-29 09:53:48 +02:00
|
|
|
return formatter.format(list);
|
|
|
|
}
|
|
|
|
return list.join(",");
|
|
|
|
}
|