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

Instant changed language after call method "this.$moment.locale(myNewLocale)" #108

Open
dawid-kokoszka opened this issue Nov 9, 2018 · 3 comments

Comments

@dawid-kokoszka
Copy link

I use this configuration in main.js

const moment = require('moment')
require('moment/locale/es')

Vue.use(require('vue-moment'), {
    moment
})

and everything works great but when I called a method
this.$moment.locale(myNewLocale)
in my component the language change only after reloading component/view.
Is there a way to translate immediately?

@dawid-kokoszka
Copy link
Author

I found a solution but I don't know if is corrected. Below method which changing languages. I used "forceUpdate" to rerender view

changeLang (lang){
     this.$moment.locale(lang)
     this.$forceUpdate()
}

@dawid-kokoszka
Copy link
Author

this solution works only within the range of own component. How I can update child components or how create a global component with lang switcher?

@carlosgeos
Copy link

I think the rerender with force update is necessary like you said. I found a solution to update the rest of components.

I have the lang switcher in a sub component (not global), that emits a signal to this.$root when the language is changed:

this.$root.$emit('locale_signal')

Now, elements that use vue-moment filters etc, can listen to this signal and auto update themselves:

   mounted () {
     this.$root.$on("locale_signal", () => {
       this.$forceUpdate();
     })
   }

I am not using Vuex so I am using this.$root like a message center where ALL components can emit and listen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants