|
| 1 | +# Component Interpolation |
| 2 | + |
| 3 | +## Basic Usage |
| 4 | + |
| 5 | +Sometimes, we need to localize with a locale message that was included in a HTML tag or component. For example: |
| 6 | + |
| 7 | +```html |
| 8 | +<p>I accept xxx <a href="/term">Terms of Service Agreement</a></p> |
| 9 | +``` |
| 10 | + |
| 11 | +In the above message, if you use `$t`, you will probably try to compose the following locale messages: |
| 12 | + |
| 13 | +```js |
| 14 | +const messages = { |
| 15 | + en: { |
| 16 | + term1: 'I Accept xxx\'s', |
| 17 | + term2: 'Terms of Service Agreement' |
| 18 | + } |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +And your localized template may look like this: |
| 23 | + |
| 24 | +```html |
| 25 | +<p>{{ $t('term1') }}<a href="/term">{{ $t('term2') }}</a></p> |
| 26 | +``` |
| 27 | + |
| 28 | +Output: |
| 29 | + |
| 30 | +```html |
| 31 | +<p>I accept xxx <a href="/term">Terms of Service Agreement</a></p> |
| 32 | +``` |
| 33 | + |
| 34 | +This is very cumbersome, and if you configure the `<a>` tag in a locale message, there is a possibility of XSS vulnerabilities due to localizing with `v-html="$t('term')"`. |
| 35 | + |
| 36 | +You can avoid it using the Translation component (`i18n-t`). For example the below. |
| 37 | + |
| 38 | +Template: |
| 39 | + |
| 40 | +```html |
| 41 | +<div id="app"> |
| 42 | + <!-- ... --> |
| 43 | + <i18n-t keypath="term" tag="label" for="tos"> |
| 44 | + <a :href="url" target="_blank">{{ $t('tos') }}</a> |
| 45 | + </i18n-t> |
| 46 | + <!-- ... --> |
| 47 | +</div> |
| 48 | +``` |
| 49 | + |
| 50 | +JavaScript: |
| 51 | + |
| 52 | +```js |
| 53 | +import { createApp } from 'vue' |
| 54 | +import { createI18n } from 'vue-i18n' |
| 55 | + |
| 56 | +const i18n = createI18n({ |
| 57 | + locale: 'en', |
| 58 | + messages: { |
| 59 | + en: { |
| 60 | + tos: 'Term of Service', |
| 61 | + term: 'I accept xxx {0}.' |
| 62 | + }, |
| 63 | + ja: { |
| 64 | + tos: '利用規約', |
| 65 | + term: '私は xxx の{0}に同意します。' |
| 66 | + } |
| 67 | + } |
| 68 | +}) |
| 69 | + |
| 70 | +const app = createApp({ |
| 71 | + data: () => ({ url: '/term' }) |
| 72 | +}) |
| 73 | + |
| 74 | +app.use(i18n) |
| 75 | +app.mount('#app') |
| 76 | +``` |
| 77 | + |
| 78 | +The following output: |
| 79 | + |
| 80 | +```html |
| 81 | +<div id="app"> |
| 82 | + <!-- ... --> |
| 83 | + <label for="tos"> |
| 84 | + I accept xxx <a href="/term" target="_blank">Term of Service</a>. |
| 85 | + </label> |
| 86 | + <!-- ... --> |
| 87 | +</div> |
| 88 | +``` |
| 89 | + |
| 90 | +About the above example, see the [example](https://github.com/intlify/vue-i18n-next/blob/master/examples/legacy/components/translation.html) |
| 91 | + |
| 92 | +The children of translation component are interpolated with locale message of `keypath` prop. In the above example, |
| 93 | + |
| 94 | +:::v-pre |
| 95 | +`<a :href="url" target="_blank">{{ $t('tos') }}</a>` |
| 96 | +::: |
| 97 | + |
| 98 | +Is interpolated with `term` locale message. |
| 99 | + |
| 100 | +In the above example, the component interpolation follows the **list interpolation**. The children of translation component are interpolated by their order of appearance. |
| 101 | + |
| 102 | +<!-- textlint-disable --> |
| 103 | +You can choose the root node element type by specifying a `tag` prop. If omitted, it defaults to [Fragments](https://v3.vuejs.org/guide/migration/fragments.html#overview). |
| 104 | +<!-- textlint-enable --> |
| 105 | + |
| 106 | +## Slots syntax usage |
| 107 | + |
| 108 | +It’s more convenient to use the named slots syntax. For example the below: |
| 109 | + |
| 110 | +Template: |
| 111 | + |
| 112 | +```html |
| 113 | +<div id="app"> |
| 114 | + <!-- ... --> |
| 115 | + <i18n-t keypath="info" tag="p"> |
| 116 | + <template v-slot:limit> |
| 117 | + <span>{{ changeLimit }}</span> |
| 118 | + </template> |
| 119 | + <template v-slot:action> |
| 120 | + <a :href="changeUrl">{{ $t('change') }}</a> |
| 121 | + </template> |
| 122 | + </i18n-t> |
| 123 | + <!-- ... --> |
| 124 | +</div> |
| 125 | +``` |
| 126 | + |
| 127 | +JavaScript: |
| 128 | + |
| 129 | +```js |
| 130 | +import { createApp } from 'vue' |
| 131 | +import { createI18n } from 'vue-i18n' |
| 132 | + |
| 133 | +const i18n = createI18n({ |
| 134 | + locale: 'en', |
| 135 | + messages: { |
| 136 | + en: { |
| 137 | + info: 'You can {action} until {limit} minutes from departure.', |
| 138 | + change: 'change your flight', |
| 139 | + refund: 'refund the ticket' |
| 140 | + } |
| 141 | + } |
| 142 | +}) |
| 143 | + |
| 144 | +const app = createApp({ |
| 145 | + data: () => ({ |
| 146 | + changeUrl: '/change', |
| 147 | + refundUrl: '/refund', |
| 148 | + changeLimit: 15, |
| 149 | + refundLimit: 30 |
| 150 | + }) |
| 151 | +}) |
| 152 | + |
| 153 | +app.use(i18) |
| 154 | +app.mount('#app') |
| 155 | +``` |
| 156 | + |
| 157 | +Outputs: |
| 158 | + |
| 159 | +```html |
| 160 | +<div id="app"> |
| 161 | + <!-- ... --> |
| 162 | + <p> |
| 163 | + You can <a href="/change">change your flight</a> until <span>15</span> minutes from departure. |
| 164 | + </p> |
| 165 | + <!-- ... --> |
| 166 | +</div> |
| 167 | +``` |
| 168 | + |
| 169 | +You can also use the following slots shorthand in templates: |
| 170 | + |
| 171 | +```html |
| 172 | +<div id="app"> |
| 173 | + <!-- ... --> |
| 174 | + <i18n-t keypath="info" tag="p"> |
| 175 | + <template #limit> |
| 176 | + <span>{{ changeLimit }}</span> |
| 177 | + </template> |
| 178 | + <template #action> |
| 179 | + <a :href="changeUrl">{{ $t('change') }}</a> |
| 180 | + </template> |
| 181 | + </i18n-t> |
| 182 | + <!-- ... --> |
| 183 | +</div> |
| 184 | +``` |
| 185 | + |
| 186 | +:::warning LIMITATION |
| 187 | +:warning: In translation component, slots props are not supported. |
| 188 | +::: |
| 189 | + |
| 190 | +### Pluralization Usage |
| 191 | + |
| 192 | +You can use pluralization in Component interpolation by use `plural` prop. For example the below. |
| 193 | + |
| 194 | +Template: |
| 195 | + |
| 196 | +```html |
| 197 | +<div id="app"> |
| 198 | + <!-- ... --> |
| 199 | + <i18n-t keypath="message.plural" locale="en" :plural="count"> |
| 200 | + <template #n> |
| 201 | + <b>{{ count }}</b> |
| 202 | + </template> |
| 203 | + </i18n-t> |
| 204 | + <!-- ... --> |
| 205 | +</div> |
| 206 | +``` |
| 207 | + |
| 208 | +JavaScript: |
| 209 | + |
| 210 | +```js |
| 211 | +const { createApp, ref } = Vue |
| 212 | +const { createI18n } = VueI18n |
| 213 | + |
| 214 | +const i18n = createI18n({ |
| 215 | + legacy: false, |
| 216 | + locale: 'en', |
| 217 | + messages: { |
| 218 | + en: { |
| 219 | + message: { |
| 220 | + plural: 'no bananas | {n} banana | {n} bananas' |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | +}) |
| 225 | + |
| 226 | +const app = createApp({ |
| 227 | + setup() { |
| 228 | + const count = ref(2) |
| 229 | + return { count } |
| 230 | + } |
| 231 | +}) |
| 232 | +app.use(i18n) |
| 233 | +app.mount('#app') |
| 234 | +``` |
| 235 | + |
| 236 | +The following output: |
| 237 | +```html |
| 238 | +<div id="app" data-v-app=""> |
| 239 | + <!-- ... --> |
| 240 | + <b>2</b> bananas |
| 241 | + <!-- ... --> |
| 242 | +</div> |
| 243 | +``` |
0 commit comments