multiline text using vuejs i18n
I'm using i18n single file component to have translation support on my application. To do so, I'm using the tag as following
<i18n>
{
"fr": {
"text": "blabla in french
blabla
bla"
},
"en": {
"text": "blabla in english
bla"
}
}
</i18n>
But I have multiple lines text with html formating, how can I use language handling for long html text ?
Found a pretty cool solution here.
It is possible to achieve this with Interpolation. In this example, the {0}
placeholder will be replaced with what you put into the <i18n>
tag.
en.json
{
"footer": "Built with Vue and Vue I18n{0}Powered by an excessive amount of coffee"
}
Footer.vue
<template>
<i18n path="footer" tag="p" class="footer">
<br />
</i18n>
</template>
You could always use backticks:
<i18n>
{
"fr": {
"text": `blabla in french
blabla
bla`
},
"en": {
"text": `blabla in english
bla`
}
}
</i18n>
You will get some (harmless) warning about something concerning POJO strings though.
#1. You can use backticks:
i18n file:
{
text: `Content
With some
Break lines`
}
#2. You can use combination of js and css
{
text: 'Content \n With some \n Break lines'
}
css:
.class {
white-space: pre-line
}
#3. You can use HTML and v-html (but becareful because without sanitizing your HTML you lead to XSS attacks!)
{
text: 'Content <br /> With some <br /> Break lines'
}
template:
<div v-html="yourI18nRule" />
Learn more about sanitizing HTML here:
https://www.npmjs.com/package/sanitize-html