Files
zn-ai/src/composables/useLocale.ts
duanshuwen e77c815a86 feat: add new stores for cron, locale, providers, script, shared data, skills, and user info
- Implemented `cron` store to manage scheduled tasks with CRUD operations.
- Created `locale` store for language settings with persistence and system language detection.
- Added `providers` store to handle provider accounts and configurations with API interactions.
- Developed `script` store for managing automation scripts, including recording and execution.
- Introduced `sharedStore` for managing shared data across components.
- Established `skills` store for fetching, installing, and managing skills from a marketplace.
- Created `userinfo` store for user authentication and session management.

chore: update path aliases from `@store` to `@stores` in TypeScript configuration and Vite config
2026-04-15 21:49:25 +08:00

103 lines
2.1 KiB
TypeScript

import { useI18n } from 'vue-i18n';
import { useLocaleStore } from '@src/stores/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 };