feat(i18n): add internationalization support across multiple components

This commit is contained in:
2025-10-27 14:41:58 +08:00
parent 49742f6219
commit 46cc8a254a
23 changed files with 777 additions and 2623 deletions

View File

@@ -17,8 +17,10 @@ import {
Link,
} from '@mui/material';
import { Controller, useForm } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import logger from '@/utils/logger';
import { LLM_FACTORY_LIST, type LLMFactory } from '@/constants/llm';
import i18n from '@/locales';
// 表单数据接口
export interface OllamaFormData {
@@ -64,7 +66,7 @@ const llmFactoryToUrlMap: { [x: string]: string } = {
function getURLByFactory(factory: LLMFactory) {
const url = llmFactoryToUrlMap[factory];
return {
textTip: `如何集成 ${factory}`,
textTip: `${i18n.t('setting.howToIntegrate')} ${factory}`,
url,
}
}
@@ -91,6 +93,7 @@ function OllamaDialog({
initialData,
editMode = false,
}: OllamaDialogProps) {
const { t } = useTranslation();
const {
control,
@@ -163,7 +166,7 @@ function OllamaDialog({
return (
<Dialog open={open} onClose={onClose} maxWidth="sm" fullWidth>
<DialogTitle>
{editMode ? `编辑 ${initialData?.llm_factory || LLM_FACTORY_LIST.Ollama}` : `配置 ${initialData?.llm_factory || LLM_FACTORY_LIST.Ollama}`}
{editMode ? t('setting.edit') : t('setting.configure')} {initialData?.llm_factory || LLM_FACTORY_LIST.Ollama}
</DialogTitle>
<DialogContent>
<Box component="form" sx={{ mt: 2 }}>
@@ -171,13 +174,13 @@ function OllamaDialog({
<Controller
name="model_type"
control={control}
rules={{ required: '模型类型是必填项' }}
rules={{ required: t('setting.modelTypeRequired') }}
render={({ field }) => (
<FormControl fullWidth margin="normal" error={!!errors.model_type}>
<InputLabel> *</InputLabel>
<InputLabel>{t('setting.modelType')} *</InputLabel>
<Select
{...field}
label="模型类型 *"
label={`${t('setting.modelType')} *`}
>
{modelTypeOptions.map((option) => (
<MenuItem key={option.value} value={option.value}>
@@ -196,17 +199,17 @@ function OllamaDialog({
<Controller
name="llm_name"
control={control}
rules={{ required: '模型名称是必填项' }}
rules={{ required: t('setting.modelNameRequired') }}
render={({ field }) => (
<TextField
{...field}
fullWidth
label="模型名称"
label={t('setting.modelName')}
margin="normal"
required
error={!!errors.llm_name}
helperText={errors.llm_name?.message || '请输入模型名称'}
placeholder="例如: llama2, mistral"
helperText={errors.llm_name?.message || t('setting.modelNamePlaceholder')}
placeholder={t('setting.modelNameExample')}
/>
)}
/>
@@ -216,21 +219,21 @@ function OllamaDialog({
name="api_base"
control={control}
rules={{
required: '基础 URL 是必填项',
required: t('setting.baseUrlRequired'),
pattern: {
value: /^https?:\/\/.+/,
message: '基础 URL 必须是有效的 URL'
message: t('setting.baseUrlInvalid')
}
}}
render={({ field }) => (
<TextField
{...field}
fullWidth
label="基础 URL"
label={t('setting.baseUrl')}
margin="normal"
required
error={!!errors.api_base}
helperText={errors.api_base?.message || '基础 URL'}
helperText={errors.api_base?.message || t('setting.baseUrl')}
placeholder="http://localhost:8888"
/>
)}
@@ -247,8 +250,8 @@ function OllamaDialog({
label="API Key"
margin="normal"
error={!!errors.api_key}
helperText={errors.api_key?.message || 'API Key (可选)'}
placeholder="如果需要认证,请输入 API Key"
helperText={errors.api_key?.message || t('setting.apiKeyTip')}
placeholder={t('setting.apiKeyPlaceholder')}
/>
)}
/>
@@ -258,26 +261,26 @@ function OllamaDialog({
name="max_tokens"
control={control}
rules={{
required: '最大 Token 数是必填项',
required: t('setting.maxTokensRequired'),
min: {
value: 1,
message: '最大 Token 数必须大于 0'
message: t('setting.maxTokensMin')
},
max: {
value: 100000,
message: '最大 Token 数不能超过 100000'
message: t('setting.maxTokensMax')
}
}}
render={({ field }) => (
<TextField
{...field}
fullWidth
label="最大 Token 数"
label={t('setting.maxTokens')}
margin="normal"
type="number"
required
error={!!errors.max_tokens}
helperText={errors.max_tokens?.message || '模型支持的最大 Token 数'}
helperText={errors.max_tokens?.message || t('setting.maxTokensValidation')}
placeholder="4096"
onChange={(e) => field.onChange(parseInt(e.target.value) || 0)}
/>
@@ -307,7 +310,7 @@ function OllamaDialog({
{/* 右侧按钮组 */}
<Box sx={{ display: 'flex', gap: 1 }}>
<Button onClick={onClose} disabled={loading}>
{t('setting.cancel')}
</Button>
<Button
onClick={handleSubmit(handleFormSubmit)}
@@ -315,7 +318,7 @@ function OllamaDialog({
disabled={loading}
startIcon={loading ? <CircularProgress size={20} /> : null}
>
{t('setting.confirm')}
</Button>
</Box>
</Box>