语言包
使用语言包可以将 DSL 中以 ::开头 或 $L(Word) 标记的字符串,翻译为用户设定的语言。
约定
- 示例中约定应用根目录为
/data/app
, 实际编写时需替换为应用根目录。 - 使用
<>
标识自行替换的内容。 例如:icon-<图标名称>
, 实际编写时应替换为:icon-foo
,icon-bar
...
结构
语言包由一组与 DSL 同名 yaml
文件构成, 语言包存放在应用 langs
目录。
<ISO 639-1 语言编码(全小写)>
/global.yml
全局语言包文件
<ISO 639-1 语言编码(全小写)>
/<Widget 目录>
/<Widget 文件名>.yaml
DSL 文件语言包文件 优先匹配
ISO 639-1 standard language codes
简体中文: zh-cn
语言包文件 | 说明 |
---|---|
/data/app/langs/zh-cn/global.yml | 全局语言包文件 |
/data/app/langs/zh-cn/models/pet.mod.yml | Model pet 语言包 |
/data/app/langs/zh-cn/models/user/pet.mod.yml | Modeluser.pet 语言包 |
/data/app/langs/zh-cn/tables/pet.mod.yml | Table pet 语言包 |
繁体中文: zh-hk
语言包文件 | 说明 |
---|---|
/data/app/langs/zh-hk/global.yml | 全局语言包文件 |
/data/app/langs/zh-hk/models/pet.mod.yml | Model pet 语言包 |
/data/app/langs/zh-hk/models/user/pet.mod.yml | Modeluser.pet 语言包 |
/data/app/langs/zh-hk/tables/pet.mod.yml | Table pet 语言包 |
在 DSL 中使用
使用 ::
前缀, 或 $L(<字符串>)
声明的字符串,DSL 解析时, 将被替换为语言包中的字符串。
在 DSL 中, 带 :: 前缀的字符不希望被替换, 可以使用\转义,写为 ::
语言包文件:
/data/app/langs/zh-cn/global.yml
yaml
Pet: 宠物
/data/app/langs/zh-cn/models/pet.mod.yml
yaml
Pet Name: 宠物名称
/data/app/langs/zh-hk/global.yml
yaml
Pet: 寵物
/data/app/langs/zh-hk/models/pet.mod.yml
yaml
Pet Name: 寵物名稱
DSL 文件:
/data/app/models/pet.mod.json
json
{
"name": "::Pet",
"table": { "name": "pet", "comment": "$L(Pet) Table" },
"columns": [
{ "name": "id", "comment": "ID", "type": "ID" },
{
"name": "name",
"comment": "::Pet Name",
"type": "string",
"length": 80,
"index": true,
"nullable": true
}
]
}
解析结果
当语言环境变量 YAO_LANG=zh-cn
时, DSL 将解析为:
json
{
"name": "宠物",
"table": { "name": "pet", "comment": "宠物 Table" },
"columns": [
{ "name": "id", "comment": "ID", "type": "ID" },
{
"name": "name",
"comment": "宠物名称",
"type": "string",
"length": 80,
"index": true,
"nullable": true
}
]
}
当语言环境变量 YAO_LANG=zh-hk
时, DSL 将解析为:
json
{
"name": "寵物",
"table": { "name": "pet", "comment": "寵物 Table" },
"columns": [
{ "name": "id", "comment": "ID", "type": "ID" },
{
"name": "name",
"comment": "寵物名稱",
"type": "string",
"length": 80,
"index": true,
"nullable": true
}
]
}
未找到可匹配的语言包时,DSL 将解析为:
json
{
"name": "Pet",
"table": { "name": "pet", "comment": "Pet Table" },
"columns": [
{ "name": "id", "comment": "ID", "type": "ID" },
{
"name": "name",
"comment": "Pet Name",
"type": "string",
"length": 80,
"index": true,
"nullable": true
}
]
}
在 Script 中使用
使用 $L()
函数, 进行多语言替换
/data/app/scripts/test.js
javascript
function Bar() {
return $L('Pet');
}
当语言环境变量 YAO_LANG=zh-cn
时:
bash
yao run scripts.test.Bar
# 宠物
当语言环境变量 YAO_LANG=zh-hk
时:
bash
yao run scripts.test.Bar
# 寵物
未找到可匹配的语言包时:
bash
yao run scripts.test.Bar
# Pet