Home  >  Q&A  >  body text

i18n localization for Vue 3 composition API not updated

<p>I'm trying to change the locale for Vue-i18n by clicking a button. But when I click the button, nothing happens. There are no errors either. how to solve this problem? </p> <pre class="brush:php;toolbar:false;"><button v-on:click.prevent="setLocale('id')">Ind</button> <button v-on:click.prevent="setLocale('en')">Eng</button> import i18n from "../i18n"; const setLocale = (lang) => { i18n.global.locale = lang; };</pre> <p><br /></p>
P粉752826008P粉752826008396 days ago372

reply all(1)I'll reply

  • P粉550823577

    P粉5508235772023-08-26 00:14:37

    There may be other issues, but there is at least one bug in the code. i18n.global.locale is a ref and should be used as follows:

    const setLocale = (lang) => {
      i18n.global.locale.value = lang;
    };

    It cannot be done in a reactive manner.

    reply
    0
  • Cancelreply