Skip to content

feat(i18n): Unify i18n loading — declare translations in stack config, auto-register in createKernel#1107

Merged
hotlong merged 3 commits intomainfrom
copilot/unify-i18n-plugin-loading
Mar 21, 2026
Merged

feat(i18n): Unify i18n loading — declare translations in stack config, auto-register in createKernel#1107
hotlong merged 3 commits intomainfrom
copilot/unify-i18n-plugin-loading

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 21, 2026

Server and MSW/mock environments maintained separate i18n pipelines: duplicate loadAppLocale functions in browser.ts/server.ts, manual MSW custom handlers, and CRM translations (crmLocales) never wired into the stack config. This caused format inconsistencies and made the i18n route fragile across environments.

Changes

  • examples/crm/objectstack.config.ts — Declare i18n: { namespace: 'crm', defaultLocale, supportedLocales, translations: crmLocales } in the stack definition
  • apps/console/objectstack.shared.ts — Aggregate i18n bundles from all composed stacks into i18n.bundles
  • apps/console/src/mocks/createKernel.ts — Read appConfig.i18n.bundles, register a kernel i18n service, and auto-inject the MSW handler for GET /i18n/translations/:lang returning the spec envelope format. Add i18n.getTranslations to broker shim.
  • apps/console/src/mocks/browser.ts & server.ts — Remove loadAppLocale, remove custom i18n handler, remove unused http/HttpResponse imports

Flow

defineStack({ i18n: { namespace, translations } })
  → sharedConfig merges i18n.bundles from all stacks
    → createKernel registers i18n service + auto-generates MSW handler
      → GET /api/v1/i18n/translations/:lang → { data: { locale, translations } }

Callers no longer construct i18n MSW routes — createKernel derives them from config. Response uses the spec envelope so loadLanguage.ts unwraps identically for mock and production.

Original prompt

This section details on the original issue you should resolve

<issue_title>[i18n] 统一 i18n 插件加载与翻译注入机制,支持 server & MSW/mock 双模式</issue_title>
<issue_description>## 背景
目前 ObjectUI 在 i18n 加载链路上 server(Hono/production)和 MSW/mock(前端本地开发、Storybook 测试等)两种运行模式存在分裂:

  • 服务端(server.ts)默认不会自动注册 @objectstack/service-i18n 插件,且 appConfig/manifest 通常缺失 translations 字段,导致 i18n REST API 不可用。
  • 前端 mock 环境(apps/console/src/mocks/browser.ts, server.ts)通过 MSW customHandler 手动模拟 /api/v1/i18n/translations/:lang 路由,未用 spec 标准插件链路,返回格式也不完全一致。
  • examples/crm 的 CRM 业务翻译包仅以 crmLocales 导出,并未集成到任何 stack config 的 translations 字段里。

这会引发:

  • 业务对象/字段翻译容易失效,尤其是生产 server 模式下
  • 维护两套 mock 和真实服务代码,容易产生格式和体验不一致问题

期望目标

  • 支持 unified i18n 加载机制,server & mock/MSW 环境下均采用规范的 @objectstack/service-i18n 插件动态注册/翻译注入
  • translations 在 objectstack.config.ts、sharedConfig、AppPlugin 中完整传递,并写入 kernel 的 i18n service,由 dispatcher 自动暴露 REST API 路由
  • 移除所有自定义 i18n MSW 路由 handler,全部用 dispatcher 内置机制
  • mock/browser/server/runtime 不再需要手动 import crmLocales 或自管 label

改动

  1. examples/crm
    • 在 objectstack.config.ts 添加 i18ntranslations: [crmLocales] 字段
  2. apps/console/objectstack.shared.ts
    • 合并出来的 sharedConfig 要传递 translations 字段,不要丢失
  3. apps/console/src/mocks/createKernel.ts
    • kernel 初始化时始终注册 service-i18n plugin(可区分 dev/prod 用 memory adapter 或 disk adapter)
    • AppPlugin 正常触发 loadTranslations
    • 保证无论 server 还是 browser/msw,都走同一 i18n 插件链路
  4. apps/console/src/mocks/browser.ts / server.ts
    • 移除 MSW customHandler 的 i18n 路由
    • 只用 MSWPlugin 暴露的 dispatcher handler
  5. README & 相关文档
    • 说明双环境链路、无需手动维护模拟 i18n 响应
    • 推荐参考 examples/msw-todo 的最佳实践

兼容性 & 工程要求

  • 保证 mock、dev、storybook、server/production 环境下所有对象和字段国际化 label 均能正常 fallback/切换
  • 测试覆盖 examples/crm、apps/console 各物理环境
  • 变更后做一次相关代码和文档梳理,明确 i18n 传递和注册链路

如需详细方案/代码协助,请联系 Core Team。
</issue_description>

Comments on the Issue (you are @copilot in this section)


📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 21, 2026

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

Project Deployment Actions Updated (UTC)
objectui-demo Ready Ready Preview, Comment Mar 21, 2026 11:37am
2 Skipped Deployments
Project Deployment Actions Updated (UTC)
objectui Ignored Ignored Mar 21, 2026 11:37am
objectui-storybook Ignored Ignored Mar 21, 2026 11:37am

Request Review

…g, auto-register in createKernel, remove manual MSW handlers

- CRM config: add i18n field with namespace, translations, defaultLocale, supportedLocales
- sharedConfig: merge i18n bundles from all composed stacks
- createKernel: register i18n kernel service, auto-generate MSW handler from config bundles
- browser.ts/server.ts: remove loadAppLocale and custom i18n MSW handlers
- Add i18n.getTranslations support to broker shim
- Update CHANGELOG.md

Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
Agent-Logs-Url: https://github.com/objectstack-ai/objectui/sessions/00eab53d-cefa-4f13-9413-eed4af8e9854
Copilot AI changed the title [WIP] Update i18n plugin loading and translation injection mechanism feat(i18n): Unify i18n loading — declare translations in stack config, auto-register in createKernel Mar 21, 2026
Copilot AI requested a review from hotlong March 21, 2026 11:42
@hotlong hotlong marked this pull request as ready for review March 21, 2026 11:44
Copilot AI review requested due to automatic review settings March 21, 2026 11:44
@hotlong hotlong merged commit f0e1181 into main Mar 21, 2026
6 checks passed
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR unifies the console’s mock i18n loading pipeline by declaring translations in each stack config, aggregating those bundles into the composed app config, and having the MSW kernel factory auto-register an i18n service + MSW translation endpoint in the spec envelope format.

Changes:

  • Declare CRM translations in examples/crm stack config via an i18n section (namespace, locales, translations).
  • Merge all stacks’ i18n declarations into sharedConfig.i18n.bundles for the console app.
  • In the MSW kernel factory, register an i18n service and auto-inject an MSW handler for GET /api/v1/i18n/translations/:lang returning { data: { locale, translations } }; remove per-environment loadAppLocale and manual MSW i18n handlers.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
examples/crm/objectstack.config.ts Adds stack-level i18n declaration (namespace + translation map) so translations are discoverable via config.
apps/console/objectstack.shared.ts Aggregates stack i18n configs into sharedConfig.i18n.bundles for downstream consumers.
apps/console/src/mocks/createKernel.ts Registers an i18n kernel service from config bundles and auto-injects an MSW i18n translations handler returning the spec envelope.
apps/console/src/mocks/browser.ts Removes duplicate locale loader + manual i18n MSW handler; relies on createKernel injection.
apps/console/src/mocks/server.ts Removes duplicate locale loader + manual i18n MSW handler; relies on createKernel injection.
CHANGELOG.md Documents the unified i18n pipeline changes and removal of custom handlers.

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.

[i18n] 统一 i18n 插件加载与翻译注入机制,支持 server & MSW/mock 双模式

3 participants