Vue 3 vue-i18n. How to use $t(t) outside the application (.vue file)? Synthesis API
P粉032649413
2023-08-26 10:24:05
<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>
I found the solution. Just import your
i18n
into a file outside your application anduse i18n.global.t