import { mergeProps } from '../util/object' ; import { globalLocales } from '../global-locales' ; import { CalendarOptions , CalendarOptionsRefined } from '../options' ; export type LocaleCodeArg = string | string [ ] ; export type LocaleSingularArg = LocaleCodeArg | LocaleInput ; export interface Locale { codeArg : LocaleCodeArg ; codes : string [ ] ; week : { dow : number ; doy : number ; } ; simpleNumberFormat : Intl . NumberFormat ; options : CalendarOptionsRefined ; } export interface LocaleInput extends CalendarOptions { code : string ; } export type LocaleInputMap = { [ code ] : LocaleInput ; } ; export interface RawLocaleInfo { map : LocaleInputMap ; defaultCode : string ; } const MINIMAL_RAW_EN_LOCALE = { code : 'en' , week : { dow : 0 , doy : 4 , } , direction : 'ltr' as ( 'ltr' | 'rtl' ) , buttonText : { prev : 'prev' , next : 'next' , prevYear : 'prev year' , nextYear : 'next year' , year : 'year' , today : 'today' , month : 'month' , week : 'week' , day : 'day' , list : 'list' , } , weekText : 'W' , weekTextLong : 'Week' , closeHint : 'Close' , timeHint : 'Time' , eventHint : 'Event' , allDayText : 'all-day' , moreLinkText : 'more' , noEventsText : 'No events to display' , } ; const RAW_EN_LOCALE = { ... MINIMAL_RAW_EN_LOCALE , buttonHints : { prev : 'Previous $0' , next : 'Next $0' , today ( buttonText , unit ) { return ( unit === 'day' ) ? 'Today' : `This ${ buttonText }` ; } , } , viewHint : '$0 view' , navLinkHint : 'Go to $0' , moreLinkHint ( eventCnt ) { return `Show ${ eventCnt } more event${ eventCnt === 1 ? '' : 's' }` ; } , } ; export function organizeRawLocales ( explicitRawLocales ) { let defaultCode = explicitRawLocales . length > 0 ? explicitRawLocales [ 0 ] . code : 'en' ; let allRawLocales = globalLocales . concat ( explicitRawLocales ) ; let rawLocaleMap = { en : RAW_EN_LOCALE , } ; for ( let rawLocale of allRawLocales ) { rawLocaleMap [ rawLocale . code ] = rawLocale ; } return { map : rawLocaleMap , defaultCode , } ; } export function buildLocale ( inputSingular , available ) { if ( typeof inputSingular === 'object' && ! Array . isArray ( inputSingular ) ) { return parseLocale ( inputSingular . code , [ inputSingular . code ] , inputSingular ) ; } return queryLocale ( inputSingular , available ) ; } function queryLocale ( codeArg , available ) { let codes = [ ] . concat ( codeArg || [ ] ) ; let raw = queryRawLocale ( codes , available ) || RAW_EN_LOCALE ; return parseLocale ( codeArg , codes , raw ) ; } function queryRawLocale ( codes , available ) { for ( let i = 0 ; i < codes . length ; i += 1 ) { let parts = codes [ i ] . toLocaleLowerCase ( ) . split ( '-' ) ; for ( let j = parts . length ; j > 0 ; j -= 1 ) { let simpleId = parts . slice ( 0 , j ) . join ( '-' ) ; if ( available [ simpleId ] ) { return available [ simpleId ] ; } } } return null ; } function parseLocale ( codeArg , codes , raw ) { let merged = mergeProps ( [ MINIMAL_RAW_EN_LOCALE , raw ] , [ 'buttonText' ] ) ; delete merged . code ; let { week } = merged ; delete merged . week ; return { codeArg , codes , week , simpleNumberFormat : new Intl . NumberFormat ( codeArg ) , options : merged , } ; }