Internationalization (often abbreviated as i18n) is the process of designing an application so that it can be adapted to various languages and regions without requiring engineering changes. In the context of Vue.js, i18n involves translating your application’s text, handling different date and number formats, and making sure your application can cater to users from different cultural backgrounds. This chapter will take you from the basics to advanced topics in Vue.js internationalization, ensuring you have a thorough understanding of how to make your Vue.js applications accessible to a global audience.
Internationalization is the preparation of software so that it can support different languages and regions. It involves:
Vue I18n is the official internationalization library for Vue.js. To get started, install Vue I18n via npm:
npm install vue-i18n@next
Create an instance of Vue I18n and configure it in your Vue application. Here’s a simple setup:
import { createApp } from 'vue';
import { createI18n } from 'vue-i18n';
import App from './App.vue';
// Define messages
const messages = {
en: {
welcome: 'Welcome'
},
fr: {
welcome: 'Bienvenue'
}
};
// Create i18n instance with options
const i18n = createI18n({
locale: 'en', // set locale
fallbackLocale: 'en', // set fallback locale
messages, // set locale messages
});
// Create Vue app
const app = createApp(App);
app.use(i18n);
app.mount('#app');
In this example, we define messages for English (en
) and French (fr
). We create an i18n
instance with these messages and configure the default locale as English.
Use the $t
method to access translations in your components:
{{ $t('welcome') }}
This will display “Welcome” or “Bienvenue” depending on the current locale.
Allow users to switch between languages dynamically:
{{ $t('welcome') }}
This example adds a dropdown for language selection. When the user selects a language, the changeLocale
method updates the current locale.
Vue I18n supports pluralization, which allows you to handle singular and plural forms of words:
const messages = {
en: {
apples: 'No apples | One apple | {count} apples'
},
fr: {
apples: "Pas de pommes | Une pomme | {count} pommes"
}
};
Use the $tc
method for pluralization:
{{ $tc('apples', 0) }}
{{ $tc('apples', 1) }}
{{ $tc('apples', 5, { count: 5 }) }}
This will display the appropriate message based on the count.
Vue I18n can format dates according to the locale settings:
const messages = {
en: {
date: '{date, date, short}'
},
fr: {
date: '{date, date, short}'
}
};
const i18n = createI18n({
locale: 'en',
fallbackLocale: 'en',
messages,
datetimeFormats: {
en: {
short: {
year: 'numeric', month: 'short', day: 'numeric'
}
},
fr: {
short: {
year: 'numeric', month: 'short', day: 'numeric'
}
}
}
});
In your component:
{{ $d(new Date(), 'short') }}
This will format the date according to the locale.
Similarly, Vue I18n can format numbers and currencies:
const messages = {
en: {
currency: '{amount, number, currency}'
},
fr: {
currency: '{amount, number, currency}'
}
};
const i18n = createI18n({
locale: 'en',
fallbackLocale: 'en',
messages,
numberFormats: {
en: {
currency: {
style: 'currency', currency: 'USD'
}
},
fr: {
currency: {
style: 'currency', currency: 'EUR'
}
}
}
});
In your component:
{{ $n(1000, 'currency') }}
This will format the number as currency according to the locale.
Use named placeholders for dynamic content:
const messages = {
en: {
greeting: 'Hello, {name}!'
},
fr: {
greeting: 'Bonjour, {name}!'
}
};
In your component:
{{ $t('greeting', { name: 'John' }) }}
This will output “Hello, John!” or “Bonjour, John!” based on the locale.
If your translations contain HTML, use the v-html
directive:
const messages = {
en: {
welcome: 'Welcome to our site!'
},
fr: {
welcome: 'Bienvenue sur notre site!'
}
};
In your component:
Ensure that translations are accurate and contextually correct. Use unit tests to verify translations:
import { createI18n } from 'vue-i18n';
import { mount } from '@vue/test-utils';
import App from '@/App.vue';
const messages = {
en: { welcome: 'Welcome' },
fr: { welcome: 'Bienvenue' }
};
test('renders welcome message in English', () => {
const i18n = createI18n({ locale: 'en', messages });
const wrapper = mount(App, { global: { plugins: [i18n] } });
expect(wrapper.text()).toContain('Welcome');
});
test('renders welcome message in French', () => {
const i18n = createI18n({ locale: 'fr', messages });
const wrapper = mount(App, { global: { plugins: [i18n] } });
expect(wrapper.text()).toContain('Bienvenue');
});
Use Vue DevTools and console logging to debug issues with internationalization. Ensure that locale messages are loaded correctly and that the current locale is set properly.
Internationalization is a critical aspect of modern web applications, enabling you to reach a global audience and provide a better user experience. Vue I18n offers a comprehensive set of tools to handle translations, formatting, and locale management in Vue.js applications. By following the practices outlined in this chapter, you can ensure that your Vue.js application is fully internationalized and ready to cater to users from different linguistic and cultural backgrounds. Happy coding !❤️