Skip to content

Commit 8b2eb0f

Browse files
authored
Merge pull request #5 from ZhangYingchun-OPPO/main
i18n for cards in quickapp project
2 parents 1d4a705 + 7b68d92 commit 8b2eb0f

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
- 提案时间: 2023-10-20
2+
- 影响版本: 1.3.0.0
3+
- 相关 Issues: 无
4+
5+
## 【必填】概述
6+
7+
快应用卡片无法使用 $t 和 $tc 语法实现多语言覆盖,期望快应用卡片与快应用能力保持一致,也支持 $t 和 $tc。
8+
参考文档:https://doc.quickapp.cn/framework/i18n.html
9+
10+
## 【必填】动机
11+
12+
方便快应用混合工程(工程内同时包含快应用和卡片)卡片适配多语言。
13+
14+
## 【必填】API规格和使用案例
15+
16+
#### 资源文件
17+
在卡片路由路径下定义i18n文件夹,内部放置每个语言地区下的资源定义文件即可;其中文件名定义如:`zh-CN.json`。每个 JSON 文件的内容格式如下:
18+
```json
19+
{
20+
"message": {
21+
"card": {
22+
"text": "pure-text-content",
23+
"format": {
24+
"object": "type-{name}",
25+
"array": "type-{0}"
26+
},
27+
"array": [
28+
"item-type-string",
29+
{
30+
"key": "item-type-object-attribute"
31+
},
32+
["item-type-array"]
33+
],
34+
"plurals": {
35+
"double": "car | cars",
36+
"three": "no apples | one apple | {count} apples",
37+
"format": {
38+
"object": "type-{name}",
39+
"array": "type-{0}"
40+
}
41+
}
42+
}
43+
}
44+
}
45+
```
46+
47+
#### 接口定义
48+
在卡片页面ux文件的`<template``<script>`中可以使用`$t``$tc`获取多语言文本。
49+
50+
##### 代码示例
51+
```html
52+
<template>
53+
<div>
54+
<text>{{ $t('message.card.text') }}</text>
55+
<text>{{ $t('message.card.format.object', { name: 'arg-object' }) }}</text>
56+
<text>{{ $t('message.card.format.array', ['arg-array']) }}</text>
57+
</div>
58+
</template>
59+
60+
<script>
61+
export default {
62+
onInit () {
63+
// 简单格式化:
64+
this.$t('message.card.text')
65+
this.$t('message.card.format.object', { name: 'arg-object' })
66+
this.$t('message.card.format.array', ['arg-array'])
67+
68+
// 单复数格式化:
69+
// 示例:message的值为两个选项时,传递数值不为单数
70+
// 输出:"cars"
71+
this.$tc('message.card.plurals.double', 0)
72+
// 示例:message的值为两个选项时,传递数值为单数
73+
// 输出:"car"
74+
this.$tc('message.card.plurals.double', 1)
75+
// 示例:message的值为两个选项时,传递数值不为单数
76+
// 输出:"cars"
77+
this.$tc('message.card.plurals.double', 2)
78+
}
79+
}
80+
</script>
81+
```
82+
83+
##### 简单格式化方法
84+
85+
| 属性 | 类型 | 参数 | 描述 |
86+
| --- | --- | --- | --- |
87+
| $t | Function | path: String 资源路径; arg0: object | array 格式化参数,非必要参数,根据系统语言完成简单的替换:`this.$t('message.pageA.text')` |
88+
89+
##### 单复数格式化方法
90+
| 属性 | 类型 | 参数 | 描述 |
91+
| --- | --- | --- | --- |
92+
| $tc | Function | path: String 资源路径; count: number要表达的值 | 根据系统语言完成单复数替换:`this.$tc('message.plurals.double')`,注意:定义资源的内容通过 | 分隔为多个选项 |
93+
94+
95+
## 【必填】提案人员是否愿意自行实现该功能
96+
97+
98+
99+
## 详细设计
100+
101+
卡片内多语言覆盖逻辑与快应用保持一致:
102+
1. 开发者在卡片路由路径下新增 `i18n文件夹`,用于存放多语言资源;
103+
2. hap-toolkit打包工具需要在编译过程中将卡片的 `i18n` 资源拷贝到rpk文件对应的路径;
104+
3. 快应用引擎在卡片加载时,根据当前系统的语言地区返回对应的语言资源。
105+
106+
## 缺陷
107+
108+
我们是不是可以不做这个功能,请考虑:
109+
110+
- 实现这个功能的投入:包括代码的复杂度、代码体积的增加、实现功能投入的人力
111+
- 这个功能是不是不需要 hapjs提供,使用 hapjs的开发者也可以在应用层实现,甚至实现得更好
112+
- 对 hapjs既有惯用开发习惯的影响
113+
- 对已发布版本和现有功能的影响,以及用户进行迁移的成本
114+
- 对其它未有代码实现的 RFC 提案的影响
115+
116+
## 替代选择
117+
118+
还有其他的方案也可以实现这个功能吗?
119+
120+
## 适配策略
121+
122+
如果我们实现了这个提案,有没有什么办法可以帮助开发者更好地适应这个改动?

0 commit comments

Comments
 (0)