I want to use a vue-i18n to translate error codes into error messages. To show this method I created a function a ts file looking like this.
import {ElMessage} from "element-plus";
import {useI18n} from "vue-i18n";
export function defaultErrorHandler(error) {
const t = useI18n({
useScope: 'global',
inheritLocale: true
}).t;
if (error.response == undefined) {
ElMessage({
message: '...',
type: 'error',
})
} else if (error.response.status == 403) {
store.commit('logout')
doAuthRedirect()
} else if (error.response.status == 400 || error.response.data != undefined) {
ElMessage({
message: t(error.response.data, t("unknown response error from server, notify administrator")),
type: 'error',
});
} else {
ElMessage({
message: '...',
type: 'error',
})
}
}
Result in:
Uncaught (in promise) SyntaxError: Must be called at the top of a `setup` function
createCompileError message-compiler.esm-bundler.js:32
createI18nError vue-i18n.esm-bundler.js:66
useI18n vue-i18n.esm-bundler.js:2037
defaultErrorHandler index.ts:6
It is not in context of a vue component.
But how do i obtain an instance of i18n ?
This the i18n setup code from main.ts
const i18n = createI18n({
legacy: false,
locale: 'de',
messages: {
'de': messagesDe
}
});
const app = createApp(App)
app.use(store)
app.use(router)
app.use(i18n)
Thank you for your help