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

Translating with Composition API outside of template tags, does not load the right locale #1916

Open
4 tasks done
fl0wo opened this issue Aug 13, 2024 · 2 comments
Open
4 tasks done
Labels
Status: Need More Info Lacks enough info to make progress

Comments

@fl0wo
Copy link

fl0wo commented Aug 13, 2024

Reporting a bug?

I'm using Astro + Vue and vue-i18n.

When translating using composition api, inside the "script setup" block, seems that Vue fallbacks to english locale for some reason.

After 10ms on load page (basically onMounted or onCompleted) the translation function uses the right locale fetched from the url.

But, after those 10ms, only the t('a') call inside the template is reactive, while the const variable in the setup script does not update.

Any way I could find a work-around for this?

<script setup lang="ts">

import {useI18n} from "vue-i18n";

const {t} = useI18n({
  useScope: 'global',
});

const audienceSizeLabels = {
  "a": t('a'),
  "b": t('b'),
};

</script>
<template>
{{t('a')}} <!-- this is translated -->
{{audienceSizeLabels['a']}} <!-- this is not (fallbacks to en locale) -->
</template>

Expected behavior

I'd expect to find a way to make the object reactive to locale changes.

Reproduction

<script setup lang="ts">

import {useI18n} from "vue-i18n";

const {t} = useI18n({
  useScope: 'global',
});

const audienceSizeLabels = {
  "a": t('a'),
  "b": t('b'),
};

</script>
<template>
{{t('a')}} <!-- this is translated -->
{{audienceSizeLabels['a']}} <!-- this is not-->
</template>

System Info

System:
    OS: macOS 14.2.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 398.58 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.0 - ~/.nvm/versions/node/v20.11.0/bin/node
    Yarn: 1.22.21 - /opt/homebrew/bin/yarn
    npm: 10.8.1 - ~/Library/pnpm/npm
    pnpm: 9.5.0 - ~/.nvm/versions/node/v20.11.0/bin/pnpm
  Browsers:
    Chrome: 127.0.6533.100
    Safari: 17.2.1
  npmPackages:
    vue: ^3.4.31 => 3.4.37 
    vue-i18n: ^9.13.1 => 9.13.1 
    vue-tsc: ^2.0.26 => 2.0.29

Screenshot

No response

Additional context

No response

Validations

@fl0wo fl0wo added the Status: Review Needed Request for review comments label Aug 13, 2024
@kazupon
Copy link
Member

kazupon commented Aug 27, 2024

Thank you for your reporting!

Unfortunately, I am not familiar astro.
Could you provide a minimum reporoduction, e.g. using stackbliz? 🙏

@kazupon kazupon added Status: Need More Info Lacks enough info to make progress and removed Status: Review Needed Request for review comments labels Aug 27, 2024
Copy link

Thank you for your feadback!
Would you be able to provide a reproduction 🙏

More info

Why do I need to provide a reproduction?

Reproductions make it possible for us to triage and fix issues quickly with a relatively small team. It helps us discover the source of the problem, and also can reveal assumptions you or we might be making.

What will happen?

If you've provided a reproduction, we'll remove the label and try to reproduce the issue. If we can, we'll mark it as a bug and prioritise it based on its severity and how many people we think it might affect.

If Status: Need More Info labeled issues don't receive any substantial activity (e.g., new comments featuring a reproduction link), we'll close them. That's not because we don't care! At any point, feel free to comment with a reproduction and we'll reopen it.

How can I create a reproduction?

We have a couple of templates for starting with a minimal reproduction:

👉 Reproduction mininal starter
👉 Reproduction starter with unpluguin-vue-i18n

A public GitHub repository is also perfect. 👌

Please ensure that the reproduction is as minimal as possible.

You might also find these other articles interesting and/or helpful:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Need More Info Lacks enough info to make progress
Projects
None yet
Development

No branches or pull requests

2 participants