Skip to content
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

feat: i18n plugin #6149

Merged
merged 49 commits into from
Apr 24, 2023
Merged

feat: i18n plugin #6149

merged 49 commits into from
Apr 24, 2023

Conversation

luhc228
Copy link
Collaborator

@luhc228 luhc228 commented Apr 11, 2023

国际化的背景和方案设计:#6050

使用文档参考:https://github.com/alibaba/ice/blob/feat/i18n/website/docs/guide/advanced/i18n.md

Also close: #5965

@luhc228 luhc228 requested a review from ClarkXia April 17, 2023 03:05
const packageJSON = _require('../package.json');
const { name: packageName } = packageJSON;

consola.withTag(packageName);
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个要么 后面可以考虑在 ice 内部统一增加

Copy link
Collaborator Author

@luhc228 luhc228 Apr 18, 2023

Choose a reason for hiding this comment

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

使用了 createLogger 代替了

name: packageName,
setup: ({ addRoutesDefinition, generator }) => {
checkPluginOptions(i18nConfig);
i18nConfig = mergeDefaultConfig(i18nConfig);
Copy link
Collaborator

Choose a reason for hiding this comment

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

覆盖入参不是推荐的方式

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

去掉了

}
}

function mergeDefaultConfig(i18nConfig: I18nConfig) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

默认值走解构就好了

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

去掉了

children,
locales,
defaultLocale,
disabledCookie,
Copy link
Collaborator

Choose a reason for hiding this comment

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

disableCookie / cookieDisabled ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

change to disableCookie


history.push = navigate;

history.replace = navigate;
Copy link
Collaborator

Choose a reason for hiding this comment

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

这样的话 replace 最后也是走 originHistory.push 了

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

packages/plugin-i18n/src/runtime/index.tsx Outdated Show resolved Hide resolved
}

if (autoRedirect) {
addResponseHandler((req) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

server 相关的 runtime 应该是一个独立的导出,不然很容易意外地引入 server 依赖,可以增加类似 staticRuntime 的,评估下目前有没有新增额外的依赖,如果没有的话可以在后续的 PR 中处理

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

目前看引入的依赖: universal-cookie, accept-language-parser, url-join 没有server 端的代码引入

getAllLocales().map((locale: string) => {
return (
<li key={locale}>
<Link
Copy link
Collaborator

Choose a reason for hiding this comment

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

类似 <Link to="/home"> 在开启多语言后的使用变化是什么样的

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

在开启多语言后:

  • 用户视角:无需关心/指定语言的值,由插件内部处理掉
  • 插件:自动根据当前页面的语言,拼接上语言值到路由地址上

@luhc228 luhc228 requested a review from ClarkXia April 18, 2023 04:01
});

generator.modifyRenderData((renderData) => {
renderData.customRuntimeOptions ||= '';
Copy link
Collaborator

Choose a reason for hiding this comment

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

customRuntimeOptions 这个还是处理成对象吧,API 层面建议简单点,由内置模版处理成字符

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@luhc228 luhc228 requested a review from ClarkXia April 20, 2023 06:59
ClarkXia
ClarkXia previously approved these changes Apr 20, 2023
<% if (runtimeOptions.exports) { -%>
<%- runtimeOptions.exports %>
<% } -%>
<% if (locals.customRuntimeOptions) { -%>
Copy link
Collaborator

Choose a reason for hiding this comment

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

locals 字段始终会存在吗?好像没看到传入的地方

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

这个是 ejs 默认的对象,这个对象包含你传入的所有的 options

ejs.render(`xxxx`, {  a: 1 })

// locals: { a: 1 }

@@ -267,6 +281,16 @@ async function doRender(serverContext: ServerContext, renderOptions: RenderOptio
await Promise.all(runtimeModules.commons.map(m => runtime.loadModule(m)).filter(Boolean));
}

const responseHandlers = runtime.getResponseHandlers();
Copy link
Collaborator

Choose a reason for hiding this comment

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

这段主要是做什么的?有没有可能前移到 renderToHTML 或 renderToResponse 里?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

插件可以注册 response,比如需要在 render 前做 302 重定向

@luhc228 luhc228 merged commit 1c3d3fe into release/next Apr 24, 2023
@luhc228 luhc228 deleted the feat/i18n branch April 24, 2023 02:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need review Need Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants