nuxt-modules / i18n

I18n module for Nuxt
https://i18n.nuxtjs.org
MIT License
1.75k stars 483 forks source link

Narrow typed locale identifiers #2857

Closed warflash closed 4 months ago

warflash commented 8 months ago

Describe the feature

const { locale, locales } = useI18n()

Currently locale is typed as string and locales is string[]. The suggestion is to use tighter types like locale -> "en" | "de" in the case of a config like:

    i18n: {
        locales: [
            { code: "en", iso: "en-US", file: "en.json" },
            { code: "de", iso: "de-DE", file: "de.json" },
        ],
    },

That would make type safe access to typed records possible. e.g.

const MIN_SEARCH_LEN_BY_LOCALE = {
    en: 3,
    de: 3,
    ko: 1,
}

const { locale, locales } = useI18n()

const minLen = MIN_SEARCH_LEN_BY_LOCALE[locale] 

Additional information

Final checks

BobbieGoede commented 4 months ago

Should be resolved by #3025, which will be included in v9

warflash commented 4 months ago

Thanks! 💚