Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions dashboard/src/i18n/composables.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ref, computed } from 'vue';
import { translations as staticTranslations } from './translations';
import { SUPPORTED_LOCALES, isLocaleSupported, loadLocaleTranslations } from './translations';
import type { Locale } from './types';

// 全局状态
Expand All @@ -12,32 +12,32 @@ const translations = ref<Record<string, any>>({});
export async function initI18n(locale: Locale = 'zh-CN') {
currentLocale.value = locale;

// 加载静态翻译数据
loadTranslations(locale);
// 加载翻译数据
await loadTranslations(locale);
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Outdated
}

/**
* 加载翻译数据(现在从静态导入获取)
*/
function loadTranslations(locale: Locale) {
async function loadTranslations(locale: Locale): Promise<void> {
try {
const data = staticTranslations[locale];
const data = await loadLocaleTranslations(locale);
if (data) {
translations.value = data;
} else {
console.warn(`Translations not found for locale: ${locale}`);
// 回退到中文
if (locale !== 'zh-CN') {
console.log('Falling back to zh-CN');
translations.value = staticTranslations['zh-CN'];
translations.value = await loadLocaleTranslations('zh-CN');
}
}
} catch (error) {
console.error(`Failed to load translations for ${locale}:`, error);
// 回退到中文
if (locale !== 'zh-CN') {
console.log('Falling back to zh-CN');
translations.value = staticTranslations['zh-CN'];
translations.value = await loadLocaleTranslations('zh-CN');
}
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

loadTranslations 函数中,处理加载失败后回退到中文的逻辑在 else 块和 catch 块中重复出现。此外,由于 loadLocaleTranslations 在成功时总是返回一个对象(真值),try 块中的 else 分支实际上是无法访问的死代码。建议重构此函数,以消除重复代码和死代码,使逻辑更清晰。同时,为回退加载本身也增加 try-catch,可以使函数更健壮。

async function loadTranslations(locale: Locale): Promise<void> {
  try {
    translations.value = await loadLocaleTranslations(locale);
  } catch (error) {
    console.error(`Failed to load translations for ${locale}:`, error);
    if (locale !== 'zh-CN') {
      console.log('Falling back to zh-CN');
      try {
        translations.value = await loadLocaleTranslations('zh-CN');
      } catch (fallbackError) {
        console.error('Failed to load fallback translations for zh-CN:', fallbackError);
        translations.value = {};
      }
    }
  }
}

Expand Down Expand Up @@ -85,7 +85,7 @@ export function useI18n() {
const setLocale = async (newLocale: Locale) => {
if (newLocale !== currentLocale.value) {
currentLocale.value = newLocale;
loadTranslations(newLocale);
await loadTranslations(newLocale);

// 保存到localStorage
localStorage.setItem('astrbot-locale', newLocale);
Expand All @@ -103,7 +103,7 @@ export function useI18n() {
const locale = computed(() => currentLocale.value);

// 获取可用语言列表
const availableLocales: Locale[] = ['zh-CN', 'en-US', 'ru-RU'];
const availableLocales: Locale[] = [...SUPPORTED_LOCALES];

// 检查是否已加载
const isLoaded = computed(() => Object.keys(translations.value).length > 0);
Expand Down Expand Up @@ -221,7 +221,7 @@ function deepMerge(target: Record<string, any>, source: Record<string, any>) {
export async function setupI18n() {
// 从localStorage获取保存的语言设置
const savedLocale = localStorage.getItem('astrbot-locale') as Locale;
const initialLocale = savedLocale && ['zh-CN', 'en-US', 'ru-RU'].includes(savedLocale)
const initialLocale = savedLocale && isLocaleSupported(savedLocale)
? savedLocale
: 'zh-CN';

Expand Down
Loading