Skip to content

fix(i18n): localize article editor page - #388

Merged
longsizhuo merged 2 commits into
InvolutionHell:mainfrom
richardkkk:fix/editor-i18n
Jul 26, 2026
Merged

fix(i18n): localize article editor page#388
longsizhuo merged 2 commits into
InvolutionHell:mainfrom
richardkkk:fix/editor-i18n

Conversation

@richardkkk

Copy link
Copy Markdown
Contributor

Summary

  • localize all user-facing text on the article editor page
  • localize the Markdown starter content
  • update the starter content when changing locale only if the draft has not been edited

Validation

  • pnpm typecheck
  • pnpm lint (0 errors; existing warnings only)
  • pnpm test (67 passed)
  • manually verified EN → ZH starter-content switching locally

@vercel

vercel Bot commented Jul 24, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
website-preview Ready Ready Preview, Comment Jul 25, 2026 7:07am

@vercel

vercel Bot commented Jul 24, 2026

Copy link
Copy Markdown

@richardkkk is attempting to deploy a commit to the longsizhuo's projects Team on Vercel.

A member of the Team first needs to authorize it.

@richardkkk

richardkkk commented Jul 24, 2026

Copy link
Copy Markdown
Contributor Author

问题说明

在英文模式访问 /en/editor 时,编辑页仍有部分用户可见文案使用中文,包括页面标题、操作按钮、发布提示和 Markdown 默认示例内容。

另外,在初步改完UI问题后,测试发现,默认示例内容首次初始化后会同步到全局草稿状态;因此从英文切换到中文时,页面其他文案已切换,但编辑器仍保留英文默认内容。
sc_20260725030812

修复内容

  • 将编辑页的用户可见文案统一接入 editor i18n 命名空间。
  • 将 Markdown 默认示例内容按当前语言由页面传入,避免通用编辑器组件绑定具体语言。
  • 记录上一次自动注入的默认内容:
    • 未编辑默认内容时,切换语言会更新示例内容;
    • 用户已编辑草稿时,保留现有内容,不覆盖用户输入
sc_20260725031022

@longsizhuo

longsizhuo commented Jul 25, 2026

Copy link
Copy Markdown
Member

Review — fix(i18n): localize article editor page

感谢 @richardkkk!本地化这部分做得干净、正确——编辑器页所有硬编码中文都挪进了 messages/*.json,ICU 插值用得也对({filename}{status}{characters}{images}{message}),MarkdownEditor 不再写死起始文案。只有一个调用方,所以把 defaultMarkdown 设为必填 prop 是安全的。客户端组件,不影响 SSG。👍

一个建议合并前修掉的行为问题,外加几个小点。

🐛 "切换语言时(仅未编辑)替换起始内容"的逻辑,在真实的语言切换器下不会生效

app/components/LocaleToggle.tsx 用 next-intl 的软导航切换语言:

router.replace(pathname, { locale: next });

这会改变 [locale] 路由段,从而重挂载 EditorPageClientMarkdownEditor。重挂载时,组件内的 lastDefaultMarkdownRef(一个 useRef)复位为 null。而 useEditorStore 是一个无持久化的 zustand 模块单例,软导航中会存活下来——并且编辑器自身的 2 秒 setInterval(updateMarkdown, 2000) 到这时已经把当前显示的(旧语言)默认文案回写进了 store。

于是在切换后的这次挂载里:

const shouldUseDefault =
  !markdownRef.current ||                               // false:store 里是旧语言默认文案
  markdownRef.current === lastDefaultMarkdownRef.current; // false:(旧默认 === null)
// → shouldUseDefault = false → 保留旧语言起始内容

这个 guard 恰好在它需要跨越的那次重挂载上被复位,所以 === lastDefaultMarkdownRef.current 在每次挂载的第一次(也是唯一一次)运行时都是在跟 null 比较——永远无法识别"未编辑的上一版默认文案"。它只有在同一个实例不重挂载、却收到变化的 defaultMarkdown prop 时才可能起作用,而 [locale] 路由永远不会产生这种情况。

复现

  1. 打开 /zh/editor,什么都不输,等 ~2s(同步会把 zh 起始文案写进 store)。
  2. 切换语言 → /en/editor
  3. 期望:英文起始内容。实际:仍是中文起始内容。

(如果你测试时是在头 ~2s 内切换、或走整页刷新,它会看起来是好的——那是竞态窗口。)

修法 — 把"上次注入的默认值"(或一个 hasEdited 脏标记)放进持久化的 store,而不是组件 ref,让它能在软导航的重挂载中存活:

// editor-store 里:新增 lastDefault + setLastDefault
const shouldUseDefault = !markdown || markdown === lastDefault;

或者,如果这个替换功能不值得这份复杂度,就干脆删掉 lastDefaultMarkdownRef 这套机制,只在 store 为空时注入 defaultMarkdown(这其实就是它现在的实际行为)——让代码对自己做的事诚实。

小点

  • 孤儿 key: editor.heading("编辑器" / "Editor")在 editor 命名空间里没有消费者(EditorMetadataForm 用的是另一个 editorMetadata 命名空间)。历史遗留,非本 PR 引入,但你正好在改这块,顺手删掉即可。
  • 双语一致性: EN 的 errors.imageType 丢了 zh 版保留的"(浏览器未给出 MIME)"提示。无伤大雅,只是两个语言携带的信息略有不对称。
  • 冗余: editor.titleRequired("请填写标题",底部提示)与 editor.errors.titleRequired("请输入文章标题",alert)是两个近重复的串。看起来是有意的(不同 UI 场景)——仅提示一下。

结论

i18n 这部分本身扎实、可以合。我唯一想请你处理的是:要么修好、要么删掉那段切语言替换起始内容的逻辑,别让 PR 带上一个在真实 LocaleToggle 下并不成立的行为。这里没有任何数据丢失或安全相关的问题。

@richardkkk

richardkkk commented Jul 25, 2026

Copy link
Copy Markdown
Contributor Author

感谢 review!
editor.heading 孤儿 key 和 errors.imageType 的双语信息不一致已在最新提交中处理。
关于默认示例随语言切换的问题,按提供方案尝试复现问题
但在第2步 投稿编辑器页面中 无中英切换按钮 于是在第2步我尝试:

点击“返回首页”
在首页点击真实的 ZH / EN 切换按钮
再进入投稿页

未复现问题

我也尝试了直接修改地址栏中的 locale,也未复现。且这应该不是 router.replace 的等价路径。

是否有一个我遗漏的、可以在编辑页存活 store 的同时触发 locale 软切换的具体复现路径?

@longsizhuo

Copy link
Copy Markdown
Member

复现路径我之前写错了,编辑页没有 Header 所以没有语言切换按钮。这个行为问题影响很小

@longsizhuo
longsizhuo merged commit fb52bbf into InvolutionHell:main Jul 26, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants