Comments (13)
Actually in my app I use object destructuring, but for purpose of clarity I used ...useI18n()
in my aforementioned example.
@ux-engineer Thank you for your advice.
from vue-i18n-next.
setup
with object spread (...useI18n({ ... })
), it's merged into the render context.
So you need to use locale
on the template side.
from vue-i18n-next.
There is no problem with using locale
on the template side, the problem is with its value which is static as set inside createI18n
and does not update on the template side when switching locale in URL.
from vue-i18n-next.
@YusufISMAILOGLU can you use {{ $i18n.locale }}
in template?
I'm using Script Setup syntax, and this export seems to work as reactive in the template:
export const { locale } = useI18n();
from vue-i18n-next.
@ux-engineer thank you for your feedback, but useI18n
can be used just inside setup
. when
return { ...useI18n() }
locale
should be exposed to template and it is true but it is NOT reactive .
from vue-i18n-next.
@YusufISMAILOGLU
Could you provide minimum repro codes please?
from vue-i18n-next.
i18n.js
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;
main.js
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");
HelloWorld.vue
<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>
from vue-i18n-next.
Thanks!
and, could you provide vue & vue-i18n version, vue & vue-i18n version please ?
from vue-i18n-next.
i18n.js: if (i18n.locale !== language) { i18n.locale = language; // update the locale }
You need to use global
property i18n instance.
(i18n.global
return composer
object: https://github.com/intlify/vue-i18n-next/blob/master/docs/vue-i18n.i18n.md)
i18n.js: if (i18n.global.locale.value !== language) { i18n.global.locale.value = language; // update the locale }
from vue-i18n-next.
i18n.js: const i18n = createI18n({ legacy: true,
If set legacy: true
, useI18n
cannot be used with the composition API.
from vue-i18n-next.
Thanks!
and, could you provide vue & vue-i18n version, vue & vue-i18n version please ?
vue & vue-i18n version
- vue: 3.0.0
- vite-plugin-vue-i18n: 1.0.0-alpha.2
from vue-i18n-next.
By using global
property i18n instance, it woks fine.
@kazupon appreciate your help.
from vue-i18n-next.
@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),
};
from vue-i18n-next.
Related Issues (20)
- add possible errors on API docs HOT 3
- Modifiers for interpolated values
- How to use named interpolation in html HOT 1
- [v9.10.0] `@intlify/message-compiler` publishing missing TypeScript reference HOT 4
- Message compiler still ships with dist with runtime only HOT 3
- te function returns false although fallback exists HOT 5
- Using a implicit fallback throws a warning in the console HOT 1
- Incorrect tm return type HOT 1
- Foolproofing and trigger error, when locale is not set. HOT 2
- regression: wrong `d.ts` of `@intlify/message-compiler` HOT 1
- Hyphens in placeholders causing error `Unterminated closing brace` in vue-i18n@9 HOT 1
- Guide leads to a 404 HOT 4
- Getting 'Cannot read properties of undefined (reading 't')' error in core-base.mjs::formatParts HOT 3
- Linked messages doesn't support chinese character as key HOT 1
- Property "$tc" was accessed during render but is not defined on instance. HOT 1
- Shortcut and more readable syntax for `v-t`: allow `<h1 v-t>Hello</h1>` which will be similar to `<h1 v-t="Hello"></h1>` HOT 4
- CSP Violation when `unsafe-eval` is not set HOT 2
- Key paths containing javascript built-ins are handled inconsistently HOT 3
- Interpolations get lost HOT 2
- SSR without NODE_ENV loads devtools and causes memory leak HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vue-i18n-next.