Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

feat(datetimepicker): add support for multiple languages #438

12 changes: 9 additions & 3 deletions src/components/DateTimePicker/DateTimePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { enUS, ptBR } from 'date-fns/locale'
import * as locales from 'date-fns/locale'
import React, { ReactNode, CSSProperties } from 'react'
import InputGroup from 'react-bootstrap/InputGroup'
import DatePicker, { registerLocale, setDefaultLocale } from 'react-datepicker'

import { Icon } from '../Icon'
import 'react-datepicker/dist/react-datepicker.css'

registerLocale('enUS', enUS)
registerLocale('ptBR', ptBR)
// Generic way to register all available locales
Object.entries(locales).forEach(([key, locale]) => {
registerLocale(key, locale)
})

// Additional locales required for the frontend
registerLocale('ar', locales.arDZ)
registerLocale('zr', locales.zhCN)

setDefaultLocale('enUS')

Expand Down
1 change: 1 addition & 0 deletions src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const Dropdown = (props: Props) => {
style={item.style}
key={item.key || `dropdown-item-${i.toString()}`}
eventKey={item.eventKey || `dropdown-event-${i.toString()}`}
onClick={item.onClick}
>
{item.text}
</DropdownRB.Item>
Expand Down
38 changes: 37 additions & 1 deletion stories/datetimepicker.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { storiesOf } from '@storybook/react'
import * as locales from 'date-fns/locale'
import React, { useState } from 'react'

import { DateTimePicker } from '../src'
import { DateTimePicker, Dropdown } from '../src'

storiesOf('DateTimePickers', module)
.addParameters({
Expand Down Expand Up @@ -289,3 +290,38 @@ storiesOf('DateTimePickers', module)
</div>
)
})
.add('Supported languages', () => {
const [startDate, setStartDate] = useState(new Date())
const [language, setLanguage] = useState('enUS')
const items = Object.keys(locales).map((locale) => ({
text: locale,
onClick: () => {
setLanguage(locale)
},
}))
const ar = {
text: 'ar',
onClick: () => {
setLanguage('ar')
},
}
const zr = {
text: 'zr',
onClick: () => {
setLanguage('zr')
},
}
items.push(ar)
items.push(zr)

return (
<div>
<Dropdown text={language} items={items} />
<DateTimePicker
locale={language}
selected={startDate}
onChange={(date) => setStartDate(date)}
/>
</div>
)
})