2019-11-27 17:26:18 +01:00
|
|
|
import {
|
|
|
|
getNumberFormatter,
|
|
|
|
getDateFormatter,
|
|
|
|
getTimeFormatter,
|
|
|
|
getMessageFormatter,
|
2020-01-29 15:29:32 +01:00
|
|
|
init,
|
2020-11-05 14:01:23 +01:00
|
|
|
locale,
|
|
|
|
} from '../../../src/runtime';
|
2019-11-27 17:26:18 +01:00
|
|
|
|
2021-02-15 17:36:28 +01:00
|
|
|
const formatsJson = require('../../fixtures/formats.json');
|
|
|
|
|
2019-11-27 17:26:18 +01:00
|
|
|
beforeEach(() => {
|
2020-11-05 14:01:23 +01:00
|
|
|
init({ fallbackLocale: undefined });
|
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
|
|
|
describe('number formatter', () => {
|
2020-11-05 14:01:23 +01:00
|
|
|
const number = 123123;
|
2019-11-29 04:25:31 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('throws if no locale is set', () => {
|
2019-11-29 04:25:31 +01:00
|
|
|
expect(() => getNumberFormatter().format(number)).toThrow(
|
2020-11-05 14:01:23 +01:00
|
|
|
'[svelte-i18n] A "locale" must be set to format numbers',
|
|
|
|
);
|
|
|
|
});
|
2019-11-29 04:25:31 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('formats a number according to the current locale', () => {
|
|
|
|
init({ fallbackLocale: 'en' });
|
|
|
|
expect(getNumberFormatter().format(number)).toBe('123,123');
|
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('formats a number according to a locale', () => {
|
|
|
|
init({ fallbackLocale: 'en' });
|
2019-11-27 17:26:18 +01:00
|
|
|
expect(getNumberFormatter({ locale: 'pt-BR' }).format(number)).toBe(
|
2020-11-05 14:01:23 +01:00
|
|
|
'123.123',
|
|
|
|
);
|
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('formats a number with a custom format', () => {
|
2019-11-27 17:26:18 +01:00
|
|
|
init({
|
|
|
|
fallbackLocale: 'en',
|
2021-02-15 17:36:28 +01:00
|
|
|
formats: formatsJson,
|
2020-11-05 14:01:23 +01:00
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
|
|
|
expect(getNumberFormatter({ format: 'brl' }).format(number)).toBe(
|
2020-11-05 14:01:23 +01:00
|
|
|
'R$123,123.00',
|
|
|
|
);
|
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('formats a number with inline options', () => {
|
|
|
|
init({ fallbackLocale: 'en' });
|
2019-11-27 17:26:18 +01:00
|
|
|
|
|
|
|
expect(
|
2020-11-05 14:01:23 +01:00
|
|
|
getNumberFormatter({ style: 'currency', currency: 'BRL' }).format(number),
|
|
|
|
).toBe('R$123,123.00');
|
|
|
|
});
|
2020-01-29 15:29:32 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('formats a number according to the currently set locale', () => {
|
|
|
|
locale.set('en');
|
|
|
|
expect(getNumberFormatter().format(number)).toBe('123,123');
|
2020-01-29 15:29:32 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
locale.set('nl');
|
|
|
|
expect(getNumberFormatter().format(number)).toBe('123.123');
|
|
|
|
});
|
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
|
|
|
describe('date formatter', () => {
|
2020-11-05 14:01:23 +01:00
|
|
|
const date = new Date(2019, 1, 1);
|
2019-11-27 17:26:18 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('throws if no locale is set', () => {
|
2019-11-29 04:25:31 +01:00
|
|
|
expect(() => getDateFormatter().format(date)).toThrow(
|
2020-11-05 14:01:23 +01:00
|
|
|
'[svelte-i18n] A "locale" must be set to format dates',
|
|
|
|
);
|
|
|
|
});
|
2019-11-29 04:25:31 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('formats a date according to the current locale', () => {
|
|
|
|
init({ fallbackLocale: 'en' });
|
|
|
|
expect(getDateFormatter().format(date)).toBe('2/1/19');
|
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('formats a date according to a locale', () => {
|
|
|
|
expect(getDateFormatter({ locale: 'pt-BR' }).format(date)).toBe('01/02/19');
|
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('throws if passed a non-existing format', () => {
|
2019-11-27 17:26:18 +01:00
|
|
|
init({
|
|
|
|
fallbackLocale: 'en',
|
2021-02-15 17:36:28 +01:00
|
|
|
formats: formatsJson,
|
2020-11-05 14:01:23 +01:00
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
|
|
|
expect(() =>
|
2020-11-05 14:01:23 +01:00
|
|
|
getDateFormatter({ locale: 'pt-BR', format: 'foo' }).format(date),
|
|
|
|
).toThrowError(`[svelte-i18n] Unknown "foo" date format.`);
|
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('formats a date with a custom format', () => {
|
2019-11-27 17:26:18 +01:00
|
|
|
init({
|
|
|
|
fallbackLocale: 'en',
|
2021-02-15 17:36:28 +01:00
|
|
|
formats: formatsJson,
|
2020-11-05 14:01:23 +01:00
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
|
|
|
expect(getDateFormatter({ format: 'customDate' }).format(date)).toBe(
|
2020-11-05 14:01:23 +01:00
|
|
|
'2019 AD',
|
|
|
|
);
|
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('formats a date with inline options', () => {
|
|
|
|
init({ fallbackLocale: 'en' });
|
2019-11-27 17:26:18 +01:00
|
|
|
|
|
|
|
expect(
|
2020-11-05 14:01:23 +01:00
|
|
|
getDateFormatter({ year: 'numeric', era: 'short' }).format(date),
|
|
|
|
).toBe('2019 AD');
|
|
|
|
});
|
2020-01-29 15:29:32 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('formats a date according to the currently set locale', () => {
|
|
|
|
locale.set('en');
|
|
|
|
expect(getDateFormatter().format(date)).toBe('2/1/19');
|
2020-01-29 15:29:32 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
locale.set('nl');
|
|
|
|
expect(getDateFormatter().format(date)).toBe('1-2-19');
|
|
|
|
});
|
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
|
|
|
describe('time formatter', () => {
|
2020-11-05 14:01:23 +01:00
|
|
|
const time = new Date(2019, 1, 1, 20, 37, 32);
|
2019-11-27 17:26:18 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('throws if no locale is set', () => {
|
2019-11-29 04:25:31 +01:00
|
|
|
expect(() => getTimeFormatter().format(time)).toThrow(
|
2020-11-05 14:01:23 +01:00
|
|
|
'[svelte-i18n] A "locale" must be set to format time',
|
|
|
|
);
|
|
|
|
});
|
2019-11-29 04:25:31 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('formats a time according to the current locale', () => {
|
|
|
|
init({ fallbackLocale: 'en' });
|
|
|
|
expect(getTimeFormatter().format(time)).toBe('8:37 PM');
|
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('formats a time according to a locale', () => {
|
|
|
|
expect(getTimeFormatter({ locale: 'pt-BR' }).format(time)).toBe('20:37');
|
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('formats a time with a custom format', () => {
|
2019-11-27 17:26:18 +01:00
|
|
|
init({
|
|
|
|
fallbackLocale: 'en',
|
2021-02-15 17:36:28 +01:00
|
|
|
formats: formatsJson,
|
2020-11-05 14:01:23 +01:00
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
|
|
|
expect(getTimeFormatter({ format: 'customTime' }).format(time)).toBe(
|
2020-11-05 14:01:23 +01:00
|
|
|
'08:37:32 PM',
|
|
|
|
);
|
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('throws if passed a non-existing format', () => {
|
2019-11-27 17:26:18 +01:00
|
|
|
init({
|
|
|
|
fallbackLocale: 'en',
|
2021-02-15 17:36:28 +01:00
|
|
|
formats: formatsJson,
|
2020-11-05 14:01:23 +01:00
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
|
|
|
expect(() =>
|
2020-11-05 14:01:23 +01:00
|
|
|
getTimeFormatter({ locale: 'pt-BR', format: 'foo' }).format(time),
|
|
|
|
).toThrowError(`[svelte-i18n] Unknown "foo" time format.`);
|
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('formats a time with inline options', () => {
|
|
|
|
init({ fallbackLocale: 'en' });
|
2019-11-27 17:26:18 +01:00
|
|
|
|
|
|
|
expect(
|
|
|
|
getTimeFormatter({
|
|
|
|
hour: '2-digit',
|
|
|
|
minute: '2-digit',
|
|
|
|
second: '2-digit',
|
2020-11-05 14:01:23 +01:00
|
|
|
}).format(time),
|
|
|
|
).toBe('08:37:32 PM');
|
|
|
|
});
|
2020-01-29 15:29:32 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('formats time according to the currently set locale', () => {
|
|
|
|
locale.set('en');
|
|
|
|
expect(getTimeFormatter().format(time)).toBe('8:37 PM');
|
2020-01-29 15:29:32 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
locale.set('nl');
|
|
|
|
expect(getTimeFormatter().format(time)).toBe('20:37');
|
|
|
|
});
|
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
|
|
|
describe('message formatter', () => {
|
2020-11-05 14:01:23 +01:00
|
|
|
it('formats a message with interpolated values', () => {
|
2019-11-27 17:26:18 +01:00
|
|
|
expect(
|
|
|
|
getMessageFormatter('Page: {current,number}/{max,number}', 'en').format({
|
|
|
|
current: 2,
|
|
|
|
max: 10,
|
2020-11-05 14:01:23 +01:00
|
|
|
}),
|
|
|
|
).toBe('Page: 2/10');
|
|
|
|
});
|
2019-11-27 17:26:18 +01:00
|
|
|
|
2020-11-05 14:01:23 +01:00
|
|
|
it('formats number with custom formats', () => {
|
2021-02-15 17:36:28 +01:00
|
|
|
locale.set('en');
|
2019-11-27 17:26:18 +01:00
|
|
|
expect(
|
|
|
|
getMessageFormatter('Number: {n, number, compactShort}', 'en').format({
|
|
|
|
n: 2000000,
|
2020-11-05 14:01:23 +01:00
|
|
|
}),
|
|
|
|
).toBe('Number: 2M');
|
|
|
|
});
|
2021-02-11 01:42:23 +01:00
|
|
|
|
|
|
|
it('formats an html message with interpolated values', () => {
|
|
|
|
expect(
|
|
|
|
getMessageFormatter(
|
|
|
|
'Page: <soan class="text-bold">{current,number}/{max,number}</soan>',
|
|
|
|
'en',
|
|
|
|
).format({
|
|
|
|
current: 2,
|
|
|
|
max: 10,
|
|
|
|
}),
|
|
|
|
).toBe('Page: <soan class="text-bold">2/10</soan>');
|
|
|
|
});
|
2020-11-05 14:01:23 +01:00
|
|
|
});
|