svelte-i18n/src/runtime/configs.ts

78 lines
2.1 KiB
TypeScript
Raw Normal View History

import type { ConfigureOptions, ConfigureOptionsInit } from './types';
import { $locale } from './stores/locale';
interface Formats {
2020-09-20 15:12:18 +02:00
number: Record<string, any>;
date: Record<string, any>;
time: Record<string, any>;
}
export const defaultFormats: Formats = {
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',
},
},
};
2021-08-24 14:08:37 +02:00
export const defaultOptions: ConfigureOptions = {
fallbackLocale: null as any,
loadingDelay: 200,
formats: defaultFormats,
2019-11-28 18:59:52 +01:00
warnOnMissingMessages: true,
ignoreTag: true,
};
const options: ConfigureOptions = defaultOptions as any;
export function getOptions() {
return options;
}
export function init(opts: ConfigureOptionsInit) {
const { formats, ...rest } = opts;
2021-09-30 16:06:23 +02:00
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const initialLocale = opts.initialLocale || opts.fallbackLocale;
Object.assign(options, rest, { initialLocale });
2019-11-28 18:59:52 +01:00
if (formats) {
if ('number' in formats) {
Object.assign(options.formats.number, formats.number);
}
2019-11-28 18:59:52 +01:00
if ('date' in formats) {
Object.assign(options.formats.date, formats.date);
}
2019-11-28 18:59:52 +01:00
if ('time' in formats) {
Object.assign(options.formats.time, formats.time);
}
}
return $locale.set(initialLocale);
}