5 ways of displaying i18n translations in Vue
Anzelika
Posted on July 14, 2020
When your Vue app needs internalization, you'll probably come across i18n. It took me a while to get a proper grasp on how to display messages in different scenarios (especially #5), so I'm laying out a quick easy-digest summary here.
1. Basic interpolation
With the interpolation curly brackets you can render any content that can be placed directly in your template HTML.
<p>{{$t('introText')}}</p>
2. Binding the value
Useful for input placeholders or labels.
<v-text-field :label="$t('form.firstName')"></v-text-field>
3. Within a function
Note that within the Vue instance it would be necessary to use this
keyword
btnLabel(){
return this.$t('buttons.save')
}
4. Using v-t
directive
With v-t
directive you may specify the path of the translation string in the data object and then easily render it in the template.
data: () => ({
path: "buttons.add"
}),
<v-btn v-t="path"></v-btn>
NB: This directive is not reactive, therefore the content needs to be manually reloaded when the locale changes.
5. Using v-text
directive
To solve the reactivity problem, you may use v-text
directive instead.
data: () => ({
path: "buttons.add"
}),
<v-btn v-text="$t(path)"></v-btn>
Hope this comes in handy for our translation wizards! Give me a shout if there's a technique I missed 😜
Posted on July 14, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.