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

Language: Days get settled language but months don't #480

Closed
tonydtran opened this issue Apr 28, 2017 · 7 comments
Closed

Language: Days get settled language but months don't #480

tonydtran opened this issue Apr 28, 2017 · 7 comments
Labels
bug Oh no, something's broken :-( pull request wanted This is a great way to contribute! Help us out :-D

Comments

@tonydtran
Copy link

tonydtran commented Apr 28, 2017

Hi guys, I have an issue I hope you can help. The question is in the title, the settled language is in French, so days are in French but months stay in English. I tried everything I could, I think you guys would be better than me. I'm sure the problem is very simple, but I don't see it !

The baby coder that I am thanks you a lot !

import React, { Component } from 'react'
import { SingleDatePicker } from 'react-dates'
import moment from 'moment'

moment.locale('fr')

class DatePicker extends Component {
  state = {}

  componentWillReceiveProps (props) {
    if (props.focused) {
      this.setState({focused: props.focused})
    }
  }

  handleChange = (onChange, value) => {
    this.setState({ focused: false }, () => {
      onChange(value)
    })
  }

  render () {
    const { input: { onChange, value } } = this.props

    return (
      <SingleDatePicker
        date={value || null}
        onDateChange={(value) => this.handleChange(onChange, value)}
        focused={this.state.focused}
        onFocusChange={({ focused }) => this.setState({ focused })}
        numberOfMonths={1}
        displayFormat={() => moment.localeData('fr').longDateFormat('L')}
      />
    )
  }
}

export default DatePicker

capture d ecran 2017-04-28 a 19 30 31

@ljharb
Copy link
Member

ljharb commented Apr 28, 2017

(unrelated to your problem; i'd put the moment.locale() call in componentWillMount prior to the first render, rather than in the top level of the module)

I believe to fix this we'd need to save the moment locale in the state of CalendarMonthGrid, and in componentWillReceiveProps, recalculate the months if the locale has changed. @majapw, thoughts?

@tonydtran
Copy link
Author

Thank you @ljharb for your quick answer. I took your advice but the problem is still there (I understand it wasn't a solution but an advice, thanks again about that). What can I do to resolve this ? Sorry for my poor javascript skill, just started to learn last year :)

@majapw
Copy link
Collaborator

majapw commented Apr 28, 2017

@ljharb that seems like a reasonable solution. Someone else was having the same problem as well... why does it work in the storybook is my Q I guess?

@majapw
Copy link
Collaborator

majapw commented Apr 28, 2017

We need to submit a PR to make this happen! 🎉

@ljharb ljharb added bug Oh no, something's broken :-( pull request wanted This is a great way to contribute! Help us out :-D labels Apr 28, 2017
@aszx87410
Copy link

aszx87410 commented Jun 14, 2017

I also encounter this problem and solved by changing the locale in the constructor.

example:

class DatePicker extends Component {
  constructor (props) {
    super(props);
    moment.locale('zh-tw');
  }
}

@tonydtran
Copy link
Author

tonydtran commented Jun 22, 2017 via email

@phsantiago
Copy link

I've a similar problem, solved adding a import 'moment/locale/fr' after moment import

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Oh no, something's broken :-( pull request wanted This is a great way to contribute! Help us out :-D
Projects
None yet
Development

No branches or pull requests

5 participants