Translating props in Vue.js

Asked By: Anonymous

I am using vuex-i18n to translate my strings and bootstrap-vue for styling.
Every bootstrap-component has props that I need to translate:

<b-input id="input2" placeholder="Username" />

Vue binds the raw text (here: Username) to the placeholder-prop and it obviously gets rendered verbatim (Username).

If I want to translate my placeholder, I think I have to bind it to a variable or computed property

<b-input id="input2" :placeholder="username" />

and translate it in the script section:

computed: {
  username() {
    return this.$t('forms.placeholders.username')
  }

That is getting very verbose. Is there a better way?

Clarification:
What I would like to avoid, is having to create dozens of computed properties which basically return a translated string of the property’s name:

placeholder=*username*
...
*username* ()
  return $t('*username*')

Solution

Answered By: Anonymous

You can directly pass the localized string into the prop instead of handling it inside the target component:

<b-input id="input2" :placeholder="$t('forms.placeholders.' + username)" />

You can also create a helper function by creating a vue plugin to wrap this code and make it shorter:

Vue.use({
    install(Vue) {
        Vue.prototype.localizedTextFct = function (placeholder) {
            return this.$t('forms.placeholders.' + placeholder)
        }
    }
});

new Vue({
    ...
})

and you can now use the helper function like this:

<b-input id="input2" :placeholder="localizedTextFct(username)" />

techinplanet staff

techinplanet staff


Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno