Vue 3 vue-i18n. How to use $t(t) outside the application (.vue file)? Synthesis API
P粉032649413
P粉032649413 2023-08-26 10:24:05
0
1
370

I want to format my datetime outside the component.

function transformDatetime(config, value) { if (value) { return $d(new Date(value), config); } return $t('-'); } 

I'm trying to get $t from the application instance. But it only works in component context, just like useI18n.

import { getCurrentInstance } from 'vue' export default function useGlobal() { const instance = getCurrentInstance() if (!instance) return return instance.appContext.config.globalProperties } 


P粉032649413
P粉032649413

reply all (1)
P粉143640496

I found the solution. Just import youri18ninto a file outside your application anduse i18n.global.t

import { createI18n } from "vue-i18n" export const i18n = createI18n({ legacy: false, locale: 'ja', fallbackLocale: 'en', messages, })
import { i18n } from '@/i18n' const { t: $t, d: $d, n: $n } = i18n.global const expample = $t('some-text')
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!