


How can internationalization (i18n) and localization (l10n) be implemented in a Vue application?
Jun 20, 2025 am 01:00 AMInternationalization and localization in Vue apps are primarily handled using the Vue I18n plugin. 1. Install vue-i18n via npm or yarn. 2. Create locale JSON files (e.g., en.json, es.json) for translation messages. 3. Set up the i18n instance in main.js with locale configuration and message files. 4. Access translations in templates using $t('key'). 5. Manage translation keys consistently and avoid deep nesting for scalability. 6. Dynamically switch languages by updating the locale property at runtime. 7. Persist selected language in local storage for user preference retention. 8. Use dynamic values in translations with variable injection like $t('helloUser', { name: 'Alice' }). 9. Implement pluralization rules in i18n config and use $tc() for proper plural handling. 10. For advanced needs like RTL support or date formatting, integrate libraries like Intl or date-fns to ensure full localization coverage.
Internationalization (i18n) and localization (l10n) in a Vue app mainly come down to two things: switching content based on the user's language preference and making sure that text, dates, numbers, and other locale-specific elements display correctly. The most common and powerful way to do this is by using Vue I18n, a plugin specifically built for Vue apps.
Setting up Vue I18n
To get started, install vue-i18n
via npm or yarn:
npm install vue-i18n@9
Once installed, create a locale messages file (like en.json
, es.json
, etc.) and set up the i18n instance in your app entry point (main.js
or similar):
import { createApp } from 'vue' import { createI18n } from 'vue-i18n' import App from './App.vue' import en from './locales/en.json' import es from './locales/es.json' const i18n = createI18n({ legacy: false, locale: 'en', fallbackLocale: 'en', messages: { en, es } }) createApp(App).use(i18n).mount('#app')
Now you can access translations anywhere in your templates using $t('key')
.
Managing translation files effectively
Instead of hardcoding strings directly into components, keep them in structured JSON files like:
en.json
{ "welcome": "Welcome!", "button": { "submit": "Submit" } }
es.json
{ "welcome": "?Bienvenido!", "button": { "submit": "Enviar" } }
This makes it easier to maintain and scale as your app grows. You can also use tools like POEditor or Crowdin to help manage translations if working with multiple languages or teams.
A few tips:
- Keep keys consistent across locales.
- Avoid deeply nested structures unless necessary — they can get messy fast.
- Use comments in separate files or tools if your team includes non-developers.
Switching languages dynamically
Changing the app’s language at runtime is simple. Just update the locale
property on the i18n instance:
import { useI18n } from 'vue-i18n' export default { setup() { const { locale } = useI18n() function changeLang(newLang) { locale.value = newLang } return { locale, changeLang } } }
In your template:
<button @click="changeLang('es')">Espa?ol</button>
This will instantly switch all localized text in your app. You can also persist the selected language in local storage so users don’t have to switch every time they visit.
Handling dynamic and pluralized content
Sometimes you need to inject variables into translated strings or handle pluralization. Vue I18n supports both:
Using dynamic values:
In your JSON:
"helloUser": "Hello, {name}!"
In your component:
<p>{{ $t('helloUser', { name: 'Alice' }) }}</p>
Pluralization:
Set up plural rules in your i18n config:
const i18n = createI18n({ legacy: false, locale: 'en', fallbackLocale: 'en', pluralRules: { en: (choice, choicesLength) => { if (choicesLength === 2) { return choice > 1 ? 1 : 0 } return choice ? 1 : 0 } }, messages: { en, es } })
Then in your JSON:
"itemsSelected": "You selected {count} item | You selected {count} items"
And in your template:
<p>{{ $tc('itemsSelected', itemCount, { count: itemCount }) }}</p>
This helps make your UI feel natural in different languages where plural rules vary.
That’s basically how i18n and l10n work in Vue. It’s not too complicated once you’ve got the structure in place, but it’s easy to overlook some edge cases like RTL languages, date formatting, or currency handling. For those, look into integrating with libraries like date-fns
or Intl
. But for most standard needs, Vue I18n covers it pretty well.
The above is the detailed content of How can internationalization (i18n) and localization (l10n) be implemented in a Vue application?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema

Netflix uses React as its front-end framework. 1) React's componentized development model and strong ecosystem are the main reasons why Netflix chose it. 2) Through componentization, Netflix splits complex interfaces into manageable chunks such as video players, recommendation lists and user comments. 3) React's virtual DOM and component life cycle optimizes rendering efficiency and user interaction management.

There are two ways to jump div elements in Vue: use Vue Router and add router-link component. Add the @click event listener and call this.$router.push() method to jump.

Netflix mainly uses React as the front-end framework, supplemented by Vue for specific functions. 1) React's componentization and virtual DOM improve the performance and development efficiency of Netflix applications. 2) Vue is used in Netflix's internal tools and small projects, and its flexibility and ease of use are key.

The methods to implement the jump of a tag in Vue include: using the a tag in the HTML template to specify the href attribute. Use the router-link component of Vue routing. Use this.$router.push() method in JavaScript. Parameters can be passed through the query parameter and routes are configured in the router options for dynamic jumps.

There are the following methods to implement component jump in Vue: use router-link and <router-view> components to perform hyperlink jump, and specify the :to attribute as the target path. Use the <router-view> component directly to display the currently routed rendered components. Use the router.push() and router.replace() methods for programmatic navigation. The former saves history and the latter replaces the current route without leaving records.

Function interception in Vue is a technique used to limit the number of times a function is called within a specified time period and prevent performance problems. The implementation method is: import the lodash library: import { debounce } from 'lodash'; Use the debounce function to create an intercept function: const debouncedFunction = debounce(() => { / Logical / }, 500); Call the intercept function, and the control function is called at most once in 500 milliseconds.
