-
-
Notifications
You must be signed in to change notification settings - Fork 316
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
Locale property of useI18n seems as it is not Reactive #132
Comments
|
There is no problem with using |
@YusufISMAILOGLU can you use I'm using Script Setup syntax, and this export seems to work as reactive in the template: export const { locale } = useI18n(); |
@ux-engineer thank you for your feedback, but |
@YusufISMAILOGLU |
import { createI18n } from "vue-i18n";
const i18n = createI18n({
legacy: true,
locale: process.env.VUE_APP_I18N_LOCALE || "en",
fallbackLocale: process.env.VUE_APP_I18N_FALLBACK_LOCALE || "en",
messages: {
en: {
msg: "Hello",
},
tr: {
msg: "Merhaba",
},
},
});
export default i18n;
import { createApp } from "vue";
import App from "./App.vue";
import router from "./router/router";
import i18n from "./i18n";
import "./index.css";
// use beforeEach route guard to set the language
router.beforeEach((to, form, next) => {
// use the language form the routing params or default language
let language = to.params.lang;
if (!["en", "tr"].includes(language)) {
return next("en");
}
if (i18n.locale !== language) {
i18n.locale = language; // update the locale
}
return next();
});
createApp(App).use(router).use(i18n).mount("#app");
<template>
<h1>{{ t("msg") }} Vue 3.0 + Vite</h1>
<button @click="count++">count is: {{ count }}</button>
</template>
<script>
import { reactive, toRefs } from "vue";
import { useI18n } from "vue-i18n";
export default {
name: "HelloWorld",
setup() {
const state = reactive({
count: 0,
});
console.log(useI18n().locale.value); //should console the updated value which set in main.js file,
// but it does not and console the default locale which defined in i18n.js
return {
...toRefs(state),
...useI18n(),
};
},
};
</script> |
Thanks! |
You need to use
|
If set |
vue & vue-i18n version
|
By using |
@YusufISMAILOGLU I'd advise against using this syntax: return {
...useI18n(),
}; This way you are exposing everything to the template when actually needing just one property. Instead consider using object-desctructed properties: const { locale } = useI18n();
return {
locale,
}; Also in some cases, you might need to wrap the returned property as ref, but I'm not yet sure when this is needed to be more specific...perhaps not helpful here: const { locale } = useI18n();
return {
locale: ref(locale),
}; |
Actually in my app I use object destructuring, but for purpose of clarity I used |
Hi
vue-i18n
authors,I am working on Vue 3 project and trying to switch locale via URL
i18n.locale=to.params.lang
. In the components where I am using...useI18n()
inside the return ofsetup()
to retrievelocale
, but locale property does not change dynamically as expected.Thanks!
The text was updated successfully, but these errors were encountered: