diff --git a/packages/i18n/src/availableLangs.ts b/packages/i18n/src/availableLangs.ts index 8244d464b..775b23c6c 100644 --- a/packages/i18n/src/availableLangs.ts +++ b/packages/i18n/src/availableLangs.ts @@ -4,8 +4,8 @@ * - English: `en` * - French: `fr` */ - export const appLangs = ['en', 'fr'] as const; - export type appLangsType = typeof appLangs[number]; +export const appLangs = ['en', 'fr', 'de'] as const; +export type appLangsType = typeof appLangs[number]; /** * available languages for mirrors (ISO 639-1): @@ -200,7 +200,7 @@ * - Zhuang, Chuang: `za` * - Zulu: `zu` */ - export const mirrorsLang = [ +export const mirrorsLang = [ 'xx', 'ab', 'aa', @@ -391,9 +391,9 @@ 'yo', 'za', 'zu', - ] as const; +] as const; - export type mirrorsLangsType = Mutable +export type mirrorsLangsType = Mutable type Mutable = { -readonly [Key in keyof T]: T[Key]; diff --git a/packages/i18n/src/loadLocale.ts b/packages/i18n/src/loadLocale.ts index ca9aa03fc..c5b1b5ffa 100644 --- a/packages/i18n/src/loadLocale.ts +++ b/packages/i18n/src/loadLocale.ts @@ -1,4 +1,3 @@ - /* eslint-disable @typescript-eslint/consistent-type-imports */ import type { appLangsType } from '@i18n'; import type { ConfigType } from 'dayjs'; //=> this is just because we need ILocale interface which isn't exported @@ -17,12 +16,26 @@ export async function importLocale(locale: appLangsType) { const quasarLangs = import.meta.glob('../../../node_modules/quasar/lang/*.mjs'); const dayJSLangs = import.meta.glob('../../../node_modules/dayjs/esm/locale/*.js'); - const quasarSpecific = locale === 'en' ? 'en-US' : locale; + let quasar: { default: QuasarLanguage } | undefined; + let dayjs: { default: ILocale } | undefined; + + try { + quasar = await quasarLangs[ `../../../node_modules/quasar/lang/${locale}.mjs` ]() as unknown as { default: QuasarLanguage }; + } catch(err) { + quasar = await quasarLangs[ '../../../node_modules/quasar/lang/en-US.mjs' ]() as unknown as { default: QuasarLanguage }; + } + + try { + dayjs = await dayJSLangs[ `../../../node_modules/dayjs/esm/locale/${ locale }.js` ]() as { default: ILocale }; + } catch(err) { + dayjs = await dayJSLangs[ '../../../node_modules/dayjs/esm/locale/en.js' ]() as { default: ILocale }; + } + + if(!dayjs || !quasar) throw Error('couldn\'t load locales'); - const quasar = await quasarLangs[ `../../../node_modules/quasar/lang/${quasarSpecific}.mjs` ]() as unknown as { default: QuasarLanguage }; - const dayjs = await dayJSLangs[ `../../../node_modules/dayjs/esm/locale/${ locale }.js` ]() as { default: ILocale }; return { dayjs: dayjs.default, quasar: quasar.default, }; + } diff --git a/packages/renderer/src/locales/index.ts b/packages/renderer/src/locales/index.ts index a6fdeb719..30a6ef011 100644 --- a/packages/renderer/src/locales/index.ts +++ b/packages/renderer/src/locales/index.ts @@ -21,18 +21,16 @@ export function setupI18n(options:I18nOptions<{ message: MessageSchema }, appLan return i18n; } -export function setI18nLanguage(i18n:I18n, locale:appLangsType) { +export async function setI18nLanguage(i18n:I18n, locale:appLangsType) { - loadLocale(locale).then(messages => { - /** vue-i18n */ - i18n.global.locale = locale; - i18n.global.setLocaleMessage(locale, messages); + const messages = await loadLocale(locale); + i18n.global.locale = locale; + i18n.global.setLocaleMessage(locale, messages); + + const imported = await importLocale(locale); + Quasar.lang.set(imported.quasar); + dayjs.locale(imported.dayjs); + dayjs.extend(dayjsrelative); + dayjs.extend(dayjslocalizedformat); - importLocale(locale).then(imp => { - Quasar.lang.set(imp.quasar); - dayjs.locale(imp.dayjs); - dayjs.extend(dayjsrelative); - dayjs.extend(dayjslocalizedformat); - }); - }); }