vben5项目中切换国际化时项目刷新

我使用vben5进行前端代码开发时,有一个需求,每次切换语言时,我都要求项目要重新刷新一遍,这样才能进行后端的语言切换也好,前端国际化的展示也好,我都需要实现这个功能。

我之前一直都是在apps/web-and/src/locales/index.ts文件中进行语言的切换,但是,我发现在这个文件中进行语言切换时,每次切换国际化之后都不起作用。后来发现是切换语言的时候又刷新了页面,所有导致切换好的语言又被重置了,所以,我发现不能再这个文件中进行切换,我们应该在。

packages\effects\layouts\src\widgets\language-toggle.vue

我们应该在这个文件中进行国际化语言的切换才对。

相应的修改代码如下:

复制代码
<script setup lang="ts">
import type { SupportedLanguagesType } from '@vben/locales';

import { SUPPORT_LANGUAGES } from '@vben/constants';
import { Languages } from '@vben/icons';
import { loadLocaleMessages } from '@vben/locales';
import { preferences, updatePreferences } from '@vben/preferences';

import { VbenDropdownRadioMenu, VbenIconButton } from '@vben-core/shadcn-ui';
import { nextTick, ref } from 'vue';

defineOptions({
  name: 'LanguageToggle',
});

// 添加加载状态,防止重复点击
const loading = ref(false);

async function handleUpdate(value: string | undefined) {
  if (!value) {
    console.warn('接收到空值,跳过语言切换');
    return;
  }
  
  const locale = value as SupportedLanguagesType;
  
  // 如果正在加载或者已经是当前语言,则跳过
  if (loading.value || preferences.app.locale === locale) {
    return;
  }

  
  try {
    loading.value = true;
    
    // 1. 更新偏好设置
    updatePreferences({
      app: {
        locale,
      },
    });
    
    // 2. 加载语言包
    await loadLocaleMessages(locale);
    
    // 3. 等待下一个tick确保DOM更新完成
    await nextTick();
    
    // 4. 刷新页面以重新加载所有组件
    // 这里使用setTimeout是为了确保所有的状态更新和DOM更新都已完成
    setTimeout(() => {
      window.location.reload();
    }, 100);
      console.log('国际化手动切换成功', value);
  } catch (error) {
    console.error('切换语言失败:', error);
    // 如果切换失败,恢复原来的语言设置
    updatePreferences({
      app: {
        locale: preferences.app.locale,
      },
    });
  } finally {
    
    loading.value = false;
  }
}
</script>

<template>
  <div>
    <VbenDropdownRadioMenu
      :menus="SUPPORT_LANGUAGES"
      :model-value="preferences.app.locale"
      :disabled="loading"
      @update:model-value="handleUpdate"
    >
      <VbenIconButton 
        class="hover:animate-[shrink_0.3s_ease-in-out]"
        :disabled="loading"
      >
        <Languages class="text-foreground size-4" />
        <!-- 添加加载指示器 -->
        <span 
          v-if="loading" 
          class="absolute top-0 right-0 w-2 h-2 bg-primary rounded-full animate-ping"
        ></span>
      </VbenIconButton>
    </VbenDropdownRadioMenu>
  </div>
</template>
相关推荐
gc_229912 小时前
使用VS Code终端创建Vue项目报错:“npm : 无法加载文件 ...\npm.ps1,因为在此系统上禁止运行脚本”
vue·vs code
小阿鑫16 小时前
尤雨溪 : Vue 4 已经在规划中了!
vue·vite·vite+·程序员海军·vue4
离歌漠1 天前
《我用 Tauri 做了一个本地开发工具箱》
rust·typescript·vue
enjoy编程2 天前
playwright学习-I
typescript·vue·nodejs·1024程序员节·playwright
落花看风雪4 天前
从零开始搭建后台系统(vue3+TypeScript+Vite)01
typescript·vue·js
那年那棵树8 天前
基于WebGis的人生如旅地图系统
前端·arcgis·vue·课程设计
冒险的豆子12 天前
Vue2 、Vue3 响应式对比及优劣
vue
NetX行者12 天前
用 Vite + Vue3 + UnoCSS 搭建企业级项目模板(一):基础骨架
vue·vite·unocss
java水泥工13 天前
什么是前端
java·vue·html5
mont evergreen14 天前
一封从外部世界寄来的信:二维码数据如何穿越三重门,抵达你的屏幕
electron·vue·事件