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
413
<p>I want to format my datetime outside the component. </p> <pre class="brush:js;toolbar:false;">function transformDatetime(config, value) { if (value) { return $d(new Date(value), config); } return $t('-'); } </pre> <p>I'm trying to get $t from the application instance. But it only works in component context, just like useI18n. </p> <pre class="brush:js;toolbar:false;">import { getCurrentInstance } from 'vue' export default function useGlobal() { const instance = getCurrentInstance() if (!instance) return return instance.appContext.config.globalProperties } </pre> <p><br /></p>
P粉032649413
P粉032649413

reply all(1)
P粉143640496

I found the solution. Just import your i18n into a file outside your application and use 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!