You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using version v9.2.0-beta.20 in a project which uses Composition API and TypeScript. I want my application to solely rely on global scope translation, so I would benefit using the globally injected APIs as listed in the docs Composition API # Implicit with injected properties and functions, without having to useI18n in every component's setup function, but only for those who need to translate something in the setup function (and not in the template).
I also would benefit from having type safety / Resource keys completion support for those injected APIs depending on my global schema. However, it seems I need an extra step to tell TypeScript the correct types, as the docs say in a notice found at the bottom of Typescript Support#Resource Keys completion supporting :
Legacy Mode, and interpolation of Resource Keys of APIs such as $t and $d, which are injected into Component by globalInjection: true` of Composition API, require explicitly specifying type parameters.
<template>
<div>
{{ t('test.welcome', { username: 'john' }) }} <!-- keys completion working here -->
{{ $t('test.welcome', { username: 'john' }) }} <!-- keys completion not working here -->
</div>
</template>
<script lang="ts">
import { defineComponent } from"vue";import { useI18n } from"vue-i18n";exportdefaultdefineComponent({ setup() {const { t } =useI18n(); // I don't actually need useI18n in this component, // because I only translate messages in the template and not in the setup functionreturn { t }; }})
</script>
My I18n initialization looks like this:
// https://vue-i18n.intlify.dev/guide/advanced/typescriptconsti18n=createI18n<false>({locale: defaultLocale,fallbackLocale: defaultLocale,messages: {it: messages_it,// "en-US": messages_en,},// https://vue-i18n.intlify.dev/guide/advanced/composition.html#basic-usagelegacy: false,// enable composition API (useI18n())// https://vue-i18n.intlify.dev/guide/advanced/composition.html#implicit-with-injected-properties-and-functionsglobalInjection: true,// allow to use both $t and useI18n()});app.use(i18n);
Note the use of false as type parameter for createI18n. I also tried both
From the linked API page from the docs (https://vue-i18n.intlify.dev/api/injection) I understand that I have to use type augmentation directly on the ComponentCustomProperties interface, however I've found this advice misleading, as it would need me to augment every single override of the $t API and all other APIs.
declare module "@vue/runtime-core"{interfaceComponentCustomProperties{/* In Composition API mode, the $t is injected by app.config.globalProperties. The input / output is the same as for Composer, and it works on global scope. About that details, see Composer#t. */// $t(key: Path): TranslateResult // what to do here???}}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am using version v9.2.0-beta.20 in a project which uses Composition API and TypeScript. I want my application to solely rely on global scope translation, so I would benefit using the globally injected APIs as listed in the docs Composition API # Implicit with injected properties and functions, without having to
useI18n
in every component's setup function, but only for those who need to translate something in the setup function (and not in the template).I also would benefit from having type safety / Resource keys completion support for those injected APIs depending on my global schema. However, it seems I need an extra step to tell TypeScript the correct types, as the docs say in a notice found at the bottom of Typescript Support#Resource Keys completion supporting :
I don't understand where i should specify the type parameters and If there is an optimal solution for my use case.
My goal is to have type suggestions on both of those functions:
My I18n initialization looks like this:
Note the use of
false
as type parameter forcreateI18n
. I also tried bothand
without seeing a difference.
From the linked API page from the docs (https://vue-i18n.intlify.dev/api/injection) I understand that I have to use type augmentation directly on the
ComponentCustomProperties
interface, however I've found this advice misleading, as it would need me to augment every single override of the $t API and all other APIs.Is there a better way to do this?
Beta Was this translation helpful? Give feedback.
All reactions