Vue and Nuxt

Vue and Nuxt Localization

Localize a Vue or Nuxt application: mark the strings with Vue I18n, keep them in a JSON file, translate it in Soluling, and load the localized files into your application.

Vue has no built-in localization, so you use a library. Most Vue applications use Vue I18n, and Nuxt applications use Nuxt I18n, which is built on it. Soluling reads Vue I18n's JSON files, keeps their nesting, checks the placeholders, and writes a file for each language. Translate in Soluling Desktop or in a browser with Soluling Cloud. The tutorial uses the Driving sample, a small driving time calculator; see Nuxt for Nuxt applications.

Try Soluling

Resources JSON en.json Soluling Original (English) Translation (Finnish) Driving Time Driving distance Driving speed Calculate Driving time is hours minutes n hour | n hours n minute | n minutes Ajoaika Ajomatka Ajonopeus Laske Ajoaika on hours minutes n tunti | n tuntia n minuutti | n minuuttia The strings in en.json have been translated Localized resources JSON en.json fi fi.json de de.json ja ja.json A localized JSON file for each language, imported by the application Resources JSON en.json Soluling Original (English) Translation (Finnish) Driving Time Driving distance Driving speed Calculate Driving time is hours minutes n hour | n hours n minute | n minutes Ajoaika Ajomatka Ajonopeus Laske Ajoaika on hours minutes n tunti | n tuntia n minuutti | n minuuttia The strings in en.json have been translated Localized resources JSON en.json fi fi.json de de.json ja ja.json A localized JSON file for each language, imported by the application
One Vue build holds every language you import, and the application changes the language at runtime.
Input
Vue I18n's src/locales/en.json, or Nuxt I18n's i18n/locales/en.json
Output
Localized resource files, such as fi.json, next to en.json
Runtime
Vue I18n shows the messages of the active language and changes it without loading the page again
Cloud
Upload the resource file to Soluling Cloud directly; no desktop step needed

On this page

Tutorial

Localize a Vue application, step by step

Six steps take the Driving sample from an English-only application to a multilingual one.

01

Prepare: move the strings to en.json and mark them

Install the library with npm install vue-i18n. Create src/locales/en.json for the strings of the original language, and give each string a short key. Then create the I18n instance in src/i18n.js and add it to the application with app.use(i18n):

// src\i18n.js
import { createI18n } from 'vue-i18n';
import en from './locales/en.json';

export default createI18n({
  legacy: false,          // the Composition API: useI18n
  locale: 'en',
  fallbackLocale: 'en',
  messages: { en },
});

Then replace every text the user sees with the t function of useI18n:

  1. Template text. Change <h1>Driving Time</h1> to <h1>{{ t('drivingTime') }}</h1>.
  2. Attributes. Bind the attribute: change title="Distance in kilometers" to :title="t('distanceHint')". An attribute without the colon stays in English.
  3. Strings in code. Call t in the script too, such as for the page title. Call it in a computed or watchEffect, so the text changes with the language.
  4. Placeholders. Write a whole sentence with named placeholders, such as Hello {name}!, and pass the values: t('hello', { name }).
  5. Plurals. Separate the forms with a vertical bar, and pass the count: "{n} hour | {n} hours" with t('duration.hours', hours). See plurals.
02

Add: create a Soluling project

Vue I18n has no extract step: en.json, which you wrote while marking the strings, is the original resource file. Drag and drop it on Soluling. In the Select File Type dialog, select Vue I18n resource file: the file is also valid generic JSON, but only this parser understands Vue I18n's placeholders. Add the target languages, and click Finish.

Creating the project src/locales/en.json goes through the Select File Type dialog (Vue I18n resource file) and Select Languages page (Finnish, German, Japanese). Resource file src\locales\en.json Select file type Vue I18n resource file Languages Finnish (fi) German (de) Japanese (ja) Select Vue I18n resource file, not the generic JSON file.
Marking the texts with Vue I18n Before, the heading, tooltip, and result are hard-coded English. After, the heading and the tooltip use t, and the result is a sentence with plural forms. en.json has the texts, with Finnish translations. Before: original\src\App.vue <h1>Driving Time</h1> <input title="Distance in kilometers" /> <p>Driving time is {{ result.hours }} hour(s) ...</p> ✕ Stays English, no plural forms Internationalize After: vue-i18n\src\App.vue <h1>{{ t('drivingTime') }}</h1> <input :title="t('distanceHint')" /> t('result', { hours: t('duration.hours', hours), minutes }) en.json and fi.json in Soluling drivingTime Finnish Ajoaika duration.hours English {n} hour | {n} hours Finnish {n} tunti | {n} tuntia ✓ The keys, the texts, and the plural forms of each language
{n} is the count. Vue I18n chooses the form by the count that you pass to t.
03

Translate: texts, placeholders, and plural forms

Each string is a row in the grid, and a nested key, such as duration.hours, is shown under its parent. Placeholders such as {hours} are protected: the translator moves them but doesn't change them. A plural is one row: the translator keeps the vertical bars and writes the forms of the target language. Japanese has only one form, so its translation is just {n} 時間. Vue I18n files have no comments, so write the comments for the translator in Soluling.

  • Machine translation and translation memory. Click Use Machine Translation to translate with Amazon, Claude, DeepL, Google, Microsoft, ModernMT, or MyMemory, and reuse earlier translations from translation memory and terminology.
  • External translators. Send a translation package, an Excel file, or XLIFF to a translator, and import the results back. See teamwork.
  • Translators in a browser. Upload the project to Soluling Cloud and let translators work in Translate.
04

Check: find problems before release

Pseudo translation. Click Translate | Pseudo Translations | Fill, build, and run the application. Pseudo translations are longer and use accented characters, so texts that don't fit and strings still in the code (shown in plain English) stand out. See pseudo translation.

Pseudo-translated build

[Ðŕîvîñĝ Ţîɱé ___] PS Driving distance 220 Driving speed 88 [Çåļçûļåţé ______________] [Çåļçûļåţé ______________] [Ðŕîvîñĝ ţîɱé îš 2 ĥôûŕš 30 ɱîñûţéš ________] Too long and still English (hard-coded)

Validation. Soluling validates translations while you type and in batch (Validation | Validate Translations): missing translations, inconsistent placeholders and patterns, duplicate hotkeys, and more. See validation.

Validation

⚠ Inconsistent format string result (German) The original has {hours} and {minutes}; the translation has only {hours}: Die Fahrzeit beträgt {hours} ⚠ Missing translation speedHint (Japanese) The tooltip shows the English text.
05

Build: create the localized files and import them

Choose Home | Build All. Soluling writes fi.json, de.json, and ja.json into src/locales, with the same nesting as en.json. Import them in src/i18n.js, and add them to messages:

// src\i18n.js
import en from './locales/en.json';

// Add each language here. Soluling writes these files.
import de from './locales/de.json';
import fi from './locales/fi.json';
import ja from './locales/ja.json';

const messages = { en, de, fi, ja };

Then build the application as usual, with npm run build. Every imported language is in the bundle; to download only the language in use, see lazy loading.

Resource files and the built application src\locales has the original en.json and the localized files fi.json, de.json, and ja.json. npm run build writes one application with every language into the dist folder. Soluling: Build All DIR src\locales\ JSON en.json original: written by you JSON fi.json localized: the same keys JSON de.json JSON ja.json Vue: npm run build DIR dist\ DIR index.html and the bundle with every language
06

Update: translate only what changed

When you add or change strings in en.json, click Home | Scan. Soluling keeps the existing translations, adds the new strings, and marks the rows whose original text changed. Translate the new rows, build, and build the application. Add the scan and build steps to your build pipeline with SoluMake or, with Soluling Cloud, SoluCLI.

Reference

The message syntax

Vue I18n has its own message syntax. It is not the ICU syntax of React Intl or next-intl, so choose Vue I18n resource file, and Soluling protects these parts of a message:

// src\locales\en.json
{
  "hello": "Hello {name}!",                     // a named placeholder
  "first": "Hello {0}!",                        // a list placeholder: t('first', ['Maija'])
  "duration": {                                 // nesting: t('duration.hours', 2)
    "hours": "{n} hour | {n} hours"             // plural forms, {n} is the count
  },
  "email": "Send it to info{'@'}example.com",  // a literal @, {, }, $, or |
  "welcome": "@:hello Welcome back."           // a linked message
}

{, }, @, $, and | are special characters. A message that has them as text, such as an email address, writes them as a literal: {'@'}. The translator must keep a linked message, such as @:hello, as it is: Vue I18n replaces it with the message of that key.

Plurals

A plural message has its forms separated by a vertical bar, and {n} is the count. Vue I18n chooses the form by the count and the number of forms:

  • Two forms, such as {n} hour | {n} hours: the first for 1, the second for any other count. This fits English, Finnish, German, and many others.
  • Three forms, such as no hours | {n} hour | {n} hours: for 0, for 1, and for any other count.
  • One form, such as Japanese {n} 時間: the same text for every count.

A message can't contain two plurals, so the Driving sample has a sentence with two placeholders, and a plural message for each. When the hours are 0, it uses another sentence, resultMinutes, because Vue I18n doesn't accept an empty form.

Vue I18n's own rule doesn't know the languages that have more forms, such as Polish, Russian, or Arabic. For those, add a rule to pluralRules. This rule gets the form from the browser's Intl.PluralRules, and the Polish translation has the forms in the order one, few, and many:

const polish = new Intl.PluralRules('pl');

createI18n({
  ...
  pluralRules: {
    // "{n} godzina | {n} godziny | {n} godzin"
    pl: (count) => ({ one: 0, few: 1, many: 2 })[polish.select(count)] ?? 2,
  },
});

Soluling translates a plural as one text, so it doesn't check the number of forms. Tell the translator how many forms each language needs, in a comment or in the project's instructions.

Fallback

The sample removes the region from the browser's language, so fi-FI uses fi.json, and a language without a file uses English. If a key isn't in the language's file, Vue I18n uses the fallbackLocale, the original text in en.json.

Message lookup for a browser in fi-FI For a browser in fi-FI, the code removes the region. drivingTime and calculate are found in fi.json as Ajoaika and Laske. A string that was not translated uses en.json, the fallback language. UI culture: fi-FI fi-FI region removed by the code fi.json en.json fallbackLocale drivingTime – ✓ Ajoaika calculate – ✓ Laske aNewString – not translated ✓ A new string

Choose and change the language

The sample starts with the language the user selected earlier, or with the browser's language. When the user selects another language, it sets locale, and every component shows the new language without loading the page again.

// src\i18n.js
const STORAGE_KEY = 'language';

// The language selected by the user, or the language of the browser without the region code
function getInitialLanguage() {
  const language = localStorage.getItem(STORAGE_KEY) || (navigator.language || 'en').split(/[-_]/)[0];
  return messages[language] ? language : 'en';
}

// src\App.vue
const { t, locale } = useI18n();

function changeLanguage(value) {
  localStorage.setItem(STORAGE_KEY, value);
  locale.value = value;
}

With many languages or large files, load a language only when it is needed. Then the bundle has only English, and each language is downloaded the first time it is used:

export async function loadLanguage(i18n, language) {
  if (!i18n.global.availableLocales.includes(language)) {
    const messages = await import(`./locales/${language}.json`);
    i18n.global.setLocaleMessage(language, messages.default);
  }

  i18n.global.locale.value = language;
}

Nuxt and Nuxt I18n

Nuxt applications use the Nuxt I18n module, @nuxtjs/i18n. It is built on Vue I18n, so the messages, the syntax, and the t function are the same. Its files are in i18n/locales, and nuxt.config.ts lists the languages:

// nuxt.config.ts
export default defineNuxtConfig({
  modules: ['@nuxtjs/i18n'],
  i18n: {
    defaultLocale: 'en',
    locales: [
      { code: 'en', name: 'English', file: 'en.json' },
      { code: 'fi', name: 'suomi', file: 'fi.json' },
      { code: 'de', name: 'Deutsch', file: 'de.json' },
      { code: 'ja', name: '日本語', file: 'ja.json' },
    ],
  },
});
  • Add the file. Drag and drop i18n/locales/en.json on Soluling, and select Vue I18n resource file.
  • Build. Soluling writes fi.json and the others next to en.json, where the configuration above loads them. Nuxt I18n loads each file when its language is used.
  • The language in the URL. Nuxt I18n adds the language to the routes, such as /fi/..., and has functions for the links and the language selector. See the Nuxt I18n documentation.

i18next-vue

If your Vue application uses i18next with i18next-vue, its files are i18next files: add translation.json as an i18next resource file. See i18next in the React guide and i18next localization.

Vue I18n can also keep messages in an <i18n> block of a .vue file. Soluling localizes the JSON files, so keep the messages in src/locales.

Translate in Soluling Cloud

With Soluling Cloud, translators use Translate in a browser. How you start depends on what you localize:

  • Vue I18n resource files (.json). On the dashboard, add a project, select src/locales/en.json, and choose Vue I18n resource file (.json) as the source type. Translators work in Translate; click Build localized files, and save the downloads to src/locales as fi.json and so on. When the application changes, upload the new en.json; the existing translations are kept. No desktop step is needed.

To run the scan, upload, import, and build steps in your build pipeline, use SoluMake. See the Cloud tutorial and workflows.

Do and don't

Do

  • Bind attributes such as title and placeholder to t.
  • Use t for every text in code, including the page title.
  • Give each text a short, stable key, and group the keys by nesting.
  • Write whole sentences with named placeholders.
  • Add a pluralRules rule for each language that has more plural forms than English.
  • Pseudo-translate before the first real translation.

Don't

  • Build sentences from pieces in the template or code.
  • Use the English text as the key; the key changes when the text changes.
  • Choose plural forms with if statements; other languages have other rules.
  • Add en.json as a generic JSON file; the placeholders aren't protected.
  • Write @ or | as text in a message; write it as a literal.

Typical localizability problems

See also internationalization and pseudo translation.

Problem How it shows Find and fix
Unbound attributes Placeholders and tooltips in English in a pseudo-translated build Bind the attribute with a colon, such as :title, and use t.
Strings in code Messages and the page title in English Use t of useI18n.
A text that doesn't change with the language The old language stays after the user changes it Call t in a computed or the template, not once when the component is created.
Sentences built from pieces Word order and plurals are wrong in the translation Use one message with placeholders, and a plural message for each count.
Fixed widths Clipped text in the pseudo or German build Let the layout grow with the text.

Troubleshooting

Symptom Likely cause What to do
The application shows a key, such as drivingTime The key isn't in en.json, or it is misspelled Add the key to en.json, scan, and translate.
The application stays in English The language's file is not imported, or the locale has a region code Import the file, add it to messages, and remove the region from the language.
Message compilation error in the console A message has a special character as text, or an empty plural form Write the character as a literal, such as {'@'}, and give every form a text.
The wrong plural form in Polish or Russian Vue I18n's own rule knows only one and other Add a rule for the language to pluralRules.
Placeholders aren't protected in Soluling The file was added as a generic JSON file Create the project again with Vue I18n resource file.

Samples

The samples are in Samples\Vue of the Soluling installation and on GitHub:

  • Driving\original: the driving time calculator of this guide before internationalization. Try this first.
  • Driving\vue-i18n: internationalized with Vue I18n and localized into Finnish, German, and Japanese, with a language selector and the Soluling project, src\locales\en.ntp.
  • vue-i18n\hello and vue-i18n\sport: a Hello World application and an application that uses a multilingual API. They use Vue 2, but the resource files and the Soluling projects are the same.

See Vue localization in the Desktop documentation for all the details.

FAQ

Vue I18n. It is the most used Vue library, and Nuxt I18n is built on it. If your application already uses i18next, i18next-vue works too; see i18next-vue.

Yes. Nuxt I18n uses Vue I18n's files: add i18n/locales/en.json as a Vue I18n resource file. See Nuxt and Nuxt I18n.

Yes. Upload the resource file to Soluling Cloud, translate in Translate, and download the localized files. See Translate in Soluling Cloud.

No. One build holds every language you import, and the application chooses at runtime.

Yes. Set the locale of Vue I18n, and every component shows the new language. See Choose and change the language.

Yes. Vue 2 uses Vue I18n 8, whose $t and resource files are the same. The code of this guide uses Vue 3 and the Composition API.

Yes. The resource files are the same, and Vue I18n has types for the messages.

Related

All guides