Home > Article > Web Front-end > How to use Vue and Element-UI for international multi-language processing
How to use Vue and Element-UI for international multi-language processing
Introduction:
In an era of increasing globalization, in order to enable websites and applications to adapt to users in different countries and regions , internationalized multilingual processing is becoming more and more important. Vue is a popular front-end development framework, and Element-UI is a set of UI libraries based on Vue. This article will introduce how to use Vue and Element-UI for international multi-language processing.
1. Install and introduce necessary dependent libraries
First, we need to install and introduce some necessary dependent libraries. In the root directory of the Vue project, open a terminal and run the following command:
npm install vue-i18n npm install vant
Then, in the project's entry file (usually main.js), introduce and use these dependent libraries:
import Vue from 'vue' import VueI18n from 'vue-i18n' import vant from 'vant' import 'vant/lib/index.css' Vue.use(VueI18n) Vue.use(vant) // 初始化i18n实例 const i18n = new VueI18n({ locale: 'zh-CN', // 默认语言 fallbackLocale: 'en', // 如果当前语言包中没有对应的翻译文本,就会使用fallbackLocale中定义的语言包 messages: { 'zh-CN': require('./lang/zh-CN.json'), // 引入中文语言包 'en': require('./lang/en.json') // 引入英文语言包 } }) // 将i18n实例绑定到Vue实例上 new Vue({ el: '#app', i18n, render: h => h(App) })
2. Create language pack files
Create a lang folder in the root directory of the project, and create multiple language pack files in the folder. For example, we create a zh-CN.json file and an en.json file to place the Chinese and English translated texts respectively.
zh-CN.json file example:
{ "hello": "你好", "welcome": "欢迎使用", "button": { "submit": "提交", "cancel": "取消" }, ... }
en.json file example:
{ "hello": "Hello", "welcome": "Welcome", "button": { "submit": "Submit", "cancel": "Cancel" }, ... }
3. Use internationalized text in Vue components
When internationalization is required In the Vue component that is processed, we can use Vue's built-in instructions or filters to obtain the corresponding translated text.
Example of using Vue built-in instructions:
<template> <div> <h2>{{ $t('hello') }}</h2> <p>{{ $t('welcome') }}</p> <button @click="submit">{{ $t('button.submit') }}</button> <button @click="cancel">{{ $t('button.cancel') }}</button> </div> </template> <script> export default { name: 'MyComponent', methods: { submit() { // 提交操作 }, cancel() { // 取消操作 } } } </script>
Example of using Vue filter:
<template> <div> <h2>{{ 'hello' | t }}</h2> <p>{{ 'welcome' | t }}</p> <button @click="submit">{{ 'button.submit' | t }}</button> <button @click="cancel">{{ 'button.cancel' | t }}</button> </div> </template> <script> export default { name: 'MyComponent', filters: { t(key) { return this.$t(key) } }, methods: { submit() { // 提交操作 }, cancel() { // 取消操作 } } } </script>
4. Switching languages
The last step is to implement the function of switching languages. This can be achieved by adding a button or drop-down menu to switch languages.
Example:
<template> <div> <button @click="changeLocale('zh-CN')">中文</button> <button @click="changeLocale('en')">English</button> </div> </template> <script> export default { name: 'LanguageSwitcher', methods: { changeLocale(locale) { this.$i18n.locale = locale } } } </script>
Summary:
This article introduces how to use Vue and Element-UI for international multi-language processing. By installing and introducing relevant dependent libraries, creating language pack files, and using internationalized text in Vue components, we can achieve multi-language adaptation of websites and applications. I hope this article will be helpful to you in international multi-language processing.
The above is the detailed content of How to use Vue and Element-UI for international multi-language processing. For more information, please follow other related articles on the PHP Chinese website!