Skip to content

Commit

Permalink
fix(LIBS-629): default 'ne' locale to Nepali script
Browse files Browse the repository at this point in the history
  • Loading branch information
kabaros committed Jun 25, 2024
1 parent fe0cd68 commit 97e601d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/hooks/useDatePicker.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,29 @@ describe('custom calendars', () => {

expect(result.currMonth.label).toEqual('Ashadh')
})
it('should default to Nepali if NE is passed as it is the case for DHIS2', () => {
const onDateSelect = jest.fn()
const date = '2079-03-32'
const options = {
locale: 'ne', // non-supported locale
calendar: 'nepali' as const,
timeZone: 'Africa/Khartoum',
}
const renderedHook = renderHook(() =>
useDatePicker({ onDateSelect, date, options })
)
const result = renderedHook?.result.current as UseDatePickerReturn

expect(result.weekDayLabels).toEqual([
'सोम',
'मंगल',
'बुध',
'बिही',
'शुक्र',
'शनि',
'आइत',
])
})
})
describe('rendering Nepali (custom) day names', () => {
it('should render Nepali with ne-NP passed', () => {
Expand Down
17 changes: 17 additions & 0 deletions src/utils/localisationHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ import {
import { PickerOptions, SupportedCalendar } from '../types'
import { formatYyyyMmDD, isCustomCalendar } from './helpers'

const getPartialLocaleMatch: (
locales: Record<string, CalendarCustomLocale>,
locale: string | undefined
) => CalendarCustomLocale | undefined = (availableLocales, locale) => {
// try to see if there is a language match (even if the region doesn't match)
const partialLocaleMatch = Object.keys(availableLocales).find(
(supportedLocale) =>
supportedLocale.split('-')?.[0]?.toLowerCase() === locale
)

if (partialLocaleMatch) {
return availableLocales[partialLocaleMatch]
}
}

const getCustomCalendarLocale = (
calendar: Temporal.CalendarLike,
locale: string | undefined
Expand All @@ -17,9 +32,11 @@ const getCustomCalendarLocale = (
if (!customCalendar) {
return undefined
}

const customLocalisations = customCalendar.locales || {}
const result =
(locale && customLocalisations?.[locale]) ??
getPartialLocaleMatch(customLocalisations, locale) ??
customLocalisations?.[customCalendar.defaultLocale]

if (!result) {
Expand Down

0 comments on commit 97e601d

Please sign in to comment.