Skip to content

Commit 9fe0815

Browse files
committed
docs: add message functions section
1 parent 1c8043b commit 9fe0815

File tree

3 files changed

+210
-7
lines changed

3 files changed

+210
-7
lines changed

docs/advanced/component.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ The following output:
8989

9090
About the above example, see the [example](https://github.com/intlify/vue-i18n-next/blob/master/examples/legacy/components/translation.html)
9191

92-
The children of `i18n-t` component are interpolated with locale message of `path` prop. In the above example,
92+
The children of `i18n-t` component are interpolated with locale message of `keypath` prop. In the above example,
9393

9494
:::v-pre
9595
`<a :href="url" target="_blank">{{ $t('tos') }}</a>`

docs/advanced/function.md

Lines changed: 209 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,210 @@
1-
# Messages Function
1+
# Messages Functions
22

3-
TODO:
3+
Vue I18n recommends using the string base on list, named, and literal format as locale messages when translating messages.
4+
5+
However, sometimes string-based message format syntax is difficult to resolve.
6+
7+
For example, suppose you want to handle the following message in French:
8+
9+
- Manger de la soupe
10+
- Manger une pomme
11+
- Manger du pain
12+
- Manger de l'orge
13+
14+
As you can see, the article preceding the noun will vary based on gender and phonetics.
15+
16+
The features supported by Vue I18n Message Format Syntax may not be able to support these language-specific use cases.
17+
18+
There are situations however, where you really need the full programmatic power of JavaScript, due to the complex language syntax.
19+
So instead of string-based messages, you can use the **Message functions**.
20+
21+
:::tip NOTE
22+
The messages written in message format syntax are compiled into message functions with Vue I18n message compiler. Message functions and caching mechanism to maximize performance gains.
23+
:::
24+
25+
## Basic usage
26+
27+
The following is a message function that returns a simple greeting:
28+
29+
```js
30+
const messages = {
31+
en: {
32+
greeting: (ctx) => 'hello!'
33+
}
34+
}
35+
```
36+
37+
The message function accept the **Message context** as the first argument, which has several props and functions. We'll explain how to use it in the following sections, so let's go on.
38+
39+
The use of the message function is very easy! You just specify the key of the message function with `$t` or `t`:
40+
41+
```html
42+
<p>{{ $t('greeting') }}</p>
43+
```
44+
45+
Output is the below:
46+
47+
```html
48+
<p>hello!</p>
49+
```
50+
51+
The message function outputs the message of the return **string** value of the message function.
52+
53+
:::tip NOTE
54+
If you need to use the `<i18n-t>` component, you need to support returning not only the string value, but also the **VNode** value.
55+
56+
To support for `<i18n-t>` component usage, the `type` prop of MessageContext is used as shown in the following code example:
57+
58+
```js
59+
import { createVNode, Text } from 'vue'
60+
61+
const messages = {
62+
en: {
63+
greeting: ({ type }) => type === 'vnode'
64+
? createVNode(Text, null, 'hello', 0)
65+
: 'hello'
66+
}
67+
}
68+
```
69+
70+
If you haven't already, it's recommended to read through the [Vue render function](https://v3.vuejs.org/guide/render-function.html#the-dom-tree) before diving into message functions.
71+
:::
72+
73+
## Named interpolation
74+
75+
Vue I18n supports [named interpolation](../essentials/syntax#named-interpolation) as a string-based message format. Vue I18n interpolate the parameter values with `$t` or `t`, and it can be output it.
76+
77+
You can use the `named` function of the message context to do the same thing.
78+
79+
Here is the example of greeting:
80+
81+
```js
82+
const messages = {
83+
en: {
84+
greeting: ({ named }) => `hello, ${named('name')}!`
85+
}
86+
}
87+
```
88+
89+
Template:
90+
91+
```html
92+
<p>{{ $t('greeting', { name: 'DIO' }) }}</p>
93+
```
94+
95+
Output is the below:
96+
97+
```html
98+
<p>hello, DIO!</p>
99+
```
100+
101+
You need to specify the key that resolves the value specified with the named of `$t` or `t`.
102+
103+
## List interplation
104+
105+
Vue I18n supports [list interpolation](../essentials/syntax#list-interpolation) as a string-based message format. Vue I18n interpolate the parameter values with `$t` or `t`, and it can be output it.
106+
107+
You can use the `list` function of the message context to do the same thing.
108+
109+
Here is the example of greeting:
110+
111+
```js
112+
const messages = {
113+
en: {
114+
greeting: ({ list }) => `hello, ${list(0)}!`
115+
}
116+
}
117+
```
118+
119+
Template:
120+
121+
```html
122+
<p>{{ $t('greeting', ['DIO']) }}</p>
123+
```
124+
125+
Output is the below:
126+
127+
```html
128+
<p>hello, DIO!</p>
129+
```
130+
131+
You need to specify the index that resolves the value specified with the list of `$t` or `t`.
132+
133+
134+
## Linked messages
135+
136+
Vue I18n supports [linked messages](../essentials/syntax#linked-messages) as a string-based message format. Vue I18n interpolate the parameter values with `$t` or `t`, and it can be output it.
137+
138+
You can use the `linked` function of the message context to do the same thing.
139+
140+
Here is the example of message function:
141+
142+
```js
143+
const messages = {
144+
en: {
145+
the_world: 'the world',
146+
dio: 'DIO:',
147+
linked: ({ linked }) => `${linked('message.dio')} ${linked('message.the_world')} !!!!`
148+
}
149+
}
150+
```
151+
152+
Template:
153+
154+
```html
155+
<p>{{ $t('linked') }}</p>
156+
```
157+
158+
Output is the below:
159+
160+
```html
161+
<p>DIO: the world !!!!</p>
162+
```
163+
164+
You need to specify the key that resolves the value specified with `$t` or `t`.
165+
166+
## Pluralization
167+
168+
Vue I18n supports [pluralization](../essentials/pluralization) as a string-based message format. Vue I18n interpolate the parameter values with `$tc` or `tc`, and it can be output it.
169+
170+
You can use the `plural` function of the message context to do the same thing.
171+
172+
Here is the example of message function:
173+
174+
```js
175+
const messages = {
176+
en: {
177+
car: ({ plural }) => plural(['car', 'cars']),
178+
apple: ({ plural, named }) =>
179+
plural([
180+
'no apples',
181+
'one apple',
182+
`${named('count')} apples`
183+
])
184+
}
185+
}
186+
```
187+
188+
Template:
189+
190+
```html
191+
<p>{{ $tc('car', 1) }}</p>
192+
<p>{{ $tc('car', 2) }}</p>
193+
194+
<p>{{ $tc('apple', 0) }}</p>
195+
<p>{{ $tc('apple', 1) }}</p>
196+
<p>{{ $tc('apple', 10, { count: 10 }) }}</p>
197+
```
198+
199+
Output is the below:
200+
201+
```html
202+
<p>car</p>
203+
<p>cars</p>
204+
205+
<p>no apples</p>
206+
<p>one apple</p>
207+
<p>10 apples</p>
208+
```
209+
210+
You need to specify the key that resolves the value specified with `$tc` or `tc`.

docs/essentials/syntax.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
Vue I18n can use message format syntax to localize the messages to be displayed in the UI. Vue I18n messages are interpolations and messages with various feature syntax.
44

5-
Under the hood, The messages written in message format syntax are compiled into message functions with Vue I18n message compiler. Message functions and caching mechanism to maximize performance gains.
6-
7-
If you prefer the raw power of JavaScript, you can also [directly write message functions](../advanced/function) instead of messages.
8-
95
## Interpolations
106

117
Vue I18n supports interpolation using placeholders `{}` like "Mustache".

0 commit comments

Comments
 (0)