优化:AI翻译里面的翻译语言列表优先显示为当前用户的语言,其次系统的语言,默认英语兜底

This commit is contained in:
jingrow 2026-06-15 00:47:42 +08:00
parent 4f8aef6353
commit bc4821c0bf
3 changed files with 117 additions and 18 deletions

View File

@ -12,6 +12,8 @@
// ── 事件名(统一管理,避免散落各处)────────────────────────────────────────────
import { getCurrentLocale } from '@/shared/i18n'
/** Block Editor 推送上下文到 AI 侧栏 */
export const BE_AI_CONTEXT = 'blockeditor:ai-context'
/** Block Editor 请求 AI 执行动作 */
@ -69,23 +71,23 @@ export const TONE_OPTIONS: AIActionOption[] = [
/** 默认翻译语言列表 */
export const LANG_OPTIONS: AIActionOption[] = [
{ value: 'English', label: 'English' },
{ value: 'Simplified Chinese', label: '简体中文' },
{ value: 'Traditional Chinese', label: '繁體中文' },
{ value: 'Japanese', label: '日本語' },
{ value: 'Korean', label: '한국어' },
{ value: 'French', label: 'Français' },
{ value: 'German', label: 'Deutsch' },
{ value: 'Spanish', label: 'Español' },
{ value: 'Portuguese', label: 'Português' },
{ value: 'Italian', label: 'Italiano' },
{ value: 'Russian', label: 'Русский' },
{ value: 'Arabic', label: 'العربية' },
{ value: 'Hindi', label: 'हिन्दी' },
{ value: 'Dutch', label: 'Nederlands' },
{ value: 'Polish', label: 'Polski' },
{ value: 'Turkish', label: 'Türkçe' },
{ value: 'Vietnamese', label: 'Tiếng Việt' },
{ value: 'Thai', label: 'ภาษาไทย' },
{ value: 'Simplified Chinese', label: 'Simplified Chinese' },
{ value: 'Traditional Chinese', label: 'Traditional Chinese' },
{ value: 'Japanese', label: 'Japanese' },
{ value: 'Korean', label: 'Korean' },
{ value: 'French', label: 'French' },
{ value: 'German', label: 'German' },
{ value: 'Spanish', label: 'Spanish' },
{ value: 'Portuguese', label: 'Portuguese' },
{ value: 'Italian', label: 'Italian' },
{ value: 'Russian', label: 'Russian' },
{ value: 'Arabic', label: 'Arabic' },
{ value: 'Hindi', label: 'Hindi' },
{ value: 'Dutch', label: 'Dutch' },
{ value: 'Polish', label: 'Polish' },
{ value: 'Turkish', label: 'Turkish' },
{ value: 'Vietnamese', label: 'Vietnamese' },
{ value: 'Thai', label: 'Thai' },
]
/** 内置 AI 动作注册表 */
@ -144,6 +146,48 @@ export interface BEInsertDetail {
// ── 工具函数 ───────────────────────────────────────────────────────────────────
/**
*
* English
*/
export function getDefaultTargetLang(): string {
try {
const locale = getCurrentLocale()
if (!locale) return 'English'
const code = locale.toLowerCase()
// 直接匹配完整 locale
const direct = LANG_OPTIONS.find(o =>
o.value.toLowerCase().replace(/\s+/g, '') === code.replace(/[^a-z]/g, ''))
if (direct) return direct.value
// 匹配语言前缀
const langMap: Record<string, string> = {
zh: 'Simplified Chinese',
ja: 'Japanese',
ko: 'Korean',
fr: 'French',
de: 'German',
es: 'Spanish',
pt: 'Portuguese',
it: 'Italian',
ru: 'Russian',
ar: 'Arabic',
hi: 'Hindi',
nl: 'Dutch',
pl: 'Polish',
tr: 'Turkish',
vi: 'Vietnamese',
th: 'Thai',
}
const prefix = code.split(/[-_]/)[0]
// 特殊处理zh-TW → Traditional Chinese
if (prefix === 'zh' && (code.includes('tw') || code.includes('hk') || code.includes('mo'))) {
return 'Traditional Chinese'
}
if (langMap[prefix]) return langMap[prefix]
} catch { /* ignore */ }
return 'English'
}
/** 安全地 dispatch CustomEvent避免未捕获异常 */
export function dispatchAIEvent<T>(eventName: string, detail: T): void {
try {

View File

@ -11,6 +11,7 @@
import type { BlockModule } from '../../registry/types'
import { t } from '@/shared/i18n'
import { getDefaultTargetLang } from '../../ai/ai-protocol'
const AICommandBlock: BlockModule = {
name: 'aiCommand',
@ -127,7 +128,7 @@ const AICommandBlock: BlockModule = {
editor.chain().focus().deleteRange(range).run()
window.dispatchEvent(
new CustomEvent('blockeditor:ai-action', {
detail: { editor, action: 'translate', params: { target_lang: 'Simplified Chinese' }, selectedText },
detail: { editor, action: 'translate', params: { target_lang: getDefaultTargetLang() }, selectedText },
}),
)
},

View File

@ -22709,3 +22709,57 @@ msgstr "已全部拒绝"
msgid "Restore Review"
msgstr "恢复审阅"
msgid "English"
msgstr "英语"
msgid "Simplified Chinese"
msgstr "简体中文"
msgid "Traditional Chinese"
msgstr "繁体中文"
msgid "Japanese"
msgstr "日语"
msgid "Korean"
msgstr "韩语"
msgid "French"
msgstr "法语"
msgid "German"
msgstr "德语"
msgid "Spanish"
msgstr "西班牙语"
msgid "Portuguese"
msgstr "葡萄牙语"
msgid "Italian"
msgstr "意大利语"
msgid "Russian"
msgstr "俄语"
msgid "Arabic"
msgstr "阿拉伯语"
msgid "Hindi"
msgstr "印地语"
msgid "Dutch"
msgstr "荷兰语"
msgid "Polish"
msgstr "波兰语"
msgid "Turkish"
msgstr "土耳其语"
msgid "Vietnamese"
msgstr "越南语"
msgid "Thai"
msgstr "泰语"