Vue I18n is a popular internationalization (i18n) library and progressive JavaScript framework for Vue.js.
Internationalization is the process of designing and adapting a software application to support multiple languages and regions.
Before you get started, ensure that you have the following prerequisites installed on your system:
npm or Yarn package manager
The following is the required client library that needed to be installed:
Install Vue I18n package from the npm registry to project's node_modules directory, and make it available for use in JavaScript code.
Set up vue-i18n
Step 1 : Create Language files and folder
Create a folder named Language to store the language JavaScript file
Next, Create language JavaScript files inside the Language folder act as dictionary to store translation words for different languages.
For example, Chinese and English as two supported languages.
Step 2 : Set up vue-i18n
Create a file name i18n.js to set up vue-i18n
Step 3 : Initialize vue-i18n
Inside main.js file, import thei18n.js file as i18n.
Then, set the Vue app use the vue-i18n plugin
Now, we can use the vue-i18n in our Vue app !
Example of Vue i18n Usage
Example 1 : Use in HTML
{{ $t('loginPage.title') }}:
This expression uses the $t directive to access and display a translated message.
The argument 'loginPage.title' refers to a specific translation key.
Vue I18n will replace this key with the appropriate translated text based on the current locale set in the application.
{{ $t('loginPage.companyName') }}:
Similar to the first expression, this one accesses the translation message associated with the key 'loginPage.companyName' and displays it in the component.
Example 2 : use in Script tag
Sometime we need the translation word as message inside the method in <script> tag.
Here is the way on how to use vue-i18n: