2019-11-26 03:04:10 +01:00
|
|
|
import { getClientLocale } from './includes/utils'
|
|
|
|
import { ConfigureOptions } from './types'
|
|
|
|
import { $locale } from './stores/locale'
|
|
|
|
|
2019-11-26 17:05:17 +01:00
|
|
|
interface Formats {
|
|
|
|
number: Record<string, any>
|
|
|
|
date: Record<string, any>
|
|
|
|
time: Record<string, any>
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Options {
|
|
|
|
fallbackLocale: string
|
|
|
|
initialLocale: string
|
|
|
|
formats: Formats
|
|
|
|
loadingDelay: number
|
2019-11-28 18:59:52 +01:00
|
|
|
warnOnMissingMessages: boolean
|
2019-11-26 17:05:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export const defaultFormats: Formats = {
|
2019-11-26 03:04:10 +01:00
|
|
|
number: {
|
|
|
|
scientific: { notation: 'scientific' },
|
|
|
|
engineering: { notation: 'engineering' },
|
|
|
|
compactLong: { notation: 'compact', compactDisplay: 'long' },
|
|
|
|
compactShort: { notation: 'compact', compactDisplay: 'short' },
|
|
|
|
},
|
|
|
|
date: {
|
|
|
|
short: { month: 'numeric', day: 'numeric', year: '2-digit' },
|
|
|
|
medium: { month: 'short', day: 'numeric', year: 'numeric' },
|
|
|
|
long: { month: 'long', day: 'numeric', year: 'numeric' },
|
|
|
|
full: { weekday: 'long', month: 'long', day: 'numeric', year: 'numeric' },
|
|
|
|
},
|
|
|
|
time: {
|
|
|
|
short: { hour: 'numeric', minute: 'numeric' },
|
|
|
|
medium: { hour: 'numeric', minute: 'numeric', second: 'numeric' },
|
|
|
|
long: {
|
|
|
|
hour: 'numeric',
|
|
|
|
minute: 'numeric',
|
|
|
|
second: 'numeric',
|
|
|
|
timeZoneName: 'short',
|
|
|
|
},
|
|
|
|
full: {
|
|
|
|
hour: 'numeric',
|
|
|
|
minute: 'numeric',
|
|
|
|
second: 'numeric',
|
|
|
|
timeZoneName: 'short',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-11-26 17:05:17 +01:00
|
|
|
export const defaultOptions: Options = {
|
|
|
|
fallbackLocale: null,
|
|
|
|
initialLocale: null,
|
|
|
|
loadingDelay: 200,
|
|
|
|
formats: defaultFormats,
|
2019-11-28 18:59:52 +01:00
|
|
|
warnOnMissingMessages: true,
|
2019-11-26 03:04:10 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 17:05:17 +01:00
|
|
|
const options: Options = defaultOptions
|
2019-11-26 03:04:10 +01:00
|
|
|
|
2019-11-26 17:05:17 +01:00
|
|
|
export function getOptions() {
|
|
|
|
return options
|
2019-11-26 03:04:10 +01:00
|
|
|
}
|
|
|
|
|
2019-11-27 04:58:53 +01:00
|
|
|
export function init(opts: ConfigureOptions) {
|
2019-11-28 18:59:52 +01:00
|
|
|
const { formats, ...rest } = opts
|
2019-11-26 17:05:17 +01:00
|
|
|
const initialLocale = opts.initialLocale
|
|
|
|
? typeof opts.initialLocale === 'string'
|
|
|
|
? opts.initialLocale
|
2019-11-28 18:59:52 +01:00
|
|
|
: getClientLocale(opts.initialLocale) || opts.fallbackLocale
|
|
|
|
: opts.fallbackLocale
|
2019-11-26 17:05:17 +01:00
|
|
|
|
2019-11-28 18:59:52 +01:00
|
|
|
Object.assign(options, rest, { initialLocale })
|
2019-11-26 03:04:10 +01:00
|
|
|
|
2019-11-28 18:59:52 +01:00
|
|
|
if (formats) {
|
|
|
|
if ('number' in formats) {
|
|
|
|
Object.assign(options.formats.number, formats.number)
|
2019-11-26 17:05:17 +01:00
|
|
|
}
|
2019-11-28 18:59:52 +01:00
|
|
|
if ('date' in formats) {
|
|
|
|
Object.assign(options.formats.date, formats.date)
|
2019-11-26 17:05:17 +01:00
|
|
|
}
|
2019-11-28 18:59:52 +01:00
|
|
|
if ('time' in formats) {
|
|
|
|
Object.assign(options.formats.time, formats.time)
|
2019-11-26 17:05:17 +01:00
|
|
|
}
|
2019-11-26 03:04:10 +01:00
|
|
|
}
|
|
|
|
|
2019-11-28 18:59:52 +01:00
|
|
|
return $locale.set(initialLocale)
|
2019-11-26 03:04:10 +01:00
|
|
|
}
|