-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add Bengali (Bangladesh) [bn-bd] locale (#1806)
- Loading branch information
1 parent
32b7fe3
commit 840ed76
Showing
2 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Bengali (Bangladesh) [bn-bd] | ||
import dayjs from 'dayjs' | ||
|
||
const symbolMap = { | ||
1: '১', | ||
2: '২', | ||
3: '৩', | ||
4: '৪', | ||
5: '৫', | ||
6: '৬', | ||
7: '৭', | ||
8: '৮', | ||
9: '৯', | ||
0: '০' | ||
} | ||
|
||
const numberMap = { | ||
'১': '1', | ||
'২': '2', | ||
'৩': '3', | ||
'৪': '4', | ||
'৫': '5', | ||
'৬': '6', | ||
'৭': '7', | ||
'৮': '8', | ||
'৯': '9', | ||
'০': '0' | ||
} | ||
|
||
const locale = { | ||
name: 'bn-bd', | ||
weekdays: 'রবিবার_সোমবার_মঙ্গলবার_বুধবার_বৃহস্পতিবার_শুক্রবার_শনিবার'.split('_'), | ||
months: 'জানুয়ারি_ফেব্রুয়ারি_মার্চ_এপ্রিল_মে_জুন_জুলাই_আগস্ট_সেপ্টেম্বর_অক্টোবর_নভেম্বর_ডিসেম্বর'.split('_'), | ||
weekdaysShort: 'রবি_সোম_মঙ্গল_বুধ_বৃহস্পতি_শুক্র_শনি'.split('_'), | ||
monthsShort: 'জানু_ফেব্রু_মার্চ_এপ্রিল_মে_জুন_জুলাই_আগস্ট_সেপ্ট_অক্টো_নভে_ডিসে'.split('_'), | ||
weekdaysMin: 'রবি_সোম_মঙ্গ_বুধ_বৃহঃ_শুক্র_শনি'.split('_'), | ||
weekStart: 0, | ||
preparse(string) { | ||
return string.replace(/[১২৩৪৫৬৭৮৯০]/g, match => numberMap[match]) | ||
}, | ||
postformat(string) { | ||
return string.replace(/\d/g, match => symbolMap[match]) | ||
}, | ||
ordinal: (n) => { | ||
const s = ['ই', 'লা', 'রা', 'ঠা', 'শে'] | ||
const v = n % 100 | ||
return `[${n}${s[(v - 20) % 10] || s[v] || s[0]}]` | ||
}, | ||
formats: { | ||
LT: 'A h:mm সময়', | ||
LTS: 'A h:mm:ss সময়', | ||
L: 'DD/MM/YYYY খ্রিস্টাব্দ', | ||
LL: 'D MMMM YYYY খ্রিস্টাব্দ', | ||
LLL: 'D MMMM YYYY খ্রিস্টাব্দ, A h:mm সময়', | ||
LLLL: 'dddd, D MMMM YYYY খ্রিস্টাব্দ, A h:mm সময়' | ||
}, | ||
meridiem: hour => | ||
/* eslint-disable no-nested-ternary */ | ||
(hour < 4 | ||
? 'রাত' | ||
: hour < 6 | ||
? 'ভোর' | ||
: hour < 12 | ||
? 'সকাল' | ||
: hour < 15 | ||
? 'দুপুর' | ||
: hour < 18 | ||
? 'বিকাল' | ||
: hour < 20 | ||
? 'সন্ধ্যা' | ||
: 'রাত'), | ||
relativeTime: { | ||
future: '%s পরে', | ||
past: '%s আগে', | ||
s: 'কয়েক সেকেন্ড', | ||
m: 'এক মিনিট', | ||
mm: '%d মিনিট', | ||
h: 'এক ঘন্টা', | ||
hh: '%d ঘন্টা', | ||
d: 'এক দিন', | ||
dd: '%d দিন', | ||
M: 'এক মাস', | ||
MM: '%d মাস', | ||
y: 'এক বছর', | ||
yy: '%d বছর' | ||
} | ||
} | ||
|
||
dayjs.locale(locale, null, true) | ||
|
||
export default locale |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import moment from 'moment' | ||
import MockDate from 'mockdate' | ||
import dayjs from '../../src' | ||
import relativeTime from '../../src/plugin/relativeTime' | ||
import localeData from '../../src/plugin/localeData' | ||
import preParsePostFormat from '../../src/plugin/preParsePostFormat' | ||
import '../../src/locale/bn-bd' | ||
|
||
dayjs.extend(localeData) | ||
dayjs.extend(relativeTime) | ||
dayjs.extend(preParsePostFormat) | ||
|
||
beforeEach(() => { | ||
MockDate.set(new Date()) | ||
}) | ||
|
||
afterEach(() => { | ||
MockDate.reset() | ||
}) | ||
|
||
it('Format Month with locale function', () => { | ||
for (let i = 0; i <= 7; i += 1) { | ||
const dayjsBN = dayjs() | ||
.locale('bn-bd') | ||
.add(i, 'day') | ||
const momentBN = moment() | ||
.locale('bn-bd') | ||
.add(i, 'day') | ||
const testFormat1 = 'DD MMMM YYYY MMM' | ||
const testFormat2 = 'MMMM' | ||
const testFormat3 = 'MMM' | ||
expect(dayjsBN.format(testFormat1)).toEqual(momentBN.format(testFormat1)) | ||
expect(dayjsBN.format(testFormat2)).toEqual(momentBN.format(testFormat2)) | ||
expect(dayjsBN.format(testFormat3)).toEqual(momentBN.format(testFormat3)) | ||
} | ||
}) | ||
|
||
it('Month short', () => { | ||
const date = '2021-02-01T05:54:32.005Z' | ||
const dayjsBN = dayjs(date) | ||
.locale('bn-bd') | ||
const momentBN = moment(date) | ||
.locale('bn-bd') | ||
const testFormat1 = 'DD MMMM YYYY MMM' | ||
expect(dayjsBN.format(testFormat1)).toEqual(momentBN.format(testFormat1)) | ||
}) | ||
|
||
it('Preparse with locale function', () => { | ||
for (let i = 0; i <= 7; i += 1) { | ||
dayjs.locale('bn-bd') | ||
const momentBN = moment() | ||
.locale('bn-bd') | ||
.add(i, 'day') | ||
expect(dayjs(momentBN.format()).format()).toEqual(momentBN.format()) | ||
} | ||
}) | ||
|
||
it('RelativeTime: Time from X', () => { | ||
const T = [ | ||
[44.4, 'second'], // a few seconds | ||
[89.5, 'second'], // a minute | ||
[130, 'second'], // two minutes | ||
[43, 'minute'], // 44 minutes | ||
[1, 'hour'], // 1 hour | ||
[21, 'hour'], // 21 hours | ||
[2, 'day'], // 2 days | ||
[25, 'day'], // 25 days | ||
[2, 'month'], // 2 months | ||
[10, 'month'], // 10 months | ||
[18, 'month'], // 2 years | ||
[15, 'year'] // 15 years | ||
] | ||
|
||
T.forEach((t) => { | ||
dayjs.locale('bn-bd') | ||
moment.locale('bn-bd') | ||
expect(dayjs().from(dayjs().add(t[0], t[1]))).toBe(moment().from(moment().add(t[0], t[1]))) | ||
expect(dayjs().from(dayjs().add(t[0], t[1]), true)) | ||
.toBe(moment().from(moment().add(t[0], t[1]), true)) | ||
}) | ||
}) |