Couple of i18n and homepage fixes
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -4,7 +4,7 @@ import { DateFnsPlugin } from "@/plugins/dateFns";
|
||||
import en from "../i18n/en_US.json";
|
||||
import langs from "../i18n/langs.json";
|
||||
|
||||
const DEFAULT_LOCALE = "en";
|
||||
const DEFAULT_LOCALE = "en_US";
|
||||
|
||||
let language = document.documentElement.getAttribute("lang") as string;
|
||||
language = language || ((window.navigator as any).userLanguage || window.navigator.language).replace(/-/, "_");
|
||||
@@ -13,36 +13,47 @@ export const locale =
|
||||
|
||||
Vue.use(VueI18n);
|
||||
|
||||
console.log(en);
|
||||
console.log(locale);
|
||||
export const i18n = new VueI18n({
|
||||
locale: DEFAULT_LOCALE, // set locale
|
||||
messages: (en as unknown) as VueI18n.LocaleMessages, // set locale messages
|
||||
fallbackLocale: "en",
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
messages: en, // set locale messages
|
||||
fallbackLocale: DEFAULT_LOCALE,
|
||||
});
|
||||
console.log(i18n);
|
||||
|
||||
Vue.use(DateFnsPlugin, { locale });
|
||||
|
||||
const loadedLanguages = ["en"];
|
||||
const loadedLanguages = [DEFAULT_LOCALE];
|
||||
|
||||
function setI18nLanguage(lang: string): string {
|
||||
i18n.locale = lang;
|
||||
return lang;
|
||||
}
|
||||
|
||||
function fileForLanguage(lang: string) {
|
||||
const matches: Record<string, string> = {
|
||||
fr: "fr_FR",
|
||||
en: "en_US",
|
||||
};
|
||||
function fileForLanguage(matches: Record<string, string>, lang: string) {
|
||||
if (Object.prototype.hasOwnProperty.call(matches, lang)) {
|
||||
return matches[lang];
|
||||
}
|
||||
return lang;
|
||||
}
|
||||
|
||||
export async function loadLanguageAsync(lang: string): Promise<string> {
|
||||
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) });
|
||||
|
||||
async function loadLanguageAsync(lang: string): Promise<string> {
|
||||
// If the same language
|
||||
if (i18n.locale === lang) {
|
||||
return Promise.resolve(setI18nLanguage(lang));
|
||||
@@ -53,15 +64,13 @@ export async function loadLanguageAsync(lang: string): Promise<string> {
|
||||
return Promise.resolve(setI18nLanguage(lang));
|
||||
}
|
||||
|
||||
console.log(fileForLanguage(lang));
|
||||
// If the language hasn't been loaded yet
|
||||
return import(/* webpackChunkName: "lang-[request]" */ `@/i18n/${fileForLanguage(lang)}.json`).then(
|
||||
(newMessages: any) => {
|
||||
i18n.setLocaleMessage(lang, newMessages.default);
|
||||
loadedLanguages.push(lang);
|
||||
return setI18nLanguage(lang);
|
||||
}
|
||||
const newMessages = await import(
|
||||
/* webpackChunkName: "lang-[request]" */ `@/i18n/${vueI18NfileForLanguage(lang)}.json`
|
||||
);
|
||||
i18n.setLocaleMessage(lang, newMessages.default);
|
||||
loadedLanguages.push(lang);
|
||||
return setI18nLanguage(lang);
|
||||
}
|
||||
|
||||
loadLanguageAsync(locale);
|
||||
|
||||
Reference in New Issue
Block a user