-
Notifications
You must be signed in to change notification settings - Fork 52
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(web): react-typed-i18n页面国际化 #833
Conversation
🦋 Changeset detectedLatest commit: f4205e9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 18 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## master #833 +/- ##
==========================================
- Coverage 71.01% 70.58% -0.44%
==========================================
Files 129 130 +1
Lines 3664 3722 +58
Branches 481 497 +16
==========================================
+ Hits 2602 2627 +25
- Misses 970 1001 +31
- Partials 92 94 +2
☔ View full report in Codecov by Sentry. |
}; | ||
} | ||
|
||
export default useI18nTranslateToString; |
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.
目前JS生态的共识是尽量少用export default,直接export function useI18nTranslateToString()
就好
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.
这两个函数可以直接写在src/i18n/index.ts里
1移动翻译函数至i18n/index.ts中对之前的review意见做出了修改 2从Cookies或header中获取语言id 2自定义配置文件中的国际化文本示例
页面调用时,如果碰到原有字符串判定逻辑仍然使用publicConfig.PASSWORD_PATTERN_MESSAGE 以上的问题请确认 关于第三点,目前placeholder里面的显示只是最简单基础的一项,common.yaml文件中也要求必须配置 |
const cookies = parseCookies({ req }); | ||
|
||
if (cookies && cookies.language) { | ||
return cookies.language; |
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.
检查cookie中的language值是否合法
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.
整个流程是,先从cookie或者header中获取已经有的值,然后检查获取到的值是否合法
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.
把默认语言、可以接受的语言全部定义为常量(数组常量),这样以后修改起来方便一些
libs/config/src/common.ts
Outdated
errorMessage: Type.Union([ | ||
Type.String({ | ||
description: "如果密码不符合规则显示什么", | ||
default: "必须包含字母、数字和符号,长度大于等于8位", | ||
}), | ||
Type.Object({ i18n: I18nTypeSchema }, { description: "系统内展示文本是否采用国际化类型" }), | ||
]), |
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.
目前应该所有可以配置国际化的名字的配置项都是(string | { i18n: { cn: string ...} }}
这个类型,所以可以定义一处然后所有地方都用
libs/config/src/type.ts
Outdated
export type I18nType = { | ||
i18n: { | ||
default: string, | ||
zh_cn: string, | ||
en: string, | ||
} | ||
} |
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.
为何要单独定义?下面代码有问题吗?
const I18nStringSchema = Type.Union([
Type.String({ }),
Type.Object({ i18n: Type.Object({ default: Type.String(), ... } })
]);
@ddadaal (1)配置文本i18n逻辑重构
(2) 变更配置文本类型
(3)config.ts下的runtimeConfig或serverConfig中定义i18n文本类型
(4)config.ts下增加按语言ID获取对应字符串函数,这个地方如果逻辑没问题想确认一下不存在时的返回值
src/util/constants.ts
|
(4) 可以使用类型系统更精确地确定返回值: RUNTIME_I18N_CONFIG_TEXTS: {
passwordPatternMessage: I18nStringType | undefined,
test: I18nStringType;
}
export const getRuntimeI18nConfigText = <TKey extends RuntimeI18nConfigKeys>(
languageId: string, key: TKey,
): ((typeof publicConfig.RUNTIME_I18N_CONFIG_TEXTS)[TKey]) extends (I18nStringType)
? (string) : string | undefined => {
const i18nConfigText = publicConfig.RUNTIME_I18N_CONFIG_TEXTS[key];
if (!i18nConfigText) {
return undefined as any;
}
return getI18nConfigCurrentText(i18nConfigText, languageId);
};
const a = getRuntimeI18nConfigText("zh-CN", "passwordPatternMessage"); // string | undefined
const b = getRuntimeI18nConfigText("zh-CN", "test"); // string (5) 不需要单独定义常量,因为typescript会在写错的时候报错 |
|
@ddadaal |
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.
在apps/cli/assets/config下的配置文件示例中,在每个支持国际化文本的配置项上加注释“此文本支持国际化”,如下:
# 主页标题
homeTitle:
# 默认文本
# 此文本支持国际化
defaultText: "Super Computing on Web"
apps/auth/package.json
Outdated
@@ -43,7 +44,8 @@ | |||
"pino": "8.15.0", | |||
"nodemailer": "6.9.4", | |||
"qrcode": "1.5.3", | |||
"speakeasy": "2.0.0" | |||
"speakeasy": "2.0.0", | |||
"react-typed-i18n": "^2.3.0" |
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.
不要^,SCOW的package.json中的依赖版本都确定为具体的版本
@ddadaal 上述意见已修改,请确认~ |
实现方案:
使用react-typed-i18n实现国际化
https://github.com/ddadaal/react-typed-i18n
开发文档:
https://jgf29kqp7z.feishu.cn/wiki/PH98witE4iO2p0khJYPcpWxqnpb
利用Provider外包UI组件
_app.tsx
<AntdConfigProvider color={primaryColor} locale="zh_cn">
/component.ts
因为采用Provider方案,包括路由导航栏在内的所有UI国际化渲染均类似,实现简单
优点:
1.实现简单,结构清晰
2.翻译文本资源key的prefix单独定义,在同一个翻译函数下简单使用不同的prefix
3.Localized使页面组件不需要再次引入翻译函数就可以直接进行翻译
缺点:
一次引入所有翻译资源可能使加载速度变慢
但是目前简单测试一两个页面没有感觉到什么影响
甚至因为没有全局对象异步等待等问题语言切换时显示速度很快
翻译文本资源整理
配置文件参考下列表单(demo示例和登录页面配置)
https://jgf29kqp7z.feishu.cn/wiki/BFFUwHLgaiYxK5k9QGwci4X0nMe
其他页面展示文本参考下表各系统zh_cn.ts和en.ts工作表
https://jgf29kqp7z.feishu.cn/wiki/RFXDw06rCiodzMkbOYqcEAclnkg
配置文件中可国际化配置由原来的字符串类型变更为字符串或i18n对象
eg:
xxxx.config
# 允许兼容原来的字符串类型
# title: "开源算力中心门户和管理平台"
# 当想实现国际化功能时,不再配置原来的字符串类型,配置成下列展示的国际化类型
# 包含默认值(字符串类型),英文文本(字符串类型),中文文本(字符串类型)
title:
i18n:
default: "开源算力中心门户和管理平台"
en: "Open-source Compute Center Portal and Management Platform"
zh_cn: "开源算力中心门户和管理平台"
修改移动web后端中文文字到前端并实现国际化
主要包括以下错误信息的文字提示:
管理系统
1.修改作业时限时,时长错误信息 "设置作业时限需要大于该作业的运行时长。"
2.个人信息: 修改密码时错误信息: 来源于common配置文件中的配置的密码错误的错误信息
3.各创建租户和创建用户页面
用户Id错误信息: 来源于mis.yaml配置的创建用户id错误信息
密码错误信息: 来源于common配置文件中的配置的密码错误的错误信息
4.在账户中添加用户的原
”用户已经存在于此账户中!“
因为是grpc返回错误,直接使用后端错误信息元数据的英文展示,不做国际化门户系统
1.个人信息中修改密码的错误信息: 来源于common配置文件中的配置的密码错误的错误信息