当前位置:   article > 正文

记录I18n、@intlify/vite-plugin-vue-i18n、ElementPlus的那些事儿_@intlify/unplugin-vue-i18n/vite

@intlify/unplugin-vue-i18n/vite

先上需求:如何在不刷新页面的情况下,切换elementplus组件库的国际化

 

本人用到的插件:I18n,elementPlus,@intlify/vite-plugin-vue-i18n

首先:直接使用全局配置ConfigProvider组件

代码如下:

  1. import zhCn from 'element-plus/lib/locale/lang/zh-cn';
  2. <ElConfigProvider :locale="zhCn">
  3. <RouterView></RouterView>
  4. </ElConfigProvider>

嗯,这样就可以全局配置了,but这玩意儿是静态的,而且只能引用官方的国际化文件,并且无法切换。

顺便提下@intlify/vite-plugin-vue-i18n插件,vite.config.ts中的配置代码如下:

  1. import path from 'path'
  2. import { defineConfig } from 'vite'
  3. import vue from '@vitejs/plugin-vue'
  4. import vueI18n from '@intlify/vite-plugin-vue-i18n'
  5. export default defineConfig({
  6. plugins: [
  7. vue(), // you need to install `@vitejs/plugin-vue`
  8. vueI18n({
  9. // if you want to use Vue I18n Legacy API, you need to set `compositionOnly: false`
  10. // compositionOnly: false,
  11. // you need to set i18n resource including paths !
  12. include: path.resolve(__dirname, '../locales/**')
  13. })
  14. ]
  15. })

这玩意儿可以直接加载整个文件夹内的配置文件,如

会自动加载locales文件夹中的四个json国际化文件。

新建一个lang.ts文件

  1. import { createI18n } from 'vue-i18n';
  2. import messages from '@intlify/vite-plugin-vue-i18n/messages';
  3. const i18n = createI18n({
  4. locale: 'en-US',
  5. globalInjection: true,
  6. messages,
  7. });
  8. export default i18n;

main.ts中使用国际化

  1. // i18n
  2. import i18n from './utils/lang';
  3. ...
  4. app.use(i18n) // 直接使用即可
  5. ...

国际化简单的配置好了,那如何才能动态切换elementplus组件库的国际化呢

首先我们在App.vue中引用

  1. import localesList from '@intlify/vite-plugin-vue-i18n/messages';
  2. ...
  3. // 这里引用I18n
  4. const { t, availableLocales, locale } = useI18n();

其次我们再新建一个计算属性 elementlocale

  1. const toJson = (json: any, k: string) => {
  2. const result: any = {};
  3. // eslint-disable-next-line no-restricted-syntax
  4. for (const key in json) {
  5. if (Object.prototype.hasOwnProperty.call(json, key)) {
  6. const element = json[key];
  7. if (typeof element === 'object') {
  8. result[key] = toJson(element, `${k}.${key}`)
  9. } else {
  10. result[key] = t(`${k}.${key}`);
  11. }
  12. }
  13. }
  14. return result;
  15. };
  16. const elementlocale = computed(()=>{
  17. const obj = {
  18. name: locale.value,
  19. el: toJson(localesList[locale.value].lang.el)
  20. };
  21. return obj;
  22. })

这里不得不说一下,为什么会有toJson这个函数,因为 @intlify/vite-plugin-vue-i18n/messages 获取到的国际化内容格式是这样的

对比了一下elementplus自带的国际化文件的数据

发现i18n国际化中多了一层source这个属性,debugger看了下源码,这里是直接返回一个函数,不是个字符串,导致之前怎么切换都无效果,所以这里我写了一个toJson函数转化了一下

到这里就好了,把之前写的计算属性放到ConfigProvider中,后续只需要在其他页面更改locale.value即可,完整使用代码:

  1. <script setup lang="ts">
  2. import localesList from '@intlify/vite-plugin-vue-i18n/messages';
  3. ...
  4. const { t, availableLocales, locale } = useI18n();
  5. ...
  6. // i18n转json
  7. const toJson = (json: any) => {
  8. let result:any = {};
  9. for (const key in json) {
  10. if (Object.prototype.hasOwnProperty.call(json, key)) {
  11. const element = json[key];
  12. if(element.source){
  13. result[key] = element.source;
  14. }else{
  15. result[key] = toJson(element);
  16. }
  17. }
  18. }
  19. return result;
  20. }
  21. // 计算属性,监听locale
  22. const elementlocale = computed(()=>{
  23. const obj = {
  24. name: locale.value,
  25. el: toJson(localesList[locale.value].lang.el)
  26. };
  27. return obj;
  28. });
  29. </script>
  30. <template>
  31. <ElConfigProvider :locale="elementlocale">
  32. <RouterView></RouterView>
  33. </ElConfigProvider>
  34. </template>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/332229
推荐阅读
相关标签
  

闽ICP备14008679号