How To Build A Multilingual Website With Nuxt.js 

Development

Creating a multilingual website is essential for businesses and individuals who want to reach a global audience. With Nuxt.js, a powerful framework built on top of Vue.js, building a multilingual site becomes a streamlined and efficient process. This guide will walk you through the steps to create a multilingual website with Nuxt.js, from setting up the project to implementing multiple languages. 

 Setting Up Your Nuxt.js Project 

1. Setting Up Your Nuxt.js Project 

To get started with a Nuxt.js project, ensure you have Node.js and npm (or Yarn) installed on your machine. If not, download them from the official Node.js website. 

1. Create a new Nuxt.js project 

   Open your terminal and run the following command to create a new Nuxt.js project: 

   “`bash 

   npx create-nuxt-app multilingual-site 

   “` 

   This command will prompt you to enter various options, such as project name, programming language, package manager, and more. Once you’ve configured these options, the project will be created, and you can navigate to the project directory. 

2. Install dependencies 

   Navigate to your project directory and install the necessary dependencies: 

   “`bash 

   cd multilingual-site 

   npm install 

   “` 

2. Installing i18n for Multilingual Support 

Nuxt.js offers seamless integration with the `nuxt-i18n` module, which simplifies the process of making your site multilingual. 

1. Install `nuxt-i18n` 

   Add the `nuxt-i18n` module to your project: 

   “`bash 

   npm install @nuxtjs/i18n 

   “` 

2. Configure `nuxt-i18n` 

   After installation, add the module to your `nuxt.config.js` file: 

   “`javascript 

   export default { 

     modules: [ 

       ‘@nuxtjs/i18n’, 

     ], 

     i18n: { 

       locales: [ 

         { code: ‘en’, name: ‘English’, iso: ‘en-US’, file: ‘en-US.json’ }, 

         { code: ‘fr’, name: ‘Français’, iso: ‘fr-FR’, file: ‘fr-FR.json’ }, 

         { code: ‘es’, name: ‘Español’, iso: ‘es-ES’, file: ‘es-ES.json’ } 

       ], 

       defaultLocale: ‘en’, 

       langDir: ‘locales/’, 

       vueI18n: { 

         fallbackLocale: ‘en’, 

       }, 

     } 

   } 

   “` 

   In this configuration: 

   – `locales` defines the languages your site will support. 

   – `defaultLocale` sets the default language. 

   – `langDir` specifies the directory where the translation files will be stored. 

3. Create translation files 

   Inside the `locales` directory, create JSON files for each language, such as `en-US.json`, `fr-FR.json`, and `es-ES.json`. These files will contain key-value pairs for translations: 

   “`json 

   { 

     “welcome”: “Welcome”, 

     “about”: “About Us” 

   } 

   “` 

Adding Multilingual Content

3. Adding Multilingual Content 

Now that the basic setup is complete, it’s time to add multilingual content to your site. 

1. Using translation keys 

   In your Vue components or pages, use the `$t` function to display translated content: 

   “`html 

   <template> 

     <div> 

       <h1>{{ $t(‘welcome’) }}</h1> 

       <p>{{ $t(‘about’) }}</p> 

     </div> 

   </template> 

   “` 

2. Managing routes 

   `nuxt-i18n` automatically handles routing for different languages. For instance, if your site supports English and French, the routes will be structured as `/en/about` and `/fr/about`. You can define multilingual routes in the `nuxt.config.js` file: 

   “`javascript 

   i18n: { 

     … 

     pages: { 

       about: { 

         en: ‘/about’, 

         fr: ‘/a-propos’, 

         es: ‘/acerca’ 

       } 

     } 

   } 

   “` 

3. Adding language switcher 

   Include a language switcher in your site’s navigation or footer to allow users to change languages: 

   “`html 

   <template> 

     <div> 

       <ul> 

         <li v-for=”locale in $i18n.locales” :key=”locale.code”> 

           <nuxt-link :to=”‘/’ + locale.code + $route.path”>{{ locale.name }}</nuxt-link> 

         </li> 

       </ul> 

     </div> 

   </template> 

   “` 

4. SEO Considerations 

For a multilingual site, it’s crucial to consider SEO to ensure that your site ranks well in search engines for different languages. 

1. Language-specific meta tags 

   Use `nuxt-i18n` to manage language-specific meta tags and hreflang attributes. The module automatically generates the necessary tags to indicate the language of each page to search engines. 

2. Sitemap generation 

   Use a module like `@nuxtjs/sitemap` to create a sitemap for each language version of your site. This helps search engines index all language variants effectively. 

5. Testing and Deployment 

Before deploying your multilingual website, test it thoroughly to ensure that all translations are accurate, routes function correctly, and the language switcher works seamlessly. 

1. Testing 

   Test your site in different languages and on various devices to ensure consistency and accuracy across the board. 

2. Deployment 

   Deploy your Nuxt.js site using platforms like Vercel, Netlify, or any hosting service that supports static sites. Ensure that the deployment environment is configured to handle multiple languages. 

Building a multilingual website with Nuxt.js is a straightforward process thanks to the robust `nuxt-i18n` module. By following this guide, you can create a site that caters to a global audience, providing localized content that enhances user experience and broadens your reach. Proper setup, translation management, and attention to SEO are key components to ensure your multilingual site’s success.