• Vue3 + Element Plus + i18n 国际化


    1 前言

    本篇旨在 Vue3 + Element Plus 国际化配置,支持多语言切换

    2 Vue3 国际化

    2.1 安装 vue-i18n

    npm i vue-i18n
    

    2.2 新建 locales 文件夹

    简体中文英文为例

    • 在 src 下新建 locales 文件夹
    • 在 locales 文件夹下新建 zh-cn.ts
    1. export default {
    2. buttons: {
    3. login: '登录'
    4. },
    5. menus: {
    6. home: '首页'
    7. }
    8. }
    • 在 locales 文件夹下新建 en.ts
    1. export default {
    2. buttons: {
    3. login: 'Login'
    4. },
    5. menus: {
    6. home: 'Home'
    7. }
    8. }
    • 在 locales 文件夹下新建 index.ts
    1. import { createI18n } from 'vue-i18n'
    2. import EN from './en'
    3. import ZH from './zh'
    4. const messages = {
    5. en: {
    6. ...EN
    7. },
    8. zh: {
    9. ...ZH
    10. }
    11. }
    12. const getCurrentLanguage = () => {
    13. //设置
    14. const UAlang = navigator.language // zh-CN
    15. const langCode = UAlang.indexOf('zh') !== -1 ? 'zh' : 'en'
    16. localStorage.setItem('lang', langCode)
    17. return langCode
    18. }
    19. const i18n = createI18n({
    20. legacy: false,
    21. globalInjection: true,
    22. locale: getCurrentLanguage() || 'zh', //去getCurrentLanguage函数找有没有设置的语言,默认选择zh
    23. messages: messages
    24. })
    25. export default i18n

    2.3 注册 i18n

    在 main.ts 文件下注册 i18n

    1. import { createApp } from 'vue'
    2. import App from './App.vue'
    3. import i18n from './locales'
    4. const app = createApp(App)
    5. app.use(i18n)
    6. app.mount('#app')

    vue组件按钮切换

    1. <!-- 切换语言 -->
    2. <template>
    3. <!-- command用法很简单,绑定在el-dropdown-item标签的"zh"值和"en",让其发生变化时候做出其他操作,类似监听标签属性 -->
    4. <el-dropdown @command="handleCommand">
    5. <!-- 这里的用的icon是阿里巴巴的-iconfont -->
    6. <span v-if="currentLanguage === 'zh'" size="30" class="iconfont icon-zhongyingwenqiehuan-zhongwen"></span>
    7. <span v-if="currentLanguage === 'en'" size="30" class="iconfont icon-zhongyingwenqiehuan-yingwen"></span>
    8. <template #dropdown>
    9. <el-dropdown-menu>
    10. <!-- command绑定值"zh" disabled是标签属性,不然选择-->
    11. <el-dropdown-item command="zh" :disabled="currentLanguage === 'zh'"
    12. >中文</el-dropdown-item>
    13. <!-- command绑定值"zh" disabled是标签属性,不然选择-->
    14. <el-dropdown-item command="en" :disabled="currentLanguage === 'en'"
    15. >English</el-dropdown-item
    16. >
    17. </el-dropdown-menu>
    18. </template>
    19. </el-dropdown>
    20. </template>
    21. <script lang="ts" setup>
    22. import { useI18n } from "vue-i18n";
    23. import { computed, ref } from "vue";
    24. import { useStore } from "vuex";
    25. const i18n = useI18n();
    26. const store = useStore();
    27. //获取当前的语言是什么,让页面标签el-dropdown-item变成灰色不然点击,只能让其点击没选中的
    28. const currentLanguage = computed(() => {
    29. return i18n.locale.value;
    30. });
    31. //command用法很简单,绑定在el-dropdown-item标签的"zh"值和"en",让其发生变化时候做出其他操作,类似监听标签属性
    32. //和在便签中定义函数传参类似的
    33. const handleCommand = (val:string) => {
    34. //command获取值切换语音
    35. i18n.locale.value = val;
    36. };
    37. </script>
    38. <style lang="scss" scoped>
    39. .el-dropdown {
    40. position: absolute;
    41. right: 52px;
    42. }
    43. .icon {
    44. width: 1em;
    45. height: 1em;
    46. vertical-align: -0.15em;
    47. fill: currentColor;
    48. overflow: hidden;
    49. }
    50. </style>

    2.4 使用方法

    2.4.1 在 template 中的使用

    1. {{ $t('menus.home') }}

    2.4.2 在 ts 中的使用

    1. import i18n from './locales'
    2. console.log(i18n.global.t('menus.home'))

    3 Element Plus 国际化

    Element Plus 官方提供了一个 Vue 组件 ConfigProvider 用于全局配置国际化的设置
    el-config-provider 由 Element Plus 按需引入 - 自动导入
    el-config-provider 手动导入:import { ElConfigProvider } from 'element-plus'

    1. <template>
    2. <el-config-provider :locale="useAppStoreHook().locale === 'zhCn' ? zhCn : en">
    3. <app />
    4. </el-config-provider>
    5. </template>
    6. <script lang="ts" setup>
    7. import zhCn from 'element-plus/lib/locale/lang/zh-cn'
    8. import en from 'element-plus/lib/locale/lang/en'
    9. import { useAppStoreHook } from '@/store/modules/app' //store存放语言配置
    10. </script>

    4 语言切换

    切换语言时,修改 store 、 localstorage 和 i18n 中的语言配置

    1. // store/modules/app
    2. import { defineStore } from 'pinia'
    3. import { store } from '@/store'
    4. import i18n from '@/locales'
    5. const useAppStore = defineStore('app', {
    6. state: () => {
    7. return {
    8. locale: localStorage.getItem('lang') || 'zhCn'
    9. }
    10. },
    11. actions: {
    12. SET_LOCALE(locale: string) { //语言切换
    13. this.locale = locale
    14. storageLocal.setItem('lang', locale)
    15. i18n.global.locale.value = locale
    16. }
    17. }
    18. })
    19. export function useAppStoreHook() {
    20. return useAppStore(store)
    21. }

    遇到问题:

    使用i18n控制台警告信息:
    You are running the esm-bundler build of vue-i18n. It is recommended to configure your bundler to explicitly replace feature flag globals with boolean literals to get proper tree-shaking in the final bundle.

    解决:
    在vue.config.js配置中添加如下:

    1. chainWebpack: config => {
    2.     config.resolve.alias.set('vue-i18n', 'vue-i18n/dist/vue-i18n.cjs.js')
    3. }

  • 相关阅读:
    个人微信号管理工具哪个好?
    C认证笔记 - 计算机通识 - IP基础
    Linux多线程编程- 无名信号量
    【一】1D测量 Measuring——1.1 close_measure()、close_all_measure()算子
    【PyTorch深度学习项目实战100例】—— 基于RNN+CNN实现NLP判别新闻真伪 | 第45例
    算法——二分查找
    文件中的关键字与对应的协议
    Mysql学习之——增删改查语句
    拼多多回应六万人砍价不成功:不实;苹果回应iOS 15.4正式版续航翻车;AMD FSR 2.0 即将面世|极客头条
    华为OD机试之租车骑绿岛(Java源码)
  • 原文地址:https://blog.csdn.net/qq_38567039/article/details/127963793