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(guide/i18n): calculate when inserting value #178

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions zh-CN/guide/i18n/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ ctx.i18n.define('en-US', { hello: 'Hello, {author.name}!' })

上述三段代码的实际效果完全相同,可以根据自己的需要进行选择。

如果你希望对属性值进行某些计算,传入 Javascript 表达式也是可以的:

```ts
ctx.i18n.define('zh-CN', { list: '列表:{arr.join(",")}' })
ctx.i18n.define('en-US', { list: 'List: {arr.join(",")}' })
```

shigma marked this conversation as resolved.
Show resolved Hide resolved
::: tip
目前支持的表达能力会受到解析器和特殊语法的限制。请避免在表达式中引入 `}`,以免造成歧义。

此外,如果向 `session.text()` 传入的二参数是一个数组,那么直接写 `{0}` 将表示数组中的第一个元素 (而非作为 JavaScript 表达式的 `0`)。
:::
### 使用消息元素

你也可以在模板中使用 [消息元素](../basic/element.md) 语法。消息元素的属性同样使用 `{}` 进行插值:
Expand Down
Loading