diff --git a/adminforth/spa/package.json b/adminforth/spa/package.json index 4dd52b77..231060c1 100644 --- a/adminforth/spa/package.json +++ b/adminforth/spa/package.json @@ -10,7 +10,7 @@ "build-only": "vite build", "type-check": "vue-tsc --build --force", "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore", - "i18n:extract": "echo {} > i18n-empty.json && vue-i18n-extract report --vueFiles \"./src/**/*.{js,vue}\" --output ./i18n-messages.json --languageFiles \"i18n-empty.json\" --add" + "i18n:extract": "echo {} > i18n-empty.json && vue-i18n-extract report --vueFiles \"./src/**/*.{js,vue,ts}\" --output ./i18n-messages.json --languageFiles \"i18n-empty.json\" --add" }, "dependencies": { "@iconify-prerendered/vue-flag": "^0.28.1748584105", diff --git a/adminforth/spa/src/i18n.ts b/adminforth/spa/src/i18n.ts index 7dd6a821..c59321d0 100644 --- a/adminforth/spa/src/i18n.ts +++ b/adminforth/spa/src/i18n.ts @@ -7,7 +7,7 @@ function slavicPluralRule(choice: number, choicesLength: number, orgRule: any) { if (choice === 0) { return 0 } - + const teen = choice > 10 && choice < 20 const endsWithOne = choice % 10 === 1 @@ -21,6 +21,8 @@ function slavicPluralRule(choice: number, choicesLength: number, orgRule: any) { return choicesLength < 4 ? 2 : 3 } +export let i18nInstance: ReturnType | null = null + export function initI18n(app: ReturnType) { const i18n = createI18n({ legacy: false, @@ -48,7 +50,7 @@ export function initI18n(app: ReturnType) { return key + ' '; }, }); - app.use(i18n); + i18nInstance = i18n return i18n } \ No newline at end of file diff --git a/adminforth/spa/src/main.ts b/adminforth/spa/src/main.ts index 5f4b830e..ba80262a 100644 --- a/adminforth/spa/src/main.ts +++ b/adminforth/spa/src/main.ts @@ -14,7 +14,7 @@ app.use(createPinia()) app.use(router) // get access to i18n instance outside components -window.i18n = initI18n(app); +initI18n(app); /* IMPORTANT:ADMINFORTH CUSTOM USES */ diff --git a/adminforth/spa/src/utils.ts b/adminforth/spa/src/utils.ts index eac86595..9ce61864 100644 --- a/adminforth/spa/src/utils.ts +++ b/adminforth/spa/src/utils.ts @@ -9,6 +9,7 @@ import adminforth from './adminforth'; import sanitizeHtml from 'sanitize-html' import debounce from 'debounce'; import type { AdminForthResourceColumnInputCommon, Predicate } from '@/types/Common'; +import { i18nInstance } from './i18n' const LS_LANG_KEY = `afLanguage`; const MAX_CONSECUTIVE_EMPTY_RESULTS = 2; @@ -19,6 +20,7 @@ export async function callApi({path, method, body, headers}: { body?: any headers?: Record }): Promise { + const t = i18nInstance?.global.t || ((s: string) => s) const options = { method, headers: { @@ -42,11 +44,11 @@ export async function callApi({path, method, body, headers}: { // if it is internal error, say to user if (e instanceof TypeError && e.message === 'Failed to fetch') { // this is a network error - adminforth.alert({variant:'danger', message: window.i18n?.global?.t('Network error, please check your Internet connection and try again'),}) + adminforth.alert({variant:'danger', message: t('Network error, please check your Internet connection and try again'),}) return null; } - adminforth.alert({variant:'danger', message: window.i18n?.global?.t('Something went wrong, please try again later'),}) + adminforth.alert({variant:'danger', message: t('Something went wrong, please try again later'),}) console.error(`error in callApi ${path}`, e); } }