-
-
Notifications
You must be signed in to change notification settings - Fork 861
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
How do I change the language after configuration? #2
Comments
sorry, this plugin cannot change the language after the configuration. |
@thelinuxlich <p>{{ $t('message.hello', 'en') }}</p> Please try it !! |
I tested it and it is useful but I think that it would be better to somehow set lang parameter as observable and change globally based on it |
I add |
This doesn't solve the issue unless Vue.config.lang is reactive |
Sorry,
You need reactive |
yeah, I believe it's a common case for the user switching the language during interaction |
OK. |
How about this: http://jsfiddle.net/nkovacs/1zh3agxr/? See also https://github.com/yyx990803/vue/issues/1114 |
Any news on this one? |
I'm interested in easy switching languages by user too. |
+1 |
+1 |
1 similar comment
+1 |
+1 |
@kazupon This package https://github.com/Haixing-Hu/vue-i18n has support for this through jQuery AJAX calls(from what I can tell). Perhaps you can use some of it's logic to solve this issue. Just a thought! |
@sagaio: nope, i'm currently working around it by doing a page reload when language was changed.
works, but ain't pretty! |
+1 |
1 similar comment
+1 |
+1 |
Was thinking of this issue again today and came up with this solution. First, we have someplace in our ui to change language. to change language you do something like this select(code) {
Vue.config.lang = code;
this.$broadcast('language');
} to put translations in our templates, we now do and finally a T.vue component: <template>
<span>
{{ translated }}
</span>
</template>
<script>
export default {
props: {
t: '',
},
data() {
return {
translated: '',
};
},
ready() {
this.translated = this.$t(this.t);
this.$on('language', function() {
this.translated = this.$t(this.t);
});
},
};
</script> Now your language changes are reactive. |
+1 |
@pespantelis thanks a lot : ) I solved it a different way, as I use vue-router: in the main js file:
and the lang-select component:
What do you think about this solution? |
@pespantelis' altering of the prototype is by far the most elegant solution. Really feels like it should be baked in. |
With Vue i18n 6.0, you need to adapt the
|
You can set the locale via |
Ha, @kazupon, your response fixed it. Been trying for 3 hours but we only had do to this haha; The value en can be dynamic.
If it does not work, make sure you can console log the right instance. |
thanks for the information @nickkuijpers, worked great. |
@nickkuijpers and @TheBroox the documentation says this property is readonly. That is a typo I guess? |
@roelvan i think so because in my end it works. Have u tried it? |
@roelvan it worked for me but after upgrading to i18n 6.0+ I have switched to @ssc-hrep3's solution. It still extends the prototype but the altered values are slightly different. My i18n file:
And then how I incorporate it into a template:
|
@nickkuijpers yep it seems to work for me. Thanks for sharing @ssc-hrep3's elegant solution @TheBroox. I used your snippet :) |
I think the Elegant Solution is For example</template>
<button @click="setLanguage('en')">en</button>
<button @click="setLanguage('de')">de</button>
<button @click="setLanguage('fr')">fr</button>
</template>
<script>
export default {
....
methods: {
setLanguage (val) {
this.$i18n.locale = val
}
}
}
</script> it will update language without refresh page or reload or redirect. |
In my case, Laravel takes care of filling up the 'lang' attribute in html so I see no reason to make this any more complicated. |
If you are looking for a completely js solution that can be set on page load via query string param as well as offering buttons to the user.
|
@EmadAdly I tried your code as it's very similar to the one in the docs. PS: Everything inside this components changes, but other components stay the same. Until I change the page. |
Well, seems like I used the translations in a wring way. |
@janein setLanguage (val) {
this.$i18n.locale = val
window.localStorage.setItem('Language', val)
} and in main.js read locale from local storage like the below const i18n = new VueI18n({
locale: window.localStorage.getItem('userLanguage'), // get locale
messages // set locale messages
}) |
If you want to do it inside a component:
If you want to use in vuex, so you can easily access/watch in other components:
|
@EmadAdly @longprao Thanks, your approach looks nice.
This works pretty neat as it handles all settings and not just the language. |
What if I want to reload the page to receive new content based on the user language and also I want to change the stylesheet file based on it? |
Your ideas were helpful. i solve the problem and i didn't use VueX
|
I use another solution for setting language (in main.js): const i18n = new VueI18n({ |
In my vue component file I have a method that can change. Just tested this and it was reactive to change it from en to fr and the UI updated straight away.
|
I am doing this
|
this.$i18n.locale = 'your locale' |
closes #531 * docs: add vuepress zh docs config * docs(zh/README.md): Translation README.md file * docs(zh/README.md): Translation installation.md file * docs: Translation introduction.md file * docs: Translation started.md file * docs: 更改称谓 * docs: Translation api/README.md file * style: values type * fix: pluralization -> 多元化 * fix: locale translation * docs: Translation api/formatting file * docs: Translation guide/pluralization.md file * fixed: fix type * docs: All translation completed * Update vuepress/.vuepress/config.js 参照Vue的规范修正译文中的格式 Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * fix: type * fix: 区域设置 -> 语言环境 * style: Modify the format including punctuation and spaces * fix: 修复vuepress config * Update if not ..., check ... -> 如果没有则 Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update 主语前置 Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update 完善用语 Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update 语义更明确 Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * fix: Correct the format to remove extra spaces * update: 提示 -> tip * Update vuepress/zh/api/README.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * 更改主谓 Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * 提高句子的可读性 Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * 提高句子的可读性 Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/api/README.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/api/README.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * fix: Fix some keyword errors and various issues * update: Optimize text for readability * Update vuepress/zh/guide/formatting.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/formatting.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/formatting.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/formatting.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/formatting.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/formatting.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/formatting.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/formatting.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/formatting.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/formatting.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/formatting.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/hot-reload.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/interpolation.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/interpolation.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/interpolation.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/interpolation.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/interpolation.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/lazy-loading.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/lazy-loading.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/lazy-loading.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/locale.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/locale.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/locale.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/lazy-loading.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/lazy-loading.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * 区域 -> 语言环境, 消息 -> 信息 * Update vuepress/zh/guide/messages.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/messages.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/messages.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/messages.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/number.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * fix: Locale Messages -> 语言环境信息 * 同步源仓库到Fock仓库 (#1) * ⭐ new(path): Keypath should parse if sub path contains spaces. (#533) by @exoego * feat(path): Keypath should parse if sub path contains spaces. * feat(path): Added test for whitespaces * 📝 docs(vuepress): pluralization will not work unless you use $tc (#540) by @lebesnec * ⭐ new(number): i18n-n functional component (#541) by @bponomarenko * Revert "同步源仓库到Fock仓库 (#1)" (#2) This reverts commit 8f8a88b. * Update vuepress/zh/guide/pluralization.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/pluralization.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/pluralization.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/sfc.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/sfc.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/sfc.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/sfc.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/sfc.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/sfc.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/sfc.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/sfc.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/sfc.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/sfc.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/sfc.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/guide/sfc.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * style: code style * Update vuepress/zh/installation.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/installation.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/installation.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/installation.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/installation.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/installation.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/introduction.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/introduction.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/introduction.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/introduction.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/started.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/started.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/started.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/started.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/started.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/legacy/README.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/legacy/README.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com> * Update vuepress/zh/legacy/README.md Co-Authored-By: xuhongbo <xuhongbo@outlook.com>
* - adds tests for fallbacks to be implemented (#2) * adds tests for fallbacks to be implemented (#2) * implementation for complex fallback variants (#2) * fixed test errors (#2) * fixed some documentation (#2) * fixes fallback test (#2) * refactor fallback tests (#2) * allow fallbackLocale to be false explicit (#2) * fixing test message (#2) * fixing use of wrong variable and type * Update src/index.js - according suggestion :+1 Co-Authored-By: kazuya kawaguchi <kawakazu80@gmail.com> Co-authored-by: kazuya kawaguchi <kawakazu80@gmail.com>
How to do this in vue3 |
No description provided.
The text was updated successfully, but these errors were encountered: