Skip to content

Commit bd8b122

Browse files
committed
fix: add translations to the callApi error messages
1 parent 69f04e0 commit bd8b122

File tree

4 files changed

+10
-11
lines changed

4 files changed

+10
-11
lines changed

adminforth/spa/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"build-only": "vite build",
1111
"type-check": "vue-tsc --build --force",
1212
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
13-
"i18n:extract": "echo {} > i18n-empty.json && vue-i18n-extract report --vueFiles \"./src/**/*.{js,vue}\" --output ./i18n-messages.json --languageFiles \"i18n-empty.json\" --add"
13+
"i18n:extract": "echo {} > i18n-empty.json && vue-i18n-extract report --vueFiles \"./src/**/*.{js,vue,ts}\" --output ./i18n-messages.json --languageFiles \"i18n-empty.json\" --add"
1414
},
1515
"dependencies": {
1616
"@iconify-prerendered/vue-flag": "^0.28.1748584105",

adminforth/spa/src/i18n.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function slavicPluralRule(choice: number, choicesLength: number, orgRule: any) {
77
if (choice === 0) {
88
return 0
99
}
10-
10+
1111
const teen = choice > 10 && choice < 20
1212
const endsWithOne = choice % 10 === 1
1313

@@ -21,6 +21,8 @@ function slavicPluralRule(choice: number, choicesLength: number, orgRule: any) {
2121
return choicesLength < 4 ? 2 : 3
2222
}
2323

24+
export let i18nInstance: ReturnType<typeof createI18n> | null = null
25+
2426
export function initI18n(app: ReturnType<typeof createApp>) {
2527
const i18n = createI18n({
2628
legacy: false,
@@ -48,7 +50,7 @@ export function initI18n(app: ReturnType<typeof createApp>) {
4850
return key + ' ';
4951
},
5052
});
51-
5253
app.use(i18n);
54+
i18nInstance = i18n
5355
return i18n
5456
}

adminforth/spa/src/main.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ import { createPinia } from 'pinia'
66
import App from './App.vue'
77
import router from './router'
88
import { initI18n } from './i18n'
9-
declare global {
10-
interface Window {
11-
i18n: any;
12-
}
13-
}
149

1510
export const app: ReturnType<typeof createApp> = createApp(App)
1611
/* IMPORTANT:ADMINFORTH COMPONENT REGISTRATIONS */
@@ -19,7 +14,7 @@ app.use(createPinia())
1914
app.use(router)
2015

2116
// get access to i18n instance outside components
22-
window.i18n = initI18n(app);
17+
initI18n(app);
2318

2419

2520
/* IMPORTANT:ADMINFORTH CUSTOM USES */

adminforth/spa/src/utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import adminforth from './adminforth';
99
import sanitizeHtml from 'sanitize-html'
1010
import debounce from 'debounce';
1111
import type { AdminForthResourceColumnInputCommon, Predicate } from '@/types/Common';
12+
import { i18nInstance } from './i18n'
1213

1314
const LS_LANG_KEY = `afLanguage`;
1415
const MAX_CONSECUTIVE_EMPTY_RESULTS = 2;
@@ -19,6 +20,7 @@ export async function callApi({path, method, body, headers}: {
1920
body?: any
2021
headers?: Record<string, string>
2122
}): Promise<any> {
23+
const t = i18nInstance?.global.t || ((s: string) => s)
2224
const options = {
2325
method,
2426
headers: {
@@ -42,11 +44,11 @@ export async function callApi({path, method, body, headers}: {
4244
// if it is internal error, say to user
4345
if (e instanceof TypeError && e.message === 'Failed to fetch') {
4446
// this is a network error
45-
adminforth.alert({variant:'danger', message: window.i18n?.global?.t('Network error, please check your Internet connection and try again'),})
47+
adminforth.alert({variant:'danger', message: t('Network error, please check your Internet connection and try again'),})
4648
return null;
4749
}
4850

49-
adminforth.alert({variant:'danger', message: window.i18n?.global?.t('Something went wrong, please try again later'),})
51+
adminforth.alert({variant:'danger', message: t('Something went wrong, please try again later'),})
5052
console.error(`error in callApi ${path}`, e);
5153
}
5254
}

0 commit comments

Comments
 (0)