2022-04-05 16:38:45 +02:00
|
|
|
import type {
|
|
|
|
ConfigureOptions,
|
|
|
|
ConfigureOptionsInit,
|
|
|
|
MissingKeyHandlerInput,
|
|
|
|
} from './types';
|
|
|
|
import { $locale, getCurrentLocale, getPossibleLocales } from './stores/locale';
|
|
|
|
import { hasLocaleQueue } from './includes/loaderQueue';
|
2019-11-26 03:04:10 +01:00
|
|
|
|
2019-11-26 17:05:17 +01:00
|
|
|
interface Formats {
|
2020-09-20 15:12:18 +02:00
|
|
|
number: Record<string, any>;
|
|
|
|
date: Record<string, any>;
|
|
|
|
time: Record<string, any>;
|
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',
|
|
|
|
},
|
|
|
|
},
|
2020-09-20 15:14:26 +02:00
|
|
|
};
|
2019-11-26 03:04:10 +01:00
|
|
|
|
2022-04-05 16:38:45 +02:00
|
|
|
/**
|
|
|
|
* Default missing key handler used in case "warnOnMissingMessages" is set to true.
|
|
|
|
*/
|
|
|
|
function defaultMissingKeyHandler({ locale, id }: MissingKeyHandlerInput) {
|
|
|
|
// istanbul ignore next
|
|
|
|
console.warn(
|
|
|
|
`[svelte-i18n] The message "${id}" was not found in "${getPossibleLocales(
|
|
|
|
locale,
|
|
|
|
).join('", "')}".${
|
|
|
|
hasLocaleQueue(getCurrentLocale())
|
|
|
|
? `\n\nNote: there are at least one loader still registered to this locale that wasn't executed.`
|
|
|
|
: ''
|
|
|
|
}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-24 14:08:37 +02:00
|
|
|
export const defaultOptions: ConfigureOptions = {
|
|
|
|
fallbackLocale: null as any,
|
2019-11-26 17:05:17 +01:00
|
|
|
loadingDelay: 200,
|
|
|
|
formats: defaultFormats,
|
2019-11-28 18:59:52 +01:00
|
|
|
warnOnMissingMessages: true,
|
2022-04-05 16:38:45 +02:00
|
|
|
handleMissingMessage: undefined,
|
2021-02-11 01:42:23 +01:00
|
|
|
ignoreTag: true,
|
2020-09-20 15:14:26 +02:00
|
|
|
};
|
2019-11-26 03:04:10 +01:00
|
|
|
|
2021-08-23 16:46:46 +02:00
|
|
|
const options: ConfigureOptions = defaultOptions as any;
|
2019-11-26 03:04:10 +01:00
|
|
|
|
2019-11-26 17:05:17 +01:00
|
|
|
export function getOptions() {
|
2020-09-20 15:14:26 +02:00
|
|
|
return options;
|
2019-11-26 03:04:10 +01:00
|
|
|
}
|
|
|
|
|
2021-08-23 16:46:46 +02:00
|
|
|
export function init(opts: ConfigureOptionsInit) {
|
2020-09-20 15:14:26 +02:00
|
|
|
const { formats, ...rest } = opts;
|
2021-09-30 16:06:23 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
2020-09-20 15:14:26 +02:00
|
|
|
const initialLocale = opts.initialLocale || opts.fallbackLocale;
|
2019-11-26 17:05:17 +01:00
|
|
|
|
2022-04-05 16:38:45 +02:00
|
|
|
if (rest.warnOnMissingMessages) {
|
|
|
|
delete rest.warnOnMissingMessages;
|
|
|
|
|
|
|
|
if (rest.handleMissingMessage == null) {
|
|
|
|
rest.handleMissingMessage = defaultMissingKeyHandler;
|
|
|
|
} else {
|
|
|
|
console.warn(
|
|
|
|
'[svelte-i18n] The "warnOnMissingMessages" option is deprecated. Please use the "handleMissingMessage" option instead.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 15:14:26 +02: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) {
|
2020-09-20 15:14:26 +02:00
|
|
|
Object.assign(options.formats.number, formats.number);
|
2019-11-26 17:05:17 +01:00
|
|
|
}
|
2020-09-20 15:14:26 +02:00
|
|
|
|
2019-11-28 18:59:52 +01:00
|
|
|
if ('date' in formats) {
|
2020-09-20 15:14:26 +02:00
|
|
|
Object.assign(options.formats.date, formats.date);
|
2019-11-26 17:05:17 +01:00
|
|
|
}
|
2020-09-20 15:14:26 +02:00
|
|
|
|
2019-11-28 18:59:52 +01:00
|
|
|
if ('time' in formats) {
|
2020-09-20 15:14:26 +02:00
|
|
|
Object.assign(options.formats.time, formats.time);
|
2019-11-26 17:05:17 +01:00
|
|
|
}
|
2019-11-26 03:04:10 +01:00
|
|
|
}
|
|
|
|
|
2020-09-20 15:14:26 +02:00
|
|
|
return $locale.set(initialLocale);
|
2019-11-26 03:04:10 +01:00
|
|
|
}
|