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