diff --git a/src/hooks/useDatePicker.spec.tsx b/src/hooks/useDatePicker.spec.tsx index 181f722..5ef7eb3 100644 --- a/src/hooks/useDatePicker.spec.tsx +++ b/src/hooks/useDatePicker.spec.tsx @@ -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', () => { diff --git a/src/utils/localisationHelpers.ts b/src/utils/localisationHelpers.ts index 09ed943..a600336 100644 --- a/src/utils/localisationHelpers.ts +++ b/src/utils/localisationHelpers.ts @@ -8,6 +8,21 @@ import { import { PickerOptions, SupportedCalendar } from '../types' import { formatYyyyMmDD, isCustomCalendar } from './helpers' +const getPartialLocaleMatch: ( + locales: Record, + 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 @@ -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) {