chore: restructure project and add i18n support
- Reorganize project structure with new electron and shared directories - Add comprehensive i18n support with Chinese, English, and Japanese locales - Update build configurations and TypeScript paths for new structure - Add various UI components including chat interface and task management - Include Windows release binaries and localization files - Update dependencies and fix import paths throughout the codebase
This commit is contained in:
103
src/composables/useLocale.ts
Normal file
103
src/composables/useLocale.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { useLocaleStore } from '@src/store/locale';
|
||||
import { SUPPORTED_LANGUAGES, type LanguageCode, type Namespace } from '@src/i18n/constants';
|
||||
|
||||
/**
|
||||
* 类型安全的国际化 composable
|
||||
* 提供翻译函数、语言切换和状态管理
|
||||
*/
|
||||
export function useLocale() {
|
||||
const { t, te, tm, rt, d, n, ...i18nApi } = useI18n();
|
||||
const localeStore = useLocaleStore();
|
||||
|
||||
/**
|
||||
* 类型安全的翻译函数
|
||||
* @param key 翻译键,格式为 'namespace.key' 或 'key'(使用默认命名空间)
|
||||
* @param params 插值参数
|
||||
*/
|
||||
const translate = (key: string, params?: Record<string, any>): string => {
|
||||
return t(key, params);
|
||||
};
|
||||
|
||||
/**
|
||||
* 检查翻译键是否存在
|
||||
*/
|
||||
const hasTranslation = (key: string): boolean => {
|
||||
return te(key);
|
||||
};
|
||||
|
||||
/**
|
||||
* 切换语言
|
||||
*/
|
||||
const switchLanguage = async (language: LanguageCode) => {
|
||||
await localeStore.setLanguage(language);
|
||||
};
|
||||
|
||||
/**
|
||||
* 切换语言(轮换)
|
||||
*/
|
||||
const toggleLanguage = async () => {
|
||||
await localeStore.toggleLanguage();
|
||||
};
|
||||
|
||||
/**
|
||||
* 重置为系统语言
|
||||
*/
|
||||
const resetToSystemLanguage = async () => {
|
||||
await localeStore.resetToSystemLanguage();
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取当前语言代码
|
||||
*/
|
||||
const currentLanguage = () => localeStore.currentLanguage;
|
||||
|
||||
/**
|
||||
* 获取当前语言标签
|
||||
*/
|
||||
const currentLanguageLabel = () => localeStore.languageLabel;
|
||||
|
||||
/**
|
||||
* 获取支持的语言列表
|
||||
*/
|
||||
const supportedLanguages = () => SUPPORTED_LANGUAGES;
|
||||
|
||||
/**
|
||||
* 初始化语言设置(如果尚未初始化)
|
||||
*/
|
||||
const initLocale = async () => {
|
||||
await localeStore.init();
|
||||
};
|
||||
|
||||
return {
|
||||
// 翻译函数
|
||||
t: translate,
|
||||
te: hasTranslation,
|
||||
tm: i18nApi.tm,
|
||||
rt: i18nApi.rt,
|
||||
d: i18nApi.d,
|
||||
n: i18nApi.n,
|
||||
|
||||
// 语言切换
|
||||
switchLanguage,
|
||||
toggleLanguage,
|
||||
resetToSystemLanguage,
|
||||
|
||||
// 状态获取
|
||||
currentLanguage,
|
||||
currentLanguageLabel,
|
||||
supportedLanguages,
|
||||
|
||||
// 初始化
|
||||
initLocale,
|
||||
|
||||
// 原始 i18n API
|
||||
...i18nApi,
|
||||
|
||||
// store 引用(谨慎使用)
|
||||
localeStore,
|
||||
};
|
||||
}
|
||||
|
||||
// 导出类型
|
||||
export type { LanguageCode, Namespace };
|
||||
Reference in New Issue
Block a user