vue3接入语言包
1、下载组件:vue-i18n
2、文件:main.js
JavaScript Code复制内容到剪贴板
- import i18n from './locales'
- app.use(i18n)
3、公共头部文件
XML/HTML Code复制内容到剪贴板
- <template>
- <el-menu
- :default-active="activeIndex"
- mode="horizontal"
- :router="true"
- :ellipsis="false"
- @select="handleSelectPc"
- >
- <el-menu-item index="/">{{ $t('header.menu1') }}</el-menu-item>
- <el-menu-item index="/energy">{{ $t('header.menu2') }}</el-menu-item>
- <el-menu-item index="/product">{{ $t('header.menu3') }}</el-menu-item>
- <el-menu-item index="/case">{{ $t('header.menu4') }}</el-menu-item>
- <el-menu-item index="/partner">{{ $t('header.menu5') }}</el-menu-item>
- <el-menu-item index="/resource">{{ $t('header.menu6') }}</el-menu-item>
- <el-menu-item index="/about">{{ $t('header.menu7') }}</el-menu-item>
- <el-menu-item index="/ucenter">{{ $t('header.menu8') }}</el-menu-item>
- </el-menu>
- <el-dropdown trigger="click" placement="bottom-end" @command="configLang">
- <el-button circle>
- <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 512 512"><path d="M478.33 433.6l-90-218a22 22 0 0 0-40.67 0l-90 218a22 22 0 1 0 40.67 16.79L316.66 406h102.67l18.33 44.39A22 22 0 0 0 458 464a22 22 0 0 0 20.32-30.4zM334.83 362L368 281.65L401.17 362z" fill="currentColor"></path><path d="M267.84 342.92a22 22 0 0 0-4.89-30.7c-.2-.15-15-11.13-36.49-34.73c39.65-53.68 62.11-114.75 71.27-143.49H330a22 22 0 0 0 0-44H214V70a22 22 0 0 0-44 0v20H54a22 22 0 0 0 0 44h197.25c-9.52 26.95-27.05 69.5-53.79 108.36c-31.41-41.68-43.08-68.65-43.17-68.87a22 22 0 0 0-40.58 17c.58 1.38 14.55 34.23 52.86 83.93c.92 1.19 1.83 2.35 2.74 3.51c-39.24 44.35-77.74 71.86-93.85 80.74a22 22 0 1 0 21.07 38.63c2.16-1.18 48.6-26.89 101.63-85.59c22.52 24.08 38 35.44 38.93 36.1a22 22 0 0 0 30.75-4.9z" fill="currentColor"></path></svg>
- </el-button>
- <template #dropdown>
- <el-dropdown-menu>
- <el-dropdown-item v-for="item in langList" :key="item.value" :command="item" :class="{'selected':lang === item.value}">{{item.name}}</el-dropdown-item>
- </el-dropdown-menu>
- </template>
- </el-dropdown>
- </template>
- <script setup>
- import { useRoute } from 'vue-router'
- import { getCurrentInstance, ref, watch } from 'vue'
- const { proxy } = getCurrentInstance()
- const $TOOL = proxy.$TOOL
- const route = useRoute()
- const lang = ref($TOOL.data.get('APP_LANG') || $CONFIG.LANG)
- const langList = ref([
- {
- name: '简体中文',
- value: 'zh-cn',
- },
- {
- name: 'English',
- value: 'en',
- }
- ])
- watch(
- () => lang,
- (currentVal, oldVal) => {
- proxy.$i18n.locale = currentVal.value
- console.log('设置当前语言', proxy.$i18n.locale)
- $TOOL.data.set("APP_LANG", currentVal.value)
- },
- {
- deep:true
- }
- );
- const configLang = (command) => {
- console.log(command.value)
- lang.value = command.value
- }
- ...
- </script>
4、App.vue
XML/HTML Code复制内容到剪贴板
- <template>
- <ElConfigProvider :locale="locale">
- ...
- </ElConfigProvider>
- </template>
- <script setup>
- import { ref, computed, onMounted, getCurrentInstance } from 'vue'
- const { proxy } = getCurrentInstance()
- const locale = computed(() => {
- // console.log('获取当前语言', proxy.$i18n.locale)
- return proxy.$i18n.messages[proxy.$i18n.locale].el
- });
- ...
- </script>
5、使用:
XML/HTML Code复制内容到剪贴板
- <template>
- <p class="t1">{{ $t('computer.title') }}</p>
- <p class="t2">{{ $t('computer.subTitle') }}</p>
- </template>
- <script setup>
- import { getCurrentInstance, inject, onMounted, ref } from 'vue'
- const { proxy } = getCurrentInstance()
- const $t = inject('$t')
- const dialogFormTitle1 = ref($t('computer.otherPop.title'))
ant design vue 版本:
1、main.js:
JavaScript Code复制内容到剪贴板
- // 全局语言包
- import i18n from './locales'
- const app = createApp(App)
- // ...
- app.use(i18n)
2、App.vue
XML/HTML Code复制内容到剪贴板
- <script setup>
- import { computed, getCurrentInstance, ref } from 'vue'
- const { proxy } = getCurrentInstance()
- const locale = computed(() => {
- console.log('获取当前语言', proxy.$i18n.locale)
- return proxy.$i18n.messages[proxy.$i18n.locale]
- })
- </script>
- <template>
- <a-config-provider
- :locale="locale"
- >
- <RouterView/>
- </a-config-provider>
- </template>
3、使用:
模板直接使用:{{ $t('self.login.title') }}
XML/HTML Code复制内容到剪贴板
- <script setup>
- import { getCurrentInstance, ref } from 'vue'
- const { proxy } = getCurrentInstance()
- const lang = ref($TOOL.data.get('APP_LANG') || $CONFIG.LANG)
- const langList = ref([
- {
- name: '中文',
- value: 'zh-cn',
- },
- {
- name: '英文',
- value: 'en-us',
- },
- {
- name: '卢旺达',
- value: 'lwd',
- }
- ])
- const handleMenuClick = (e) => {
- const language = e.key
- proxy.$i18n.locale = language
- console.log('设置当前语言', proxy.$i18n.locale)
- $TOOL.data.set("APP_LANG", language)
- }
- </script>
- <template>
- <a-dropdown>
- <template #overlay>
- <a-menu @click="handleMenuClick">
- <a-menu-item :key="item.value" v-for="(item, index) in langList">{{ item.name }}</a-menu-item>
- </a-menu>
- </template>
- <GlobalOutlined style="font-size: 18px;" />
- </a-dropdown>
- </template>
在JS部分使用:
JavaScript Code复制内容到剪贴板
- <script setup>
- import { useI18n } from 'vue-i18n'
- const { t } = useI18n()
- console.log(t('self.order.search1.col1'))
语言包: