vue-i18n icon indicating copy to clipboard operation
vue-i18n copied to clipboard

`$t` does not have TypeScript auto-completions

Open innocenzi opened this issue 2 years ago • 3 comments

Reporting a bug?

The $t global function in SFCs' templates don't seem to support global TypeScript definitions.

CleanShot 2022-11-05 at 23 02 35

The documentation regarding this topic doesn't mention $t but its definition look like it should support auto-completion.

Expected behavior

$t should provide auto-completion through the overriden DefineLocaleMessage interface, like t exported from useI18n does.

CleanShot 2022-11-05 at 23 03 33

Reproduction

Create a i18n.json file with the following content:

Details
{
	"en": {
		"login": {
			"welcome_text_title": "Welcome back!",
			"welcome_text_help_text": "Enter your credentials to access the app.",
			"email_field_placeholder": "Enter your email",
			"password_field_placeholder": "Enter your password",
			"sign_in_button_label": "Sign in",
			"quick_login_cta_text": "Wanna login quickly?",
			"quick_login_link_text": "Click here."
		}
	}
}

Create a i18n.ts file with the following content:

import { createI18n } from 'vue-i18n'
import messages from './i18n.json'

export type MessageSchema = typeof messages['en']

declare module 'vue-i18n' {
	export interface DefineLocaleMessage extends MessageSchema {}
}

const i18n = createI18n<[MessageSchema], 'en'>({
	locale: 'en',
	inheritLocale: true,
	messages,
})

export default i18n

Create a Vue 3 application, import ./i18n.ts and register the exported plugin.

System Info

System:
    OS: macOS 12.6
    CPU: (10) arm64 Apple M1 Pro
    Memory: 91.94 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.11.0 - /opt/homebrew/bin/node
    Yarn: 1.22.18 - /opt/homebrew/bin/yarn
    npm: 8.19.2 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 107.0.5304.87
    Firefox: 106.0.5
    Safari: 16.0
  npmPackages:
    @vitejs/plugin-vue: ^3.2.0 => 3.2.0 
    @vue/runtime-core: ^3.2.41 => 3.2.41 
    vite: ^3.2.2 => 3.2.2 
    vite-plugin-run: ^0.2.0 => 0.2.0 
    vue: ^3.2.41 => 3.2.41 
    vue-i18n: ^9.0.0 => 9.2.2

Screenshot

No response

Additional context

No response

Validations

innocenzi avatar Nov 05 '22 22:11 innocenzi

Any news about $t having TypeScript autocomplete?

TheVaan avatar Mar 08 '24 15:03 TheVaan

In the meantime you can use the non-global method and you'll have all the intellisense you need, just like in the script

<template>
  <div>{{ t("foo") }}</div>
</template>

<script setup lang="ts">
  const { t } = useI18n()
</script>

wJoenn avatar Apr 08 '24 09:04 wJoenn

Any updates?

beqacrc avatar Apr 25 '24 14:04 beqacrc