Skip to content

Commit

Permalink
fix(i18n): make sure english is used as a fallback in quasar and dayjs
Browse files Browse the repository at this point in the history
  • Loading branch information
JiPaix committed Dec 1, 2022
1 parent 7e5d829 commit f511a2e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
10 changes: 5 additions & 5 deletions packages/i18n/src/availableLangs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -200,7 +200,7 @@
* - Zhuang, Chuang: `za`
* - Zulu: `zu`
*/
export const mirrorsLang = [
export const mirrorsLang = [
'xx',
'ab',
'aa',
Expand Down Expand Up @@ -391,9 +391,9 @@
'yo',
'za',
'zu',
] as const;
] as const;

export type mirrorsLangsType = Mutable<typeof mirrorsLang[number]>
export type mirrorsLangsType = Mutable<typeof mirrorsLang[number]>

type Mutable<T> = {
-readonly [Key in keyof T]: T[Key];
Expand Down
21 changes: 17 additions & 4 deletions packages/i18n/src/loadLocale.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
};

}
22 changes: 10 additions & 12 deletions packages/renderer/src/locales/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@ export function setupI18n(options:I18nOptions<{ message: MessageSchema }, appLan
return i18n;
}

export function setI18nLanguage(i18n:I18n<unknown, unknown, unknown, string, true>, locale:appLangsType) {
export async function setI18nLanguage(i18n:I18n<unknown, unknown, unknown, string, true>, 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);
});
});
}

0 comments on commit f511a2e

Please sign in to comment.