Skip to content

Commit 840ed76

Browse files
fix: add Bengali (Bangladesh) [bn-bd] locale (#1806)
1 parent 32b7fe3 commit 840ed76

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

src/locale/bn-bd.js

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Bengali (Bangladesh) [bn-bd]
2+
import dayjs from 'dayjs'
3+
4+
const symbolMap = {
5+
1: '১',
6+
2: '২',
7+
3: '৩',
8+
4: '৪',
9+
5: '৫',
10+
6: '৬',
11+
7: '৭',
12+
8: '৮',
13+
9: '৯',
14+
0: '০'
15+
}
16+
17+
const numberMap = {
18+
'১': '1',
19+
'২': '2',
20+
'৩': '3',
21+
'৪': '4',
22+
'৫': '5',
23+
'৬': '6',
24+
'৭': '7',
25+
'৮': '8',
26+
'৯': '9',
27+
'০': '0'
28+
}
29+
30+
const locale = {
31+
name: 'bn-bd',
32+
weekdays: 'রবিবার_সোমবার_মঙ্গলবার_বুধবার_বৃহস্পতিবার_শুক্রবার_শনিবার'.split('_'),
33+
months: 'জানুয়ারি_ফেব্রুয়ারি_মার্চ_এপ্রিল_মে_জুন_জুলাই_আগস্ট_সেপ্টেম্বর_অক্টোবর_নভেম্বর_ডিসেম্বর'.split('_'),
34+
weekdaysShort: 'রবি_সোম_মঙ্গল_বুধ_বৃহস্পতি_শুক্র_শনি'.split('_'),
35+
monthsShort: 'জানু_ফেব্রু_মার্চ_এপ্রিল_মে_জুন_জুলাই_আগস্ট_সেপ্ট_অক্টো_নভে_ডিসে'.split('_'),
36+
weekdaysMin: 'রবি_সোম_মঙ্গ_বুধ_বৃহঃ_শুক্র_শনি'.split('_'),
37+
weekStart: 0,
38+
preparse(string) {
39+
return string.replace(/[]/g, match => numberMap[match])
40+
},
41+
postformat(string) {
42+
return string.replace(/\d/g, match => symbolMap[match])
43+
},
44+
ordinal: (n) => {
45+
const s = ['ই', 'লা', 'রা', 'ঠা', 'শে']
46+
const v = n % 100
47+
return `[${n}${s[(v - 20) % 10] || s[v] || s[0]}]`
48+
},
49+
formats: {
50+
LT: 'A h:mm সময়',
51+
LTS: 'A h:mm:ss সময়',
52+
L: 'DD/MM/YYYY খ্রিস্টাব্দ',
53+
LL: 'D MMMM YYYY খ্রিস্টাব্দ',
54+
LLL: 'D MMMM YYYY খ্রিস্টাব্দ, A h:mm সময়',
55+
LLLL: 'dddd, D MMMM YYYY খ্রিস্টাব্দ, A h:mm সময়'
56+
},
57+
meridiem: hour =>
58+
/* eslint-disable no-nested-ternary */
59+
(hour < 4
60+
? 'রাত'
61+
: hour < 6
62+
? 'ভোর'
63+
: hour < 12
64+
? 'সকাল'
65+
: hour < 15
66+
? 'দুপুর'
67+
: hour < 18
68+
? 'বিকাল'
69+
: hour < 20
70+
? 'সন্ধ্যা'
71+
: 'রাত'),
72+
relativeTime: {
73+
future: '%s পরে',
74+
past: '%s আগে',
75+
s: 'কয়েক সেকেন্ড',
76+
m: 'এক মিনিট',
77+
mm: '%d মিনিট',
78+
h: 'এক ঘন্টা',
79+
hh: '%d ঘন্টা',
80+
d: 'এক দিন',
81+
dd: '%d দিন',
82+
M: 'এক মাস',
83+
MM: '%d মাস',
84+
y: 'এক বছর',
85+
yy: '%d বছর'
86+
}
87+
}
88+
89+
dayjs.locale(locale, null, true)
90+
91+
export default locale

test/locale/bn-bd.test.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import moment from 'moment'
2+
import MockDate from 'mockdate'
3+
import dayjs from '../../src'
4+
import relativeTime from '../../src/plugin/relativeTime'
5+
import localeData from '../../src/plugin/localeData'
6+
import preParsePostFormat from '../../src/plugin/preParsePostFormat'
7+
import '../../src/locale/bn-bd'
8+
9+
dayjs.extend(localeData)
10+
dayjs.extend(relativeTime)
11+
dayjs.extend(preParsePostFormat)
12+
13+
beforeEach(() => {
14+
MockDate.set(new Date())
15+
})
16+
17+
afterEach(() => {
18+
MockDate.reset()
19+
})
20+
21+
it('Format Month with locale function', () => {
22+
for (let i = 0; i <= 7; i += 1) {
23+
const dayjsBN = dayjs()
24+
.locale('bn-bd')
25+
.add(i, 'day')
26+
const momentBN = moment()
27+
.locale('bn-bd')
28+
.add(i, 'day')
29+
const testFormat1 = 'DD MMMM YYYY MMM'
30+
const testFormat2 = 'MMMM'
31+
const testFormat3 = 'MMM'
32+
expect(dayjsBN.format(testFormat1)).toEqual(momentBN.format(testFormat1))
33+
expect(dayjsBN.format(testFormat2)).toEqual(momentBN.format(testFormat2))
34+
expect(dayjsBN.format(testFormat3)).toEqual(momentBN.format(testFormat3))
35+
}
36+
})
37+
38+
it('Month short', () => {
39+
const date = '2021-02-01T05:54:32.005Z'
40+
const dayjsBN = dayjs(date)
41+
.locale('bn-bd')
42+
const momentBN = moment(date)
43+
.locale('bn-bd')
44+
const testFormat1 = 'DD MMMM YYYY MMM'
45+
expect(dayjsBN.format(testFormat1)).toEqual(momentBN.format(testFormat1))
46+
})
47+
48+
it('Preparse with locale function', () => {
49+
for (let i = 0; i <= 7; i += 1) {
50+
dayjs.locale('bn-bd')
51+
const momentBN = moment()
52+
.locale('bn-bd')
53+
.add(i, 'day')
54+
expect(dayjs(momentBN.format()).format()).toEqual(momentBN.format())
55+
}
56+
})
57+
58+
it('RelativeTime: Time from X', () => {
59+
const T = [
60+
[44.4, 'second'], // a few seconds
61+
[89.5, 'second'], // a minute
62+
[130, 'second'], // two minutes
63+
[43, 'minute'], // 44 minutes
64+
[1, 'hour'], // 1 hour
65+
[21, 'hour'], // 21 hours
66+
[2, 'day'], // 2 days
67+
[25, 'day'], // 25 days
68+
[2, 'month'], // 2 months
69+
[10, 'month'], // 10 months
70+
[18, 'month'], // 2 years
71+
[15, 'year'] // 15 years
72+
]
73+
74+
T.forEach((t) => {
75+
dayjs.locale('bn-bd')
76+
moment.locale('bn-bd')
77+
expect(dayjs().from(dayjs().add(t[0], t[1]))).toBe(moment().from(moment().add(t[0], t[1])))
78+
expect(dayjs().from(dayjs().add(t[0], t[1]), true))
79+
.toBe(moment().from(moment().add(t[0], t[1]), true))
80+
})
81+
})

0 commit comments

Comments
 (0)