赞
踩
Element-plus默认语言是英语,可修改为其它语言;
此处 Element-plus 为自动按需导入方式配置;
更多导入方式:Vue3使用Element-plus-CSDN博客
全局配置默认语言参考:国际化 | Element Plus
在 App.vue 引入 Element-plus 语言包,并使用 el-config-provider 标签
- <script setup>
- import { RouterView } from 'vue-router'
- import { ElConfigProvider } from 'element-plus'
- import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
- </script>
-
- <template>
- <el-config-provider :locale="zhCn">
- <RouterView />
- </el-config-provider>
- </template>
仅作学习
- <script setup>
- import { ref, computed } from 'vue'
- import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
- import en from 'element-plus/dist/locale/en.mjs'
-
- const language = ref(zhCn)
- const locale = computed(() => (language.value === 'zh-cn' ? zhCn : en))
-
- const toggle = () => {
- language.value = language.value === 'zh-cn' ? 'en' : 'zh-cn'
- }
- </script>
-
- <template>
- <div>
- <el-button @click="toggle">Switch Language</el-button>
- <br />
- <el-config-provider :locale="locale">
- <el-table :data="[]" />
- <el-pagination :total="100" />
- </el-config-provider>
- </div>
- </template>
使用 vue-i18n 模块
npm i vue-i18n
在 src 目录下创建 locales 目录,里面创建文件:en.js、zh-cn.js、index.js
- // en.js
-
- export default {
- login: {
- title: 'Login Form',
- logIn: 'Login',
- username: 'Username',
- password: 'Password'
- }
- }
- // zh-cn.js
-
- export default {
- login: {
- title: '系统登录',
- logIn: '登录',
- username: '账号',
- password: '密码'
- }
- }
- // index.js
-
- import { createI18n } from 'vue-i18n'
- import elEnLocale from 'element-plus/es/locale/lang/en'
- import elZhLocale from 'element-plus/es/locale/lang/zh-cn'
- import enLocale from './en'
- import zhLocale from './zh-cn'
-
- const messages = {
- en: {
- ...enLocale,
- ...elEnLocale
- },
- 'zh-cn': {
- ...zhLocale,
- ...elZhLocale
- }
- }
-
- export const getLocale = () => {
- // 获取缓存
- const storLanguage = localStorage.getItem('language')
- // 存在返回当前语言
- if (storLanguage) return storLanguage
- // 不存在 获取系统语言
- const language = (navigator.language || navigator.browserLanguage).toLowerCase()
- const locales = Object.keys(messages)
- for (const locale of locales) {
- if (language.indexOf(locale) > -1) {
- return locale
- }
- }
- // 默认返回简体中文
- return 'zh-cn'
- }
- const i18n = createI18n({
- globalInjection: true,
- legacy: false,
- locale: getLocale(),
- messages: messages
- })
- export default i18n
挂载 locales/index.js
- import { createApp } from 'vue'
- import App from './App.vue'
- import router from './router'
- import i18n from '@/locales/index'
-
- const app = createApp(App)
-
- app.use(router)
- app.use(i18n)
-
- app.mount('#app')
在 App.vue 使用 el-config-provider 标签配置
- <script setup>
- import { RouterView } from 'vue-router'
- import { ElConfigProvider } from 'element-plus'
- import { useI18n } from 'vue-i18n'
- const { locale, messages } = useI18n()
- </script>
-
- <template>
- <el-config-provider :locale="messages[locale]">
- <RouterView />
- </el-config-provider>
- </template>
在 components 创建一个 language.vue 组件
- <script setup>
- import { useI18n } from 'vue-i18n'
- const { locale } = useI18n()
- const languages = [
- { name: 'English', value: 'en' },
- { name: '中文', value: 'zh-cn' }
- ]
- const handleSetLanguage = (lang) => {
- locale.value = lang
- localStorage.setItem('language', lang)
- }
- </script>
-
- <template>
- <el-button type="" v-for="item in languages" :key="item.value" @click="handleSetLanguage(item.value)">{{ item.name }}</el-button>
- </template>
在页面 home.vue 使用 language.vue 组件并使用前面配置好的语言;
- <script setup>
- import LangSelect from '@/components/lang_select/langSelect.vue'
- import { reactive } from 'vue'
- import { useI18n } from 'vue-i18n'
- const { t } = useI18n()
- const form = reactive({
- name: ''
- })
- </script>
-
- <template>
- <LangSelect />
-
- <div style="margin: 50px">
- {{ t('login.title') }}
- </div>
- <el-form :model="form" label-width="120px">
- <el-form-item :label="t('login.username')">
- <el-input v-model="form.name" :placeholder="t('login.username')" />
- </el-form-item>
- </el-form>
-
- <div>
- <el-table :data="[]" />
- <el-pagination :total="100" />
- </div>
- </template>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。