|
| 1 | +--- |
| 2 | +description: i18n component allows localizing text that contains HTML elements or Vue.js components | fluent-vue - Vue.js internationalization plugin |
| 3 | +--- |
| 4 | + |
1 | 5 | # `i18n` component
|
2 | 6 |
|
3 |
| -Allows to use Vue components and html elements inside translation messages. |
| 7 | +Very common problem of localizing web apps is localizing text that needs to include HTML elements or components. Consider the example: |
| 8 | + |
| 9 | +```html |
| 10 | +<p> |
| 11 | + <router-link :to="{ name: 'login' }">Sign in</router-link> |
| 12 | + or |
| 13 | + <router-link :to="{ name: 'register' }">sign up</router-link> |
| 14 | + to add comments on this article. |
| 15 | +</p> |
| 16 | +``` |
| 17 | + |
| 18 | +One solution for this could be to translate each part of the message separatelly like this: |
| 19 | + |
| 20 | +```html |
| 21 | +<p> |
| 22 | + <router-link :to="{ name: 'login' }">{{ $t('sign-in') }}</router-link> |
| 23 | + {{ $t('or') }} |
| 24 | + <router-link :to="{ name: 'register' }">{{ $t('sign-up') }}</router-link> |
| 25 | + {{ $t('to-comment') }} |
| 26 | +</p> |
| 27 | +``` |
| 28 | + |
| 29 | +But this is cumbersome and could lead to confusion and errors, as it is hard to see that these messages are actually just one sentence. |
| 30 | + |
| 31 | +Another solution is using v-html directive. But that only works for simpler static HTML. And it could lead to accidentaly breaking a page if the message is not properly formatted. |
| 32 | + |
| 33 | +`i18n` component allows to use Vue components and HTML elements inside translation messages. |
| 34 | + |
| 35 | +Previous example would look like this when using `i18n` component: |
| 36 | + |
| 37 | +```html |
| 38 | +<i18n path="sign-in-up-to-add-comments" tag="p"> |
| 39 | + <template #signInLink="{ signInLabel }"> |
| 40 | + <router-link :to="{ name: 'login' }">{{ signInLabel }}</router-link> |
| 41 | + </template> |
| 42 | + <template #signUpLink="{ signUpLabel }"> |
| 43 | + <router-link :to="{ name: 'register' }">{{ signUpLabel }}</router-link> |
| 44 | + </template> |
| 45 | +</i18n> |
| 46 | +``` |
| 47 | + |
| 48 | +Localization message: |
| 49 | +```ftl |
| 50 | +# $signInLink - will be replaced with link to login page |
| 51 | +# .sign-in-label - text for login link |
| 52 | +sign-in-up-to-add-comments = |
| 53 | + {$signInLink} or {$signUpLink} to add comments on this article. |
| 54 | + .sign-in-label = Sign in |
| 55 | + .sign-up-label = sign up |
| 56 | +``` |
| 57 | + |
| 58 | +As you can see entire sentence uses just one translation key. It does not use v-html directive. And we can even add comments, so translators know what they are dealing with. |
| 59 | + |
| 60 | +### API |
4 | 61 |
|
5 | 62 | * Props:
|
6 | 63 | * `path {string}` localization message key
|
|
0 commit comments