Skip to content

Commit

Permalink
fix: wrong ku locale characters and formatting
Browse files Browse the repository at this point in the history
- fix usage of non-standard Kurdish characters in ku locale iamkun#1728
- add preparse and postformat to ku locale so that the formatted
  dates will contain correct Arabic numbers that Kurdish language
  uses.
  • Loading branch information
akamfoad committed Apr 9, 2022
1 parent dc1b969 commit 1fec487
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 15 deletions.
55 changes: 42 additions & 13 deletions src/locale/ku.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
// Kurdish [ku]
import dayjs from 'dayjs'
import {
englishToArabicNumbersMap,
arabicToEnglishNumbersMap
} from '../constant'

const months = [
'کانونی دووەم',
'شوبات',
'ئازار',
'نیسان',
'ئایار',
'حوزەیران',
'تەمموز',
'ئاب',
'ئەیلوول',
'تشرینی یەکەم',
'تشرینی دووەم',
'کانونی یەکەم'
]

const locale = {
name: 'ku',
weekdays: 'یه‌كشه‌ممه‌_دووشه‌ممه‌_سێشه‌ممه‌_چوارشه‌ممه‌_پێنجشه‌ممه‌_هه‌ینی_شه‌ممه‌'.split('_'),
months: 'کانونی دووەم_شوبات_ئازار_نیسان_ئایار_حوزەیران_تەمموز_ئاب_ئەیلوول_تشرینی یەكەم_تشرینی دووەم_كانونی یەکەم'.split('_'),
months,
monthsShort: months,
weekdays: 'یەکشەممە_دووشەممە_سێشەممە_چوارشەممە_پێنجشەممە_هەینی_شەممە'.split('_'),
weekdaysShort: 'یەکشەم_دووشەم_سێشەم_چوارشەم_پێنجشەم_هەینی_شەممە'.split('_'),
weekStart: 6,
weekdaysShort: 'یه‌كشه‌م_دووشه‌م_سێشه‌م_چوارشه‌م_پێنجشه‌م_هه‌ینی_شه‌ممه‌'.split('_'),
monthsShort: 'کانونی دووەم_شوبات_ئازار_نیسان_ئایار_حوزەیران_تەمموز_ئاب_ئەیلوول_تشرینی یەكەم_تشرینی دووەم_كانونی یەکەم'.split('_'),
weekdaysMin: 'ی_د_س_چ_پ_ه_ش'.split('_'),
preparse(string) {
return string
.replace(/[١٢٣٤٥٦٧٨٩٠]/g, match => arabicToEnglishNumbersMap[match])
.replace(/،/g, ',')
},
postformat(string) {
return string
.replace(/\d/g, match => englishToArabicNumbersMap[match])
.replace(/,/g, '،')
},
ordinal: n => n,
formats: {
LT: 'HH:mm',
Expand All @@ -20,18 +49,18 @@ const locale = {
},
meridiem: hour => (hour < 12 ? 'پ.ن' : 'د.ن'),
relativeTime: {
future: 'له‌ %s',
future: 'لە %s',
past: '%s',
s: 'چه‌ند چركه‌یه‌ك',
m: 'یه‌ك خوله‌ك',
mm: '%d خوله‌ك',
h: 'یه‌ك كاتژمێر',
hh: '%d كاتژمێر',
d: 'یه‌ك ڕۆژ',
s: 'چەند چرکەیەک',
m: 'یەک خولەک',
mm: '%d خولەک',
h: 'یەک کاتژمێر',
hh: '%d کاتژمێر',
d: 'یەک ڕۆژ',
dd: '%d ڕۆژ',
M: 'یه‌ك مانگ',
M: 'یەک مانگ',
MM: '%d مانگ',
y: 'یه‌ك ساڵ',
y: 'یەک ساڵ',
yy: '%d ساڵ'
}
}
Expand Down
23 changes: 21 additions & 2 deletions test/locale/ku.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../../src'
import '../../src/locale/ku'
import locale from '../../src/locale/ku'
import preParsePostFormat from '../../src/plugin/preParsePostFormat'
import { englishToArabicNumbersMap } from '../../src/constant'

dayjs.extend(preParsePostFormat)

beforeEach(() => {
MockDate.set(new Date())
Expand All @@ -15,6 +20,20 @@ it('Format meridiem correctly', () => {
const dayjsKu = dayjs()
.startOf('day')
.add(i, 'hour')
expect(dayjsKu.locale('ku').format('h A')).toBe(`${i % 12 || 12} ${i < 12 ? 'پ.ن' : 'د.ن'}`)
const hour = (i % 12 || 12)
.toString()
.replace(/\d/g, match => englishToArabicNumbersMap[match])
const m = i < 12 ? 'پ.ن' : 'د.ن'
expect(dayjsKu.locale('ku').format('h A')).toBe(`${hour} ${m}`)
}
})

it('Preparse with locale function', () => {
for (let i = 0; i <= 7; i += 1) {
dayjs.locale(locale)
const momentKu = moment()
.locale('ku')
.add(i, 'day')
expect(dayjs(momentKu.format()).format()).toEqual(momentKu.format())
}
})

0 comments on commit 1fec487

Please sign in to comment.