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.
- Input
- Vue I18n's
src/locales/en.json, or Nuxt I18n'si18n/locales/en.json - Output
- Localized resource files, such as
fi.json, next toen.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
- The message syntax
- Plurals
- Fallback
- Choose and change the language
- Nuxt and Nuxt I18n
- i18next-vue
- Translate in Soluling Cloud
- Do and don't
- Typical localizability problems
- Troubleshooting
- Samples
- FAQ
Localize a Vue application, step by step
Six steps take the Driving sample from an English-only application to a multilingual one.
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:
-
Template text. Change
<h1>Driving Time</h1>to<h1>{{ t('drivingTime') }}</h1>. -
Attributes. Bind the attribute: change
title="Distance in kilometers"to:title="t('distanceHint')". An attribute without the colon stays in English. -
Strings in code. Call
tin the script too, such as for the page title. Call it in acomputedorwatchEffect, so the text changes with the language. -
Placeholders. Write a whole sentence with named placeholders, such as
Hello {name}!, and pass the values:t('hello', { name }). -
Plurals. Separate the forms with a vertical bar, and pass the count:
"{n} hour | {n} hours"witht('duration.hours', hours). See plurals.
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.
{n} is the count. Vue I18n chooses the form by the count that you pass to t.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.
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
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
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.
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.
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.
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.jsonon Soluling, and select Vue I18n resource file. -
Build. Soluling writes
fi.jsonand the others next toen.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, selectsrc/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 tosrc/localesasfi.jsonand so on. When the application changes, upload the newen.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
titleandplaceholdertot. - Use
tfor 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
pluralRulesrule 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
ifstatements; other languages have other rules. - Add
en.jsonas 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
i18n/locales/en.json as a Vue I18n resource file. See Nuxt and Nuxt I18n.locale of Vue I18n, and every component shows the new language. See Choose and change the language.$t and resource files are the same. The code of this guide uses Vue 3 and the Composition API.Related
- Vue localization - the full Desktop documentation
- React and Next.js localization - if you also have React applications
- i18next localization - if you use i18next
- Supported formats - everything else Soluling localizes