当前位置:   article > 正文

Vue3后台管理系统(七)Element-Plus国际化_element plus国际化

element plus国际化

Element Plus 官方提供全局配置 Config Provider实现国际化

一、配置:

在src/store/modules文件夹下新建app文件,然后在app文件夹下新建index.ts内容如下

  1. // src/store/modules/app/index.ts
  2. import {
  3. getSidebarStatus,
  4. setSidebarStatus,
  5. getSize,
  6. setSize,
  7. setLanguage
  8. } from '@/utils/localStorage';
  9. import {defineStore} from 'pinia';
  10. import {getLanguage} from '@/lang';
  11. import {computed, reactive, ref} from 'vue';
  12. import {useStorage} from '@vueuse/core';
  13. // Element Plus 语言包
  14. import zhCn from 'element-plus/es/locale/lang/zh-cn';
  15. import en from 'element-plus/es/locale/lang/en';
  16. export enum DeviceType {
  17. mobile,
  18. desktop
  19. }
  20. export enum SizeType {
  21. default,
  22. large,
  23. small
  24. }
  25. // setup
  26. export const useAppStore = defineStore('app', () => {
  27. // state
  28. const device = useStorage<string>('device', 'desktop');
  29. const size = ref(getSize() || 'default');
  30. const language = ref(getLanguage());
  31. const sidebar = reactive({
  32. opened: getSidebarStatus() !== 'closed',
  33. withoutAnimation: false
  34. });
  35. const locale = computed(() => {
  36. if (language?.value == 'en') {
  37. return en;
  38. } else {
  39. return zhCn;
  40. }
  41. });
  42. // actions
  43. function toggleSidebar(withoutAnimation: boolean) {
  44. sidebar.opened = !sidebar.opened;
  45. sidebar.withoutAnimation = withoutAnimation;
  46. if (sidebar.opened) {
  47. setSidebarStatus('opened');
  48. } else {
  49. setSidebarStatus('closed');
  50. }
  51. }
  52. function closeSideBar(withoutAnimation: boolean) {
  53. sidebar.opened = false;
  54. sidebar.withoutAnimation = withoutAnimation;
  55. setSidebarStatus('closed');
  56. }
  57. function openSideBar(withoutAnimation: boolean) {
  58. sidebar.opened = true;
  59. sidebar.withoutAnimation = withoutAnimation;
  60. setSidebarStatus('opened');
  61. }
  62. function toggleDevice(val: string) {
  63. device.value = val;
  64. }
  65. function changeSize(val: string) {
  66. size.value = val;
  67. setSize(val);
  68. }
  69. function changeLanguage(val: string) {
  70. language.value = val;
  71. setLanguage(val);
  72. }
  73. return {
  74. device,
  75. sidebar,
  76. language,
  77. locale,
  78. size,
  79. toggleDevice,
  80. changeSize,
  81. changeLanguage,
  82. toggleSidebar,
  83. closeSideBar,
  84. openSideBar
  85. };
  86. });

二、全局化

修改App.vue为:

  1. <!--src/App.vue-->
  2. <script setup lang="ts">
  3. import { ElConfigProvider } from 'element-plus';
  4. import { useAppStore } from '@/store/modules/app';
  5. const appStore = useAppStore();
  6. </script>
  7. <template>
  8. <el-config-provider :locale="appStore.locale" :size="appStore.size">
  9. <router-view />
  10. </el-config-provider>
  11. </template>

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

闽ICP备14008679号