Home > Article > Backend Development > How to use PHP and Vue to implement multi-language switching function
How to use PHP and Vue to implement multi-language switching function
In today's Internet era, multi-language functionality has become a need that cannot be ignored. Whether it is a website or a mobile application, you will be faced with the need to support multiple languages. This article will introduce how to use PHP and Vue to implement multi-language switching function, and provide specific code examples.
For example, create a file named "zh_CN.json" with the following content:
{ "hello": "你好", "welcome": "欢迎" }
At the same time, you can also create a file named "en_US.json" with the following content As follows:
{ "hello": "Hello", "welcome": "Welcome" }
More language files can be created as needed.
<template> <div> <p>{{ $t('hello') }}</p> <p>{{ $t('welcome') }}</p> </div> </template> <script> export default { name: "Translation" } </script>
In the above code, we use the $t
directive to get the translated text. This is a built-in directive provided by the Vue-i18n library.
npm install vue-i18n --save
In the entry file of the Vue project, we need to add the following code:
import Vue from 'vue'; import VueI18n from 'vue-i18n'; Vue.use(VueI18n); const i18n = new VueI18n({ locale: 'zh_CN', // 默认语言 messages: { zh_CN: require('@/lang/zh_CN.json'), // 导入中文语言文件 en_US: require('@/lang/en_US.json') // 导入英文语言文件 } }); new Vue({ router, i18n, render: h => h(App) }).$mount('#app');
In the above code , we first need to import the VueI18n library and register it into the Vue instance. Then, by creating a new VueI18n instance, we set the default language to "zh_CN" and imported the Chinese and English language files.
In the Translation.vue component, add the code for the drop-down menu:
<template> <div> <select v-model="$i18n.locale" @change="handleChange"> <option value="zh_CN">中文</option> <option value="en_US">English</option> </select> <p>{{ $t('hello') }}</p> <p>{{ $t('welcome') }}</p> </div> </template> <script> export default { name: "Translation", methods: { handleChange() { // ... } } } </script>
In the above code, we used the v-model
directive to pull down the menu The value of the menu is bound to $i18n.locale
, which is a built-in property provided by the Vue-i18n library. Then, we can listen to the language switching event in the handleChange
method and switch the content of the page according to the selected language.
handleChange
method, we need to refresh the content by reloading the page to display the translation results of the selected language. We can use the window.location.reload()
method to reload the page. handleChange() { window.location.reload(); }
Through the above steps, we successfully implemented the multi-language switching function using PHP and Vue. In actual projects, we can add more language files as needed and implement multi-language switching through the translation instructions and attributes provided in the Vue-i18n library. At the same time, we can also customize the drop-down menu for language selection to be more beautiful. The implementation of the multi-language switching function provides a better user experience for users of different languages and lays the foundation for the internationalization of the project.
(Note: The above is just a simple example, actual projects may require more configuration and function implementation)
The above is the detailed content of How to use PHP and Vue to implement multi-language switching function. For more information, please follow other related articles on the PHP Chinese website!