Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v-t directive does not rerender when locale switches #1994

Open
4 tasks done
DerZade opened this issue Oct 16, 2024 · 1 comment
Open
4 tasks done

v-t directive does not rerender when locale switches #1994

DerZade opened this issue Oct 16, 2024 · 1 comment
Labels
has the workaround legacy 🔨 p3-minor-bug Priority 3: a bug in an edge case that only affects very specific usage

Comments

@DerZade
Copy link

DerZade commented Oct 16, 2024

Reporting a bug?

v-t directive does not update when locale switches

Expected behavior

Changing the locale updates all references of v-t

Reproduction

Stackblitz: https://stackblitz.com/edit/vitejs-vite-3sfn11?file=src%2FApp.vue

GitHub Repo: https://github.com/DerZade/vue-i18n-locale-switching-bug-report

<script setup>
import { useI18n } from 'vue-i18n';

const { t, locale } = useI18n({ useScope: 'global' }); // useScope does not matter 
function changeLanguage() {
  locale.value = locale.value === 'en' ? 'ja' : 'en';
  console.log(locale.value);
}
</script>

<template>
  <button @click="changeLanguage">change</button>
  <span v-t="'hello'"></span>
</template>

System Info

System:
    OS: Linux 6.8 Ubuntu 24.04.1 LTS 24.04.1 LTS (Noble Numbat)
    CPU: (16) x64 AMD Ryzen 7 7840U w/ Radeon  780M Graphics
    Memory: 44.28 GB / 60.63 GB
    Container: Yes
    Shell: 5.2.21 - /bin/bash
  Binaries:
    Node: 20.18.0 - ~/.nvm/versions/node/v20.18.0/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v20.18.0/bin/yarn
    npm: 10.9.0 - ~/.nvm/versions/node/v20.18.0/bin/npm
  Browsers:
    Chrome: 129.0.6668.100
  npmPackages:
    @vitejs/plugin-vue: ^5.1.4 => 5.1.4 
    vite: ^5.4.8 => 5.4.9 
    vue: ^3.5.12 => 3.5.12 
    vue-i18n: ^10.0.4 => 10.0.4

Screenshot

No response

Additional context

I started investigating this and found that apparently the composer of the v-t in my example is not the global composer (I don't know why tho, since there is only one instance in the whole app), therefore the watcher for the locale isn't added in the register hook of the directive:

if (inBrowser && i18n.global === composer) {
// global scope only
el.__i18nWatcher = watch(composer.locale, () => {
binding.instance && binding.instance.$forceUpdate()
})
}

Really dumb question: Why can't we just always add the watcher, why does it only work for the global instance?

Validations

@DerZade DerZade added the Status: Review Needed Request for review comments label Oct 16, 2024
@kazupon
Copy link
Member

kazupon commented Oct 29, 2024

Thank you for your reporting!

Hmm.
vue-i18n does not seem to work v-t on legacy mode, currently.
you can avoid composition mode.
https://stackblitz.com/edit/vitejs-vite-gdnzpe?file=src%2FApp.vue,src%2Fmain.js

Legacy mode will be removed in future versions of vue-i18n.
I recommend you will use composition mode.

@kazupon kazupon added has the workaround 🔨 p3-minor-bug Priority 3: a bug in an edge case that only affects very specific usage legacy and removed Status: Review Needed Request for review comments labels Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has the workaround legacy 🔨 p3-minor-bug Priority 3: a bug in an edge case that only affects very specific usage
Projects
None yet
Development

No branches or pull requests

2 participants