当前位置:   article > 正文

vue项目中实现国际化_el-config-provider

el-config-provider

1 Element-plus配置国际化:ConfigProvider

 (1)首先需要将国际化的地方包起来,如下

  1. <script setup lang="ts">
  2. // This starter template is using Vue 3 <script setup> SFCs
  3. // Check out https://vuejs.org/api/sfc-script-setup.html#script-setup
  4. import { useRouter } from 'vue-router'
  5. import zhCn from 'element-plus/dist/locale/zh-cn'
  6. import en from 'element-plus/dist/locale/en'
  7. import { ref } from 'vue'
  8. const router = useRouter()
  9. const locale = ref(zhCn)
  10. const changeLang = (language:any)=>{
  11. locale.value = language
  12. }
  13. </script>
  14. <template>
  15. <el-config-provider :locale="locale">
  16. <el-time-picker v-model="value1" placeholder="Arbitrary time" />
  17. <button @click="changeLang(zhCn)">中文</button>
  18. <button @click="changeLang(en)">英文</button>
  19. <button @click="()=> router.push({path: '/home'})">首页</button>
  20. <button @click="()=> router.push({path: '/mine'})">个人中心</button>
  21. <router-link to="/home">首页-router-link实现跳转</router-link>
  22. <router-link to="/mine">个人中心-router-link实现跳转</router-link>
  23. <!-- 作为顶级的视图出口 -->
  24. <router-view/>
  25. </el-config-provider>
  26. </template>
  27. <style lang="scss" scoped>
  28. .logo {
  29. height: 6em;
  30. padding: 1.5em;
  31. will-change: filter;
  32. }
  33. .logo:hover {
  34. filter: drop-shadow(0 0 2em #646cffaa);
  35. }
  36. .logo.vue:hover {
  37. filter: drop-shadow(0 0 2em #42b883aa);
  38. }
  39. #app{
  40. button{
  41. font-size: 20px;
  42. }
  43. }
  44. </style>

 当按钮点击中文时,时间选择组件对应的文字会转换成中文,当点击英文时,时间选择组件对应的文字会转换成英文。

2 Vue-i18n实现网站全局国际化(1 中只能实现局部国际化,使用2可以实现全局国际化)

(1)安装:npm i vue-i18n@next

(2)在项目中新建language文件

其中i18n.ts中内容为

  1. import { createI18n } from 'vue-i18n'
  2. import zh from './zh'
  3. import en from './en'
  4. const i18n = createI18n({
  5. locale: 'zh',
  6. messages: {
  7. zh,
  8. en
  9. }
  10. })
  11. export default i18n

en.ts的内容为:

  1. export default {
  2. message: {
  3. home: 'home',
  4. mine: 'mine'
  5. }
  6. }

 zh.ts的内容为

  1. export default {
  2. message: {
  3. home: '首页',
  4. mine: '个人中心'
  5. }
  6. }

(3)在main.ts中引入

(4)使用:

(5)如何切换语言

 

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读