Vue-multiselect: Change Language

Created on 2 Sep 2018  路  3Comments  路  Source: shentao/vue-multiselect

How can I add different language options?

Because the users do not speak english.

help wanted

Most helpful comment

I think you can add translations as part of the option slot.
See: https://vue-multiselect.js.org/#sub-custom-option-template

You could do something like:

<template slot="option" slot-scope="props">
      {{ translate(props.option.label) }}
</template>

All 3 comments

I think you can add translations as part of the option slot.
See: https://vue-multiselect.js.org/#sub-custom-option-template

You could do something like:

<template slot="option" slot-scope="props">
      {{ translate(props.option.label) }}
</template>

anyone wants to change language of some elemnts like placeHolder or empty list message to any language he wants , you can use something like this (I use arabic language) :
<multiselect v-model="value2" :options="modules" :close-on-select="true" :preserve-search="true" placeholder="Module" selectLabel="丕囟睾胤 賱丕禺鬲賷丕乇 賴匕丕 丕賱毓賳氐乇" label="intitule" track-by="id" id="example2" @select="onSelect2" > <span slot="noOptions">丕賱賱丕卅丨丞 賮丕乇睾丞</span> <span slot="noResult">賱丕 鬲賵噩丿 賳鬲賷噩丞</span> </multiselect>
and of course you can use props and slots to change what you want using this link : https://vue-multiselect.js.org/#sub-props

hope this can help someone .

I think you can add translations as part of the option slot.
See: https://vue-multiselect.js.org/#sub-custom-option-template

You could do something like:

<template slot="option" slot-scope="props">
      {{ translate(props.option.label) }}
</template>

This is actually a really clean solution!
Before I saw this I had a computed() with ids and translated labels and then shove them into :options. Would also work, but in every POST I had to loop over the entire data structure and delete the "label" part and only maintain the "id".
With your current solution I just have a simple value. This simplifies my code by a ton.

With the code mentioned above it still wouldn't translate the currently selected value, this can be done with:

<multiselect 
...
:custom-label="translatedLabel"
...
/>

and in methods() you translate the string (using template literals for example):

translatedLabel (id) {
      return this.$t(`path.to.i18n.field.options.${id}`)
}

EDIT: Or in my case I _directly_ used:

:custom-label="(id)=>$t(`somefile.fields.someinput.options.${id}`)"

to be able to specify own i18n paths for each input. Arrow functions are sweet!

Awesome library! :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bushcode picture bushcode  路  3Comments

zachleigh picture zachleigh  路  3Comments

stefanheimann picture stefanheimann  路  4Comments

wujekbogdan picture wujekbogdan  路  4Comments

yaakovp picture yaakovp  路  3Comments