2020-09-20 15:14:26 +02:00
|
|
|
import { derived } from 'svelte/store';
|
2020-01-16 01:47:43 +01:00
|
|
|
|
|
|
|
import {
|
|
|
|
MessageFormatter,
|
|
|
|
MessageObject,
|
|
|
|
TimeFormatter,
|
|
|
|
DateFormatter,
|
|
|
|
NumberFormatter,
|
2020-09-20 15:14:26 +02:00
|
|
|
} from '../types';
|
|
|
|
import { lookup } from '../includes/lookup';
|
|
|
|
import { hasLocaleQueue } from '../includes/loaderQueue';
|
2020-01-16 01:47:43 +01:00
|
|
|
import {
|
|
|
|
getMessageFormatter,
|
|
|
|
getTimeFormatter,
|
|
|
|
getDateFormatter,
|
|
|
|
getNumberFormatter,
|
2020-09-20 15:14:26 +02:00
|
|
|
} from '../includes/formatters';
|
|
|
|
import { getOptions } from '../configs';
|
|
|
|
import { $dictionary } from './dictionary';
|
|
|
|
import { getCurrentLocale, getRelatedLocalesOf, $locale } from './locale';
|
2020-01-16 01:47:43 +01:00
|
|
|
|
|
|
|
const formatMessage: MessageFormatter = (id, options = {}) => {
|
|
|
|
if (typeof id === 'object') {
|
2020-09-20 15:14:26 +02:00
|
|
|
options = id as MessageObject;
|
|
|
|
id = options.id;
|
2020-01-16 01:47:43 +01:00
|
|
|
}
|
|
|
|
|
2020-09-20 15:14:26 +02:00
|
|
|
const {
|
|
|
|
values,
|
|
|
|
locale = getCurrentLocale(),
|
|
|
|
default: defaultValue,
|
|
|
|
} = options;
|
2020-01-16 01:47:43 +01:00
|
|
|
|
|
|
|
if (locale == null) {
|
|
|
|
throw new Error(
|
2020-09-20 15:14:26 +02:00
|
|
|
'[svelte-i18n] Cannot format a message without first setting the initial locale.',
|
|
|
|
);
|
2020-01-16 01:47:43 +01:00
|
|
|
}
|
|
|
|
|
2020-09-20 15:14:26 +02:00
|
|
|
const message = lookup(id, locale);
|
2020-01-16 01:47:43 +01:00
|
|
|
|
|
|
|
if (!message) {
|
|
|
|
if (getOptions().warnOnMissingMessages) {
|
|
|
|
// istanbul ignore next
|
|
|
|
console.warn(
|
|
|
|
`[svelte-i18n] The message "${id}" was not found in "${getRelatedLocalesOf(
|
2020-09-20 15:14:26 +02:00
|
|
|
locale,
|
2020-01-16 01:47:43 +01:00
|
|
|
).join('", "')}".${
|
|
|
|
hasLocaleQueue(getCurrentLocale())
|
|
|
|
? `\n\nNote: there are at least one loader still registered to this locale that wasn't executed.`
|
|
|
|
: ''
|
2020-09-20 15:14:26 +02:00
|
|
|
}`,
|
|
|
|
);
|
2020-01-16 01:47:43 +01:00
|
|
|
}
|
|
|
|
|
2020-09-20 15:14:26 +02:00
|
|
|
return defaultValue || id;
|
2020-01-16 01:47:43 +01:00
|
|
|
}
|
|
|
|
|
2020-09-20 15:14:26 +02:00
|
|
|
if (!values) return message;
|
|
|
|
|
|
|
|
return getMessageFormatter(message, locale).format(values);
|
|
|
|
};
|
2020-01-16 01:47:43 +01:00
|
|
|
|
|
|
|
const formatTime: TimeFormatter = (t, options) =>
|
2020-09-20 15:14:26 +02:00
|
|
|
getTimeFormatter(options).format(t);
|
2020-01-16 01:47:43 +01:00
|
|
|
|
|
|
|
const formatDate: DateFormatter = (d, options) =>
|
2020-09-20 15:14:26 +02:00
|
|
|
getDateFormatter(options).format(d);
|
2020-01-16 01:47:43 +01:00
|
|
|
|
|
|
|
const formatNumber: NumberFormatter = (n, options) =>
|
2020-09-20 15:14:26 +02:00
|
|
|
getNumberFormatter(options).format(n);
|
2020-01-16 01:47:43 +01:00
|
|
|
|
2020-09-20 15:14:26 +02:00
|
|
|
export const $format = derived([$locale, $dictionary], () => formatMessage);
|
|
|
|
export const $formatTime = derived([$locale], () => formatTime);
|
|
|
|
export const $formatDate = derived([$locale], () => formatDate);
|
|
|
|
export const $formatNumber = derived([$locale], () => formatNumber);
|