Internationalization
For internationaliztion supastarter integrates nuxt/i18n.
Why nuxt/i18n:
nuxt/i18n is a lightweight internationalization library based on
vue-i18n especially designed to leverage Nuxt
3 features (auto-imports, file-based route generation, etc.). It also supports
automatic language detection and url based language routing.
Usage
In Vue components, you can use the auto-imported composable useTranslations(). This composable exposes the useI18n() composable from nuxt/i18n and adds types for the translation keys.
<script setup lang="ts">
const { t } = useTranslations();
</script>
<template>
<div>{{ t('hello') }}</div>
</template>Localized Links
nuxt/i18n also auto-imports useLocalePath(), a useful composable to generate localized links.
If you want to use a link, the auto-imported component <NuxtLinkLocale> is built on top of <NuxtLink>
but changes the default behavior by internally using localePath() to make it easier to link to localized routes.
<template>
<NuxtLinkLocale to="/">{{ $t('home') }}</NuxtLinkLocale>
</template>is equivalent to
<script setup lang="ts">
const localePath = useLocalePath();
</script>
<template>
<NuxtLink :to="localePath('/')">{{ $t('home') }}</NuxtLink>
</template>Translations
Translations are stored in the packages/i18n/translations folder. The default locale is en and the translations are stored in en.json.
{
"hello": "Hello World!"
}Add a new locale
To add a new locale you need to create a new file in the packages/i18n/translations folder with the locale name as the file name. For example fr.json for French.
{
"hello": "Bonjour le monde!"
}Then register this locale in the global config file:
// ...
i18n: {
locales: {
fr: {
label: "Français",
currency: "EUR",
},
// ...
},
// ...
},
// ...