Skip to content

feat(translate): brand new translate feature #8513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 106 commits into
base: main
Choose a base branch
from

Conversation

EurFelux
Copy link
Collaborator

@EurFelux EurFelux commented Jul 25, 2025

What this PR does

Before

目前整个翻译页面的内容全部塞在一个tsx里面,任何状态改变都会导致整个页面重渲染,非常非常重,计划重构一下。

另外发现翻译历史列表没有使用虚拟化技术,同时因为输入文本会频繁触发重渲染,导致翻译历史多起来时非常卡顿,这部分也计划优化一下。

After

  • 性能优化
    • 翻译历史使用条件渲染
    • 使用虚拟列表加载翻译历史
  • 新功能
    • 交换源语言与目标语言
    • 翻译设置页面
    • 自定义翻译语言
    • 估计翻译的token用量
  • 添加可复用组件
    • LanguageSelect 通用的翻译语言语言选择组件
  • 添加 Hook
    • useWindowSize 可获取实时主窗口尺寸用于复杂的样式控制
  • Bug修复
    • fetchTranslate无法捕获流式Error导致静默错误
    • 消息翻译失败时仍然持续显示转圈
  • 更好的Qwen MT支持
  • UI 调整
2025-08-04.00-10-25.mov
image image image image

Fixes #

Why we need it and why it was done in this way

The following tradeoffs were made:

The following alternatives were considered:

Links to places where the discussion took place:

Breaking changes

If this PR introduces breaking changes, please describe the changes and the impact on users.

Special notes for your reviewer

Checklist

This checklist is not enforcing, but it's a reminder of items that could be relevant to every PR.
Approvers are expected to review this list.

Release note


@EurFelux EurFelux linked an issue Jul 25, 2025 that may be closed by this pull request
4 tasks
EurFelux added 2 commits July 25, 2025 12:53
添加防抖函数来优化文本输入和键盘事件的处理,减少不必要的状态更新和翻译请求
@EurFelux EurFelux linked an issue Jul 29, 2025 that may be closed by this pull request
EurFelux added 6 commits July 29, 2025 17:33
重构翻译页面,将输入区域相关逻辑和UI抽离到单独的InputArea组件中
优化代码结构,提高可维护性

buggy: waiting for merge
将翻译设置功能提取为独立组件,减少主页面代码复杂度
EurFelux added 9 commits July 30, 2025 20:24
统一操作栏的padding样式,将输入和输出区域的容器样式分离以提高可维护性
添加源语言与目标语言交换功能按钮
为AWS Bedrock添加i18n
在翻译提示中添加说明,要求翻译时去除文本中的"[to be translated]"前缀
添加虚拟列表组件并应用于翻译历史页面,优化长列表渲染性能
将模型选择器从翻译设置移动到翻译页面主界面,优化模型选择流程
@EurFelux EurFelux changed the title refactor(translate): refactor translate page feat(translate): brand new translate page Jul 30, 2025
EurFelux added 7 commits July 31, 2025 01:49
- 新增 CustomTranslateLanguage 类型定义
- 在数据库中增加 translate_languages 表和相关 CRUD 操作
- 实现自定义语言的添加、删除、更新和查询功能
添加翻译设置页面,包含自定义语言表格、添加/编辑模态框、翻译模型选择和提示词配置
添加自定义语言表格组件及模态框,支持增删改查操作
修复数据库字段命名不一致问题,将langcode改为langCode
新增内置语言代码列表用于校验
添加多语言支持及错误提示
新增getTranslateOptions函数,用于合并内置翻译语言和自定义语言选项。当获取自定义语言失败时,自动回退到只返回内置语言选项。
- 将翻译语言选项管理集中到useTranslate钩子中
- 修改LanguageCode类型为string以支持自定义语言
- 废弃旧的getLanguageByLangcode方法,提供新的实现
- 统一各组件对翻译语言选项的获取方式
- 优化类型定义,移除冗余类型TranslateLanguageVarious
@EurFelux EurFelux requested review from DeJeune and alephpiece August 3, 2025 13:15
@alephpiece
Copy link
Collaborator

build check 没过

Comment on lines 24 to 29
<Space.Compact direction="horizontal" block>
<span role="img" aria-label={lang.emoji} style={{ marginRight: 8 }}>
{lang.emoji}
</span>
<Space.Compact block>{lang.label()}</Space.Compact>
</Space.Compact>
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里是不是只要最外面的 Space.Compact 就行了?

自定义语言对不齐可能是在我这 emoji 宽度和 flag 不一样

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

检查一下,看有没有必要给emoji容器锁定一个长宽?

@alephpiece
Copy link
Collaborator

这中间一条能去掉吗?

image

@alephpiece
Copy link
Collaborator

这个工具栏看还能不能怎么调一下?感觉很拥挤,尤其是语言名称比较长、模型选择框里面的文字也比较长的时候

image

还有就是修改已有语言,好像不改语言代码没法直接保存了

image

@EurFelux
Copy link
Collaborator Author

EurFelux commented Aug 3, 2025

这中间一条能去掉吗?

image

纯css方案应该做不到,我看看用js控制吧

@alephpiece
Copy link
Collaborator

这中间一条能去掉吗?
image

纯css方案应该做不到,我看看用js控制吧

其实我觉得目前这个交换按钮既然本来也不是放在两个语言 select 中间的,不如就和右边的语言 select 放一起得了,也没什么区别

@alephpiece
Copy link
Collaborator

alephpiece commented Aug 3, 2025

这个工具栏看还能不能怎么调一下?感觉很拥挤,尤其是语言名称比较长、模型选择框里面的文字也比较长的时候

image 还有就是修改已有语言,好像不改语言代码没法直接保存了 image

可能至少要限制一下宽度,现在语言名字太长会撑开

image

将输入输出区域整合为统一容器,调整操作栏宽度和间距
移动设置和历史按钮到输出操作栏,简化布局结构
将硬编码的窗口最小宽度和高度值替换为从共享配置导入的常量,提高代码可维护性
将操作栏从三个独立部分改为网格布局,使用InnerOperationBar组件统一样式
移除冗余的operationBarWidth变量,简化样式代码
移除自定义的CopyButton组件,直接使用Ant Design的原生Button组件来实现复制功能,保持UI一致性并减少冗余代码
将操作按钮从左侧移动到右侧,并移除不必要的HStack组件
调整翻译按钮位置并保留其工具提示功能
@EurFelux
Copy link
Collaborator Author

EurFelux commented Aug 3, 2025

QQ_1754231149758

这样怎么样

为源语言和目标语言选择器添加统一的宽度样式,保持UI一致性
添加 Windows_GetSize IPC 通道用于获取窗口当前尺寸
添加 Windows_Resize IPC 通道用于监听窗口大小变化
新增 useWindowSize hook 方便在渲染进程中使用窗口尺寸
使用debounce优化窗口大小变化的处理,避免频繁触发更新
为翻译页面添加响应式布局,根据窗口宽度和导航栏位置动态调整模型选择器宽度
在窗口最大化或还原时发送尺寸变化事件,以便界面可以响应这些状态变化
为翻译配置添加对'zh-yue'语言代码的处理,返回对应的'Cantonese'值
当编辑已存在的自定义语言时,如果输入的语言代码已存在且与原代码不同,则抛出错误提示
@EurFelux
Copy link
Collaborator Author

EurFelux commented Aug 3, 2025

all done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-review the feature/bug/pr needs a product/development reviewing
Projects
None yet
5 participants