Skip to content

Named, list, modifier examples #2186

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
69 changes: 68 additions & 1 deletion docs/guide/essentials/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,77 @@ It’s `en` locale that has hierarchical structure in the object.
The `message.snake` has `snake case`. The `message.custom_modifier` has `custom modifiers example: @.snakeCase:{'message.snake'}`, and it’s linked to the locale messages key, which is interpolated with literal.

:::tip NOTE
You can use the interpolations (Named, List, and Literal) for the key of Linked messages.
You can use the interpolations (Named, List, and Literal) for the key of Linked messages shown below.
:::


This example shows the use of modifiers (`@.lower`, `@.upper`, `@.capitalize`) combined with named, list, and literal interpolations.


```js
const messages = {
en: {
message: {
greeting: "Hello, @.lower:{'message.name'}! You have {count} new messages.",
name:"{name}"
},

welcome: "Welcome, @.upper:{'name'}! Today is @.capitalize:{'day'}.",
name: '{0}',
day: '{1}',

literalMessage: "This is an email: foo{'@'}@.lower:domain",
domain: 'SHOUTING'
}
}
```
### Named interpolation with modifier

In `message.greeting`, we use a named interpolation for `{count}` and link to `message.name`, applying the .lower modifier.

The key `message.name` contains `{name}`, which will be interpolated with the passed `name` param.

The `message.greeting` is linked to the locale message key `message.name`.

```html
<p>{{ $t('message.greeting', { name: 'Alice', count: 5 }) }}</p>
```
As result, the below

```html
<p>Hello, alice! You have 5 new messages.</p>
```

### List interpolation with modifier

In this case, the values for `{0}` and `{1}` are passed as an array. The keys `name` and `day` are resolved using list interpolation and transformed with modifiers.

```html
<p>{{ $t('welcome', ['bob', 'MONDAY']) }}</p>
```

As result, the below

```html
<p>Welcome, BOB! Today is Monday.</p>
```

### Literal interpolation with modifier

In this example, we use a literal string inside the message and apply the `.lower` modifier.

```html
<p>{{ $t('literalMessage') }}</p>
```

Here, the modifier is applied to the content inside `domain`, and the `@` is preserved as literal output.

As result, the below

```html
<p>This is an email: foo@shouting</p>
```

## Special Characters

The following characters used in the message format syntax are processed by the compiler as special characters:
Expand Down
30 changes: 30 additions & 0 deletions packages/vue-i18n-core/test/composer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,36 @@ describe('modifiers', () => {
expect(t('hi')).toEqual('hi hello-world')
})

test('Modifiers with Named, List and Literal Interpolation', () => {
const { t } = createComposer({
locale: 'en',
messages: {
en: {
message: {
greeting:
"Hello, @.lower:{'message.name'}! You have {count} new messages.",
name: '{name}'
},

welcome: "Welcome, @.upper:{'name'}! Today is @.capitalize:{'day'}.",
name: '{0}',
day: '{1}',

literalMessage: "This is an email: foo{'@'}@.lower:domain",
domain: 'SHOUTING'
}
}
})

expect(t('message.greeting', { name: 'Alice', count: 5 })).toEqual(
'Hello, alice! You have 5 new messages.'
)
expect(t('welcome', ['bob', 'monday'])).toEqual(
'Welcome, BOB! Today is Monday.'
)
expect(t('literalMessage')).toEqual('This is an email: foo@shouting')
})

test('pascal case', () => {
const _modifiers = {
snakeCase: (str: VueMessageType) =>
Expand Down