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

<DatePicker /><DateRangePicker />の日付周りの表示を少し多言語対応 #633

Merged
merged 2 commits into from
Dec 16, 2021

Conversation

youchann
Copy link
Contributor

@youchann youchann commented Dec 15, 2021

Check List (If️ you added new component in this PR)

  • Export the component in src/components/index.ts
  • Add example to .storybook/documents/Information/Samples/Samples.stories.tsx
  • Localize added component

@netlify
Copy link

netlify bot commented Dec 15, 2021

✔️ Deploy Preview for ingred-ui ready!

🔨 Explore the source changes: 9749683

🔍 Inspect the deploy log: https://app.netlify.com/sites/ingred-ui/deploys/61b943f692b7e800080ff049

😎 Browse the preview: https://deploy-preview-633--ingred-ui.netlify.app

@youchann youchann self-assigned this Dec 15, 2021
Comment on lines +108 to +113
DatePicker: {
defaultProps: { displayFormat: "YYYY/MM/DD", monthFormat: "YYYY年 M月" },
},
DateRangePicker: {
defaultProps: { displayFormat: "YYYY/MM/DD", monthFormat: "YYYY年 M月" },
},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

表示のフォーマットを変更できるようにした

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TL; DR

  • moment.locale('ja') したうえで monthFormat : "YYYY年MMM" とする事もできるね(しなくてよい)
  • 真に多言語対応するときは考えたほうが良いかもしれないですね。
    • 特に曜日ヤバい
    • でも今はまだいいかな

jaJPのときは monthFormat YYYY年 M月

OriginalDateRangePicker として内部的に参照してるのはこれ。
https://github.com/airbnb/react-dates

react-dates の内部は ようするにmoment で変換している。

momentのフォーマットは、まぁ普通によくあるやつ( M = 1,2 ... 11, 12 , MM = 01, 02, ... 11, 12, MMM=Dec, MMMM=December
https://momentjs.com/docs/#/parsing/string-format/
moment("20121220", "YYYYMMDD").format('MM') 等で確認できる。


ここまでは良い。気になったのはmoment自体にもlocaleがあること。。。

locale('ja') を追加する
moment("20121220", "YYYYMMDD").locale('ja').format('MMMM') -> ​12月
(MMM も MMMM も同じ結果)

で、ここまでコード読んでしまいちゃんと react-dates のreadme読んでなかったので読む。
https://github.com/airbnb/react-dates#localization
Storybookには中国語サンプルが。
Storybook 2021-12-15 10-52-49
(別に中国人も「1月」でも「一月」でも通じると思われる…)

moment.locale('zh-cn'); と利用直前にセットするやつ。
https://github.com/airbnb/react-dates/blob/b8e26f01933aa6513af6fd22a7525ff1cd30df06/stories/DateRangePicker.js#L74-L101

moment.locale は振る舞いがglobalですからね。。。
https://momentjs.com/docs/#/i18n/changing-locale/

というわけで、曜日にまで踏み込むとかだとmoment.localeからやったほうが良さそう。
曜日は 星期一 とか 月曜日 とかだったり特に多様なので。

@@ -0,0 +1,158 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Tabs component testing Tabs 1`] = `
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なぜいままでなかったのか....:thinking:


<h2>DatePicker</h2>
<Typography>
※ Needs locale import (e.g. import &apos;moment/locale/ja&apos;).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

例えば曜日とかの表示は明示的にimportが必要

Copy link
Contributor

@kohashi kohashi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:komeshita:
全体としては良さそう。

@youchann youchann merged commit 4b110ef into master Dec 16, 2021
@youchann youchann deleted the fix-datepicker-locale branch December 16, 2021 08:43
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

Successfully merging this pull request may close these issues.

None yet

2 participants