-
-
Notifications
You must be signed in to change notification settings - Fork 490
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
Comments
Hi! we need to setup manually in vue-i18n. I would also like to support |
@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 :) |
Any update on this? |
I have been waiting for this feature for so long 😂 Here is a quick solution to get the list of locale keys, I wrote this to manually add type-safety for my code. 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> |
@EternalC0der how are you using that? Are you making a custom translation function that then uses This works if we use 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 |
@kazupon I was able to get type-safe checking as well as auto-completion to work for the I believe this could also be extended for type checking the methods exported like I was going to attempt to PR the feature into the upstream repository; however, there appear to be around 19 different interfaces for the |
Managed to get key autocompletion by adding a
|
Thanks! This helped me get the autocomplete working! 🙏 // lang/en.ts
export default {
welcome: "Welcome"
} I had to change the config to import type en from "./lang/en";
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;
// IMPORTANT CHANGE HERE, USING AWAITED + RETURN TYPE
type Locale = Awaited<ReturnType<typeof en>>;
declare global {
type I18nKeys = GenerateKeyPaths<Locale>;
}
declare module "vue" {
interface ComponentCustomProperties {
$t(key: I18nKeys): string;
}
} Also, remember to add the // tsconfig.json
{
// ...
"compilerOptions": {
"types": ["./i18n.d.ts"]
}
} In case it helps anybody ✌️ |
The latest v9 release candidate (https://github.com/nuxt-modules/i18n/releases/tag/v9.0.0-rc.2) includes experimental More on the option that enables this functionality: https://i18n.nuxtjs.org/docs/v9/options/misc#experimentaltypedoptionsandmessages, the |
Closing this as we have experimental support for this in the stable release of v9, please try it out and let me know your thoughts! |
It works like a charm for me! Absolutely love it! |
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
Final checks
The text was updated successfully, but these errors were encountered: