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