Internationalising Vue Components
By Adrian Sutton
As Vue gradually spreads through our JavaScript, it’s reached the point where we need to support internationalisation. We already have a system for localising numbers and dates that we can reuse easily enough with Vue filters. We also have an existing system for translations, but it’s tied into the mustache templates we’ve used to date so we’ll need to adapt or change it somehow to work with Vue.
In mustache we simply wrap any strings to be translated with {{#i18n}}…{{/i18n}} markers and a webpack loader takes care of providing a translation function as the i18n property of the state for the compiled template.
There are a number of vue translations plugins around all of which tend to take one of two approaches:
- a directive that automatically pulls out the element text (e.g. vue-translate)
- a method you call (e.g. vue-i18n-mixin)
<h1 v-text=”t(‘header.title’, ‘fr’)”>
{{ t(‘header.title’) }}
Using a directive is nice and simple but doesn’t work if the text is dynamic (any {{data}} values are already resolved in the element text). Explicitly calling a function to perform the translation leads to pretty unwieldy templates.
What we’ve settled on is a pretty direct translation of our mustache system to Vue. We use
<p><i18n>Hello world!</i18n></p>
To make it actually translate, we use a HTML pre-loader for the webpack vue-loader that translates that into:
<p>{{translate("Hello world!")}}</p>
Finally, on the client side, a global Vue mixin provides the implementation of translate that looks up the translated version of that string and replace it.
When the string includes parameters we write them in the same way as a normal Vue template, for example:
<p><i18n>Hello {{name}}!</i18n></p>
And our pre-loader translates it in exactly the same way:
<p>{{translate("Hello {{name\}\}!")}}</p>
The only slightly tricky bit is that the pre-loader escapes the } characters, otherwise Vue treats them as the end of the template string and it results in JavaScript errors.
At this point, Vue won’t replace {{name}} with the actual value of name because it’s part of a JavaScript string, not part of the template. The good part of that is that our translation function will now be looking up the string to translate prior to variable substitution. So it will look for a translation for “Hello {{name}}!” rather than needing translations for every possible name. Once we find the translation, the translate function then uses a regex to find and replace each placeholder with the actual value from the vue instance.
Notably, this process means that you can’t use anything but a simple variable name in placeholders within translated strings (though it can refer to a computed property). This is actually a good thing because we don’t want to send a string off to the translators that contains complex JavaScript they won’t understand. In fact, we have tests that place additional restrictions on strings to translate to ensure they are easily manageable through the translation process.