-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
feat: i18n plugin #6149
Conversation
packages/plugin-i18n/src/index.ts
Outdated
const packageJSON = _require('../package.json'); | ||
const { name: packageName } = packageJSON; | ||
|
||
consola.withTag(packageName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个要么 后面可以考虑在 ice 内部统一增加
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用了 createLogger 代替了
packages/plugin-i18n/src/index.ts
Outdated
name: packageName, | ||
setup: ({ addRoutesDefinition, generator }) => { | ||
checkPluginOptions(i18nConfig); | ||
i18nConfig = mergeDefaultConfig(i18nConfig); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
覆盖入参不是推荐的方式
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
去掉了
packages/plugin-i18n/src/index.ts
Outdated
} | ||
} | ||
|
||
function mergeDefaultConfig(i18nConfig: I18nConfig) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
默认值走解构就好了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
去掉了
children, | ||
locales, | ||
defaultLocale, | ||
disabledCookie, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
disableCookie
/ cookieDisabled
?
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这样的话 replace 最后也是走 originHistory.push 了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
} | ||
|
||
if (autoRedirect) { | ||
addResponseHandler((req) => { |
There was a problem hiding this comment.
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 中处理
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
类似 <Link to="/home">
在开启多语言后的使用变化是什么样的
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在开启多语言后:
- 用户视角:无需关心/指定语言的值,由插件内部处理掉
- 插件:自动根据当前页面的语言,拼接上语言值到路由地址上
packages/plugin-i18n/src/index.ts
Outdated
}); | ||
|
||
generator.modifyRenderData((renderData) => { | ||
renderData.customRuntimeOptions ||= ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
customRuntimeOptions 这个还是处理成对象吧,API 层面建议简单点,由内置模版处理成字符
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
<% if (runtimeOptions.exports) { -%> | ||
<%- runtimeOptions.exports %> | ||
<% } -%> | ||
<% if (locals.customRuntimeOptions) { -%> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
locals 字段始终会存在吗?好像没看到传入的地方
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这段主要是做什么的?有没有可能前移到 renderToHTML 或 renderToResponse 里?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
插件可以注册 response,比如需要在 render 前做 302 重定向
国际化的背景和方案设计:#6050
使用文档参考:https://github.com/alibaba/ice/blob/feat/i18n/website/docs/guide/advanced/i18n.md
Also close: #5965