Skip to content

Commit 56a45f5

Browse files
authored
docs: clarify v-t limitations and reactivity behavior (#2118)
- Improved explanation of `v-t`'s limitations regarding inline HTML and dynamic template expressions. - Clarified that `v-t` supports reactivity but is less flexible compared to `$t`. - Enhanced wording for better readability and accuracy. This improves the documentation to better reflect the behavior of `v-t` and avoid potential misunderstandings.
1 parent f81b46d commit 56a45f5

File tree

1 file changed

+41
-43
lines changed

1 file changed

+41
-43
lines changed

docs/guide/advanced/directive.md

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# Custom Directive
22

33
:::danger NOTE
4-
`v-t` will be deprecated at v11, and will be dropped at v12. This section is for those who are still using v10.
4+
The `v-t` directive will be deprecated in version 11 and removed in version 12. This section is intended for users still working with version 10.
55
:::
66

7-
You can translate not only with `$t`, but also with the `v-t` custom directive.
7+
In addition to using `$t`, you can also use the `v-t` custom directive for translations.
88

9-
## String syntax
9+
## String Syntax
1010

11-
You can pass the keypath of locale messages with string syntax.
11+
You can pass the key path of locale messages using string syntax.
1212

13-
Javascript:
13+
### JavaScript:
1414

1515
```js
1616
import { createApp } from 'vue'
@@ -31,18 +31,18 @@ app.use(i18n)
3131
app.mount('#app')
3232
```
3333

34-
Templates:
34+
### Template:
3535

3636
```html
3737
<div id="string-syntax">
38-
<!-- string literal -->
38+
<!-- Using a string literal -->
3939
<p v-t="'hello'"></p>
40-
<!-- keypath binding via data -->
40+
<!-- Binding a key path via data -->
4141
<p v-t="path"></p>
4242
</div>
4343
```
4444

45-
Outputs:
45+
### Output:
4646

4747
```html
4848
<div id="string-syntax">
@@ -51,11 +51,11 @@ Outputs:
5151
</div>
5252
```
5353

54-
## Object syntax
54+
## Object Syntax
5555

56-
You can use object syntax.
56+
Alternatively, you can use object syntax.
5757

58-
Javascript:
58+
### JavaScript:
5959

6060
```js
6161
import { createApp } from 'vue'
@@ -66,14 +66,14 @@ const i18n = createI18n({
6666
messages: {
6767
en: {
6868
message: {
69-
hi: 'Hi, {name}!',
70-
bye: 'good bye!',
69+
hi: 'Hi, {name}!'
70+
bye: 'Goodbye!',
7171
apple: 'no apples | one apple | {count} apples'
7272
}
7373
},
7474
ja: {
7575
message: {
76-
hi: 'こんにちは、 {name}!',
76+
hi: 'こんにちは、{name}!',
7777
bye: 'さようなら!',
7878
apple: 'リンゴはありません | 一つのりんご | {count} りんご'
7979
}
@@ -93,63 +93,61 @@ app.use(i18n)
9393
app.mount('#object-syntax')
9494
```
9595

96-
Templates:
96+
### Template:
9797

9898
```html
9999
<div id="object-syntax">
100-
<!-- literal -->
100+
<!-- Using an object with arguments -->
101101
<p v-t="{ path: 'message.hi', args: { name: 'kazupon' } }"></p>
102-
<!-- data binding via data -->
102+
<!-- Binding a key path via data -->
103103
<p v-t="{ path: byePath, locale: 'en' }"></p>
104-
<!-- pluralization -->
104+
<!-- Pluralization -->
105105
<p v-t="{ path: 'message.apple', plural: appleCount }"></p>
106106
</div>
107107
```
108108

109-
Outputs:
109+
### Output:
110110

111111
```html
112112
<div id="object-syntax">
113-
<p>こんにちは、 kazupon!</p>
114-
<p>good bye!</p>
113+
<p>こんにちは、kazupon!</p>
114+
<p>Goodbye!</p>
115115
<p>7 りんご</p>
116116
</div>
117117
```
118118

119-
## scoping
119+
## Scope
120120

121-
As mentioned in [the scope section](../essentials/scope.md), vue-i18n has a global scope and a local scope.
121+
As explained in [the scope section](../essentials/scope.md), `vue-i18n` supports both global and local scopes.
122122

123-
The scope under which `v-t` is also affected by scope when it works.
124-
125-
- local scope: using the i18n option in Legacy API style or using `useScope: ‘local'` in `useI18n`.
126-
- global scope: all cases other than the above.
123+
The behavior of `v-t` depends on the scope in which it is used:
127124

125+
- **Local scope**: Applied when using the i18n option in Legacy API style or setting `useScope: 'local'` in `useI18n`.
126+
- **Global scope**: Used in all other cases.
128127

129128
## `$t` vs `v-t`
130129

131130
### `$t`
132131

133-
`$t` is function of VueI18n instance. It has the following pros and cons:
134-
135-
#### Pros
136-
137-
You can **flexibly** use mustache syntax `{}` in templates and also computed props and methods in Vue component instance.
132+
`$t` is a function of the `VueI18n` instance with the following advantages and disadvantages:
138133

139-
#### Cons
134+
#### Pros:
135+
- Allows for **flexible usage** within templates, including mustache syntax `{}`.
136+
- Supports computed properties and methods within Vue components.
140137

141-
`$t` is executed **every time** when re-render occurs, so it does have translation costs.
138+
#### Cons:
139+
- `$t` is executed **on every re-render**, which can add translation overhead.
142140

143141
### `v-t`
144142

145-
`v-t` is a custom directive. It has the following pros and cons:
146-
147-
#### Pros
148-
149-
`v-t` has **better performance** than the `$t` function due to its pre-translation is possible with the Vue compiler module which was provided by [vue-i18n-extensions](https://github.com/intlify/vue-i18n-extensions).
143+
`v-t` is a custom directive with its own set of pros and cons:
150144

151-
Therefore it’s possible to make **more performance optimizations**.
145+
#### Pros:
146+
- Offers **better performance** than `$t`, as translations can be preprocessed by the Vue compiler module provided by [vue-i18n-extensions](https://github.com/intlify/vue-i18n-extensions).
147+
- Enables **performance optimizations** by reducing runtime translation overhead.
152148

153-
#### Cons
149+
#### Cons:
150+
- Less flexible than `$t`; it’s **more complex** to use.
151+
- Inserts translated content directly into the element’s `textContent`, which means it cannot be used inside inline HTML structures or combined with other dynamic template expressions.
152+
- When using server-side rendering (SSR), you must configure a [custom transform](https://github.com/intlify/vue-i18n-extensions#server-side-rendering-for-v-t-custom-directive) by setting the `directiveTransforms` option in the `compile` function of `@vue/compiler-ssr`.
154153

155-
`v-t` cannot be flexibly used like `$t`, it’s rather **complex**. The translated content with `v-t` is inserted into the `textContent` of the element. Also, when you use server-side rendering, you need to set the [custom transform](https://github.com/intlify/vue-i18n-extensions#server-side-rendering-for-v-t-custom-directive) to `directiveTransforms` option of the `compile` function of `@vue/compiler-ssr`.

0 commit comments

Comments
 (0)