Easy Vue internationalization - guide to the Vue I18n plugin
Tomek Poniatowicz
Posted on October 26, 2023
In today's globalized world, it's crucial for web developers to create applications that can be easily localized for users from different regions and cultures. Vue.js is a popular JavaScript framework which provides a powerful internationalization (i18n) plugin called Vue I18n. In this comprehensive guide, we will do a step-by-step exploration of the process of internationalizing a Vue app using the Vue I18n plugin. Whether you're a seasoned Vue developer or just getting started, this guide will get you up to speed on how to create multi-language applications with ease.
What is Vue I18n?
Vue I18n is a localization library for Vue.js that helps developers easily handle application translations. It provides a simple and flexible API to integrate translations into Vue components, which makes creating multi-language apps almost effortless. With Vue I18n, you can define translation messages in different languages and easily switch between them based on the user's locale. It also supports multiple advanced features like dynamic translations or pluralization which makes it a truly comprehensive solution for Vue internationalization.
Getting Started
Before we get to Vue internationalization, we first need to include the necessary scripts in our HTML file. You can include Vue and Vue I18n using either a script tag or a module bundler like Webpack. Here's an example of including the scripts via a script tag:
<script src="https://unpkg.com/vue@3"></script>
<script src="https://unpkg.com/vue-i18n@9"></script>
Now that the scripts are included, we can go ahead and create a Vue instance and configure the Vue I18n plugin. In the following example, we'll define translated messages for English and Japanese:
const messages = {
en: {
message: {
hello: 'Hello, world!'
}
},
ja: {
message: {
hello: 'こんにちは、世界!'
}
}
};
const i18n = VueI18n.createI18n({
locale: 'ja',
fallbackLocale: 'en',
messages
});
const app = Vue.createApp({
// Vue app options
});
app.use(i18n);
app.mount('#app');
In this code snippet, we've defined the translated messages in the messages object, with the keys representing the locales (e.g., en for English and ja for Japanese). We then created an instance of Vue I18n using createI18n and passed in the locale, fallback locale, and messages. Finally, we installed the i18n instance on the Vue app using app.use(i18n) and mounted the app on the DOM element with the ID app. This sets up the Vue app to be aware of the i18n instance and enables the translation functionality.
Translating Text in Templates
Once the setup is done, we can start working on Vue internationalization, first let's go to our templates. Vue I18n injects the $t translation API into each component, letting us easily access translated messages. Here's an example of how to use the $t API in a template:
<div id="app">
<p>{{ $t("message.hello") }}</p>
</div>
In this example, we use the $t API to translate the message with the key "message.hello". The translation is determined automatically based on the current locale set in the Vue I18n instance.
Dynamic Translations
Vue internationalization also supports dynamic translations, enabling passing variables to translated messages. This is useful when you need to include dynamic content in your translations, such as usernames or numbers. Here's an example:
<div id="app">
<p>{{ $t("message.greeting", { name: userName }) }}</p>
</div>
In this example, we have a dynamic message key "message.greeting" and pass an object with a name property to the $t API. The value of userName will be dynamically inserted into the translated message.
Pluralization
Pluralization is a common requirement in internationalization, and Vue internationalization provides built-in support for handling plural forms in translations. You can define different translations for singular and plural forms of a message and let the Vue I18n plugin automatically select the appropriate translation based on the quantity. Here's an example:
const messages = {
en: {
message: {
apple: 'one apple | {count} apples'
}
},
ja: {
message: {
apple: 'りんご一つ | りんご{count}個'
}
}
};
In this example, we define translations for the key "message.apple". The English translation has two forms: "one apple" for singular and "{count} apples" for plural. The Japanese translation also has two forms: "りんご一つ" for singular and "りんご{count}個" for plural. Vue I18n will automatically select the appropriate translation based on the value of count.
Using the Composition API
In addition to the options-based API shown in the previous examples, Vue I18n also has a feature called the Composition API, which provides a more flexible and powerful way to handle translations. To use the Composition API, we need to call the useI18n function in our Vue setup function. Here's an example:
import { useI18n } from 'vue-i18n';
export default {
setup() {
const { t, locale } = useI18n();
return {
t,
locale
};
}
};
In this example, we import the useI18n function from vue-i18n and call it in the setup function of a Vue component. This gives us access to the t translation function and the locale property, which can then be used in the template or other parts of the component.
Advanced Features with Vue I18n
Vue I18n provides a range of advanced features to handle complex translation requirements. Some of the notable features include:
Custom Formatting: You can define custom formatters to format translated values based on your specific requirements.
Fallback Strategies: Vue internationalization allows you to define fallback strategies for missing translations, ensuring a smooth user experience even when translations are not available.
Number and Date Formatting: Vue I18n provides built-in support for formatting numbers and dates according to the user's locale.
Pluralization Rules: You can customize the pluralization rules for different languages, allowing you to handle complex plural forms.
Bonus - How to easily translate your i18n files
To learn more about Vue internationalization and its features, refer to the official documentation of Vue.js. Additionally, you can use tools like DevTranslate to automate the translation process and manage your Vue files efficiently.
Conclusion
In this comprehensive guide, we explored the process of Vue internationalization using the Vue I18n plugin. We learned how to set up the whole process, translate text in templates, handle dynamic translations and pluralization, and use advanced features provided by the plugin. With Vue I18n, developers can easily create multi-language apps that cater to a global audience. By following the steps outlined in this guide and leveraging the power of Vue internationalization, you can ensure a seamless localization experience for your Vue projects.
Posted on October 26, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.