Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$t() autocomplete support #1753

Open
2 of 4 tasks
EternalC0der opened this issue Dec 20, 2022 · 5 comments · May be fixed by intlify/vue-i18n#1649
Open
2 of 4 tasks

$t() autocomplete support #1753

EternalC0der opened this issue Dec 20, 2022 · 5 comments · May be fixed by intlify/vue-i18n#1649

Comments

@EternalC0der
Copy link

Describe the feature

Hello,
It would be really nice to have auto completion feature while using t('welcome') in template, especially when you want to reuse keys in different sections.

Additional information

  • Would you be willing to help implement this feature?
  • Could this feature be implemented as a module?

Final checks

Copy link
Collaborator

kazupon commented Dec 20, 2022

Hi!
I'll support about auto complete.
vue-i18n v9 has type-safe resources feature.
https://vue-i18n.intlify.dev/guide/advanced/typescript.html

we need to setup manually in vue-i18n.
In nuxt, I will support auto complete by respecting the defaultLocale and dynamically generating types with that resource as master schema.

I would also like to support $t, but we need to refactor the $t type definition on the vue-i18n side.
related issue on vue-i18n:
intlify/vue-i18n#1124

@jenseo
Copy link

jenseo commented Oct 10, 2023

@kazupon Any news on when we can expect this to be shipped? It would be a real step forward for developer experience in my opinion :)

@natemate90
Copy link

Any update on this?

@GalacticHypernova GalacticHypernova linked a pull request Dec 4, 2023 that will close this issue
@EternalC0der
Copy link
Author

I have been waiting for this feature for so long 😂
Any updates?

Here is a quick solution to get the list of locale keys, I wrote this to manually add type-safety for my code.
I hope it helps others:

import english from '~/i18n/en-US.json'

type StringKey<T> = Extract<keyof T, string>
type GenerateKeyPaths<T, Prefix extends string = ''> = T extends object
  ? { [K in StringKey<T>]: T[K] extends object ? GenerateKeyPaths<T[K], `${Prefix}${K}.`> : `${Prefix}${K}` }[StringKey<T>]
  : Prefix

type Locale = typeof english
export type I18nKeys = GenerateKeyPaths<Locale>

@Bobakanoosh
Copy link

@EternalC0der how are you using that? Are you making a custom translation function that then uses I18nKeys?

This works if we use t from useI18n

import "vue-i18n";
import en from "./i18n/website/en.json";

type MainTranslations = typeof en;
declare module "vue-i18n" {
	export interface DefineLocaleMessage extends MainTranslations {}
}

but it doesn't work with $t because the generated types for $t are incorrect. There's like 10 different overrides for it so I dont really want to override them all like above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants