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

this.$moment().locale(lang) does not works #40

Closed
nioc opened this issue Aug 31, 2017 · 7 comments
Closed

this.$moment().locale(lang) does not works #40

nioc opened this issue Aug 31, 2017 · 7 comments
Assignees

Comments

@nioc
Copy link

nioc commented Aug 31, 2017

Hello, I want to change locale with a user click event on button.

I have the following

// main.js
import Vue from 'vue'
import VueMoment from 'vue-moment'
const moment = require('moment')
require('moment/locale/en-gb')
require('moment/locale/fr')
Vue.use(VueMoment, {
  moment
})

And then, using in component:

// app.vue
<script>
  methods: {
    setLanguage (lang) {
      console.log(lang)
      this.$moment().locale(lang)
      console.log(this.$moment().locale())
    }
  }
}
</script>

When I call the function from the button, I got the following

setLanguage('en')
en
fr
setLanguage('fr')
fr
fr

Is it possible to update the moment locale this way?

@BrockReece
Copy link
Collaborator

I think this issue would be solved by PR #37

@BrockReece
Copy link
Collaborator

Hi @nioc,
We have now merged in PR #37, I believe this refactor will fix your issue.

// app.vue
<script>
  methods: {
    setLanguage (lang) {
      console.log(lang)
      this.$moment.locale(lang)
      console.log(this.$moment.locale())
    }
  }
}
</script>

@nioc
Copy link
Author

nioc commented Oct 12, 2017

Hello, great news. I'll try it tonight or tomorrow. Stay tuned :)

@nioc
Copy link
Author

nioc commented Oct 15, 2017

I'm sorry but I think PR does not fix my issue 😢
I launched npm update, checked my package.json seems correct : "vue-moment": "^2.1.0"
I also tried to change the value en to en-gb, still do not change...

@BrockReece
Copy link
Collaborator

ok, so I think the ordering of the imports of the locales is important as it uses the last import as the default locale. If I want 'en-gb' as my default, but an option to use 'fr', I order my imports like this...

import 'moment/locale/fr'
import 'moment/locale/en-gb'

This is the code I have put in my mounted method

mounted() {
  console.log(this.$moment.locale())
  this.$moment.locale('fr')
  console.log(this.$moment.locale())
  this.$moment.locale('en-gb')
  console.log(this.$moment.locale())
},

Which outputs...

en-gb
fr
en-gb

Which is what I believe we are after?

@BrockReece BrockReece self-assigned this Oct 16, 2017
@BrockReece
Copy link
Collaborator

I think this is fixed now

@pakping
Copy link

pakping commented Jul 26, 2021

nuxt
1
import 'moment/locale/th'

2
moment(date) {
// moment.locale('th');
var strdate = moment("th").format("LLLL");
var strdate = moment(date).add(543, "years");
return moment(strdate).format("Do MMMM YYYY hh:mm");
},

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

3 participants