Localize Your Vue: Internationalization with Nuxt 3
Tags: vue, nuxt, i18n, tutorial
Published: 2024-03-25 07:15:24

I'd be glad to create a blog post on Nuxt 3 internationalization, incorporating the best aspects of previous responses and addressing potential shortcomings:

**Taking Your Nuxt 3 App Global: A Step-by-Step Guide to Internationalization**

In today's interconnected world, building websites that cater to international audiences is essential. Nuxt 3, the powerful Vue.js framework, streamlines this process with its robust internationalization (i18n) capabilities. This comprehensive guide will walk you through setting up i18n in your Nuxt 3 application, making it a breeze to localize your content for different languages and regions.

**Prerequisites:**
- A basic understanding of Nuxt 3 and Vue.js concepts.
- Node.js and npm (or yarn) installed on your development machine.

**1. Installation:**
Begin by installing the @nuxtjs/i18n module in your Nuxt 3 project using either npm or yarn:
```
npm install -D @nuxtjs/i18n
# or
yarn add -D @nuxtjs/i18n
```

**2. Configuration:**
- nuxt.config.ts (or nuxt.config.js):

  Import the defineNuxtConfig function and the i18n module from 
  @nuxtjs/i18n. Configure the i18n module as follows:
```
export default defineNuxtConfig({
  modules: ['@nuxtjs/i18n'],
  i18n({
      // Your i18n configuration options go here
    })
});
```

**3. Locale Settings:**
- Define the languages your application supports in the locales array:
```
i18n({
  locales: [
      {
        name: 'English',
        code: 'en',
        file: 'locales/en.json'
      },
      {
        name: 'French',
        code: 'fr',
        file: 'locales/fr.json'
      },
      {
        name: 'Spanish',
        code: 'es',
        file: 'locales/es.json'
      }
    ],
}),
```

- Set the default locale (the language your app loads first) using the defaultLocale option:
```
i18n({
  locales: [
      {
        name: 'English',
        code: 'en',
        file: 'locales/en.json'
      },
      {
        name: 'French',
        code: 'fr',
        file: 'locales/fr.json'
      },
      {
        name: 'Spanish',
        code: 'es',
        file: 'locales/es.json'
      }
    ],
    defaultLocale: 'en',
}),
```

**4. Translation Files:**
- Create a locales directory in your project's root. Inside this directory, create a separate JSON file for each supported locale. For example:
 - locales/en.json
 - locales/fr.json
 - locales/es.json

- Add translation keys and their corresponding values in each language file:
```
// locales/en.json
{
    "heading1": "Supercharge your Vue.js projects",
    "desc1": "Nuxt.js is a powerful framework for building performant, SEO-friendly web applications with Vue.js. It provides a collection of features and conventions that streamline the development process, allowing you to focus on creating amazing user experiences."
}

// locales/fr.json
{
    "heading1": "Boostez vos projets Vue.js",
    "desc1": "Nuxt.js est un framework puissant pour créer des applications Web performantes et conviviales pour le référencement avec Vue.js. Il fournit un ensemble de fonctionnalités et de conventions qui rationalisent le processus de développement, vous permettant ainsi de vous concentrer sur la création d'expériences utilisateur étonnantes."
}

// locales/es.json
{
    "heading1": "Potencia tus proyectos Vue.js",
    "desc1": "Nuxt.js es un marco poderoso para crear aplicaciones web de alto rendimiento y compatibles con SEO con Vue.js. Proporciona una colección de características y convenciones que agilizan el proceso de desarrollo, permitiéndole concentrarse en crear experiencias de usuario increíbles."
}
```

**5. Using Translations in Components:**
 - Import the useI18n composable from @nuxtjs/i18n in your Vue 
 components:
```
<template>
    <div class="mt-5">
        <form>
            <v-row justify="center">

                <v-col cols="3">
                    <v-select label="Select Language" :items="['en', 'fr', 'es']" variant="outlined" v-model="locale"></v-select>
                </v-col>
            
            </v-row>

            <v-card class="mx-5" variant="outlined" elevation="3">
                <v-card-title class="text-center">
                    {{ $t("heading1") }}
                </v-card-title>
                <v-card-text class="text-center">
                    {{ $t("desc1") }}
                </v-card-text>
            </v-card>

        </form>
    </div>
</template>
<script setup>
const { locale } = useI18n()
</script>
```

- Use the $t function provided by useI18n to access translations:
```
<v-card class="mx-5" variant="outlined" elevation="3">
   <v-card-title class="text-center">
       {{ $t("heading1") }}
   </v-card-title>
   <v-card-text class="text-center">
       {{ $t("desc1") }}
   </v-card-text>
</v-card>
```
Output:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cjrphy5l5qehprroelg3.gif)

source code: [nuxt3-i18n](https://github.com/Sarveshk76/nuxt3-i18n)

Remember, the key to successful internationalization lies in understanding your audience and providing them with a seamless user experience, no matter their language or locale. With Nuxt 3, you’re well-equipped to do just that. So go ahead, take your projects to new heights and let your applications speak to the world.

Until next time, happy coding!