2022-02-12 22:49:34 +00:00
|
|
|
import {createI18n} from 'vue-i18n'
|
2021-07-25 13:27:15 +00:00
|
|
|
import langEN from './lang/en.json'
|
2021-06-23 23:24:57 +00:00
|
|
|
|
2022-10-10 19:44:59 +00:00
|
|
|
export const SUPPORTED_LOCALES = {
|
|
|
|
'en': 'English',
|
2021-09-05 11:31:53 +00:00
|
|
|
'de-DE': 'Deutsch',
|
|
|
|
'de-swiss': 'Schwizertütsch',
|
|
|
|
'ru-RU': 'Русский',
|
|
|
|
'fr-FR': 'Français',
|
2021-10-25 19:15:54 +00:00
|
|
|
'vi-VN': 'Tiếng Việt',
|
|
|
|
'it-IT': 'Italiano',
|
2021-10-27 16:27:47 +00:00
|
|
|
'cs-CZ': 'Čeština',
|
2022-02-12 22:49:34 +00:00
|
|
|
'pl-PL': 'Polski',
|
|
|
|
'nl-NL': 'Nederlands',
|
|
|
|
'pt-PT': 'Português',
|
2023-06-21 18:16:03 +00:00
|
|
|
'zh-CN': '中文',
|
2022-12-22 12:05:42 +00:00
|
|
|
'no-NO': 'Norsk Bokmål',
|
2023-06-21 18:12:56 +00:00
|
|
|
'es-ES': 'Español',
|
2023-06-21 18:14:21 +00:00
|
|
|
'da-DK': 'Dansk',
|
2023-06-21 18:15:48 +00:00
|
|
|
'ja-JP': '日本語',
|
2023-08-27 08:28:31 +00:00
|
|
|
'hu-HU': 'Magyar',
|
2023-10-17 16:44:08 +00:00
|
|
|
'ar-SA': 'اَلْعَرَبِيَّةُ',
|
2023-11-21 21:14:15 +00:00
|
|
|
'sl-SI': 'Slovenščina',
|
2024-02-05 20:57:21 +00:00
|
|
|
// IMPORTANT: Also add new languages to useDayjsLanguageSync
|
2022-10-17 11:14:07 +00:00
|
|
|
} as const
|
2022-10-10 19:44:59 +00:00
|
|
|
|
|
|
|
export type SupportedLocale = keyof typeof SUPPORTED_LOCALES
|
|
|
|
|
|
|
|
export const DEFAULT_LANGUAGE: SupportedLocale= 'en'
|
|
|
|
|
|
|
|
export type ISOLanguage = string
|
2021-06-23 23:24:57 +00:00
|
|
|
|
2022-10-17 11:14:07 +00:00
|
|
|
// we load all messages async
|
2022-10-10 19:44:59 +00:00
|
|
|
export const i18n = createI18n({
|
|
|
|
fallbackLocale: DEFAULT_LANGUAGE,
|
2022-12-02 15:16:15 +00:00
|
|
|
legacy: false,
|
2022-10-10 19:44:59 +00:00
|
|
|
messages: {
|
2022-10-17 11:14:07 +00:00
|
|
|
[DEFAULT_LANGUAGE]: langEN,
|
2023-10-20 19:34:11 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2022-10-10 19:44:59 +00:00
|
|
|
} as Record<SupportedLocale, any>,
|
|
|
|
})
|
|
|
|
|
2023-06-12 16:01:56 +00:00
|
|
|
export async function setLanguage(lang: SupportedLocale): Promise<SupportedLocale | undefined> {
|
2022-02-12 22:49:34 +00:00
|
|
|
if (!lang) {
|
2022-10-10 19:44:59 +00:00
|
|
|
throw new Error()
|
2022-02-05 20:41:22 +00:00
|
|
|
}
|
2022-02-12 22:49:34 +00:00
|
|
|
|
2022-10-24 16:59:04 +00:00
|
|
|
// do not change language to the current one
|
2022-12-02 15:16:15 +00:00
|
|
|
if (i18n.global.locale.value === lang) {
|
2022-10-24 16:59:04 +00:00
|
|
|
return
|
2021-06-23 23:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the language hasn't been loaded yet
|
2022-10-24 16:59:04 +00:00
|
|
|
if (!i18n.global.availableLocales.includes(lang)) {
|
2023-08-24 09:37:23 +00:00
|
|
|
try {
|
|
|
|
const messages = await import(`./lang/${lang}.json`)
|
|
|
|
i18n.global.setLocaleMessage(lang, messages.default)
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`Failed to load language ${lang}:`, e)
|
|
|
|
return setLanguage(getBrowserLanguage())
|
|
|
|
}
|
2022-10-24 16:59:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 15:16:15 +00:00
|
|
|
i18n.global.locale.value = lang
|
|
|
|
document.documentElement.lang = lang
|
|
|
|
return lang
|
2021-06-23 23:24:57 +00:00
|
|
|
}
|
|
|
|
|
2023-06-12 16:01:56 +00:00
|
|
|
export function getBrowserLanguage(): SupportedLocale {
|
2022-10-26 13:42:44 +00:00
|
|
|
const browserLanguage = navigator.language
|
2021-06-23 23:24:57 +00:00
|
|
|
|
2022-10-17 11:14:07 +00:00
|
|
|
const language = Object.keys(SUPPORTED_LOCALES).find(langKey => {
|
2022-10-10 19:44:59 +00:00
|
|
|
return langKey === browserLanguage || langKey.startsWith(browserLanguage + '-')
|
2022-10-17 11:14:07 +00:00
|
|
|
}) as SupportedLocale | undefined
|
2021-06-23 23:24:57 +00:00
|
|
|
|
2022-10-10 19:44:59 +00:00
|
|
|
return language || DEFAULT_LANGUAGE
|
2021-06-23 23:24:57 +00:00
|
|
|
}
|