mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-11-16 18:10:43 +01:00
fix: memoizing of formatters when no locale is given. (#47)
This commit is contained in:
parent
88f7762e96
commit
27871f9775
@ -18,11 +18,10 @@ const getIntlFormatterOptions = (
|
||||
throw new Error(`[svelte-i18n] Unknown "${name}" ${type} format.`)
|
||||
}
|
||||
|
||||
export const getNumberFormatter: MemoizedIntlFormatter<
|
||||
const createNumberFormatter: MemoizedIntlFormatter<
|
||||
Intl.NumberFormat,
|
||||
Intl.NumberFormatOptions
|
||||
> = monadicMemoize(({ locale, format, ...options } = {}) => {
|
||||
locale = locale || getCurrentLocale()
|
||||
> = monadicMemoize(({ locale, format, ...options }) => {
|
||||
if (locale == null) {
|
||||
throw new Error('[svelte-i18n] A "locale" must be set to format numbers')
|
||||
}
|
||||
@ -33,12 +32,12 @@ export const getNumberFormatter: MemoizedIntlFormatter<
|
||||
|
||||
return new Intl.NumberFormat(locale, options)
|
||||
})
|
||||
export const getNumberFormatter = ({ locale = getCurrentLocale(), ...args } = {}) => createNumberFormatter({ locale, ...args })
|
||||
|
||||
export const getDateFormatter: MemoizedIntlFormatter<
|
||||
const createDateFormatter: MemoizedIntlFormatter<
|
||||
Intl.DateTimeFormat,
|
||||
Intl.DateTimeFormatOptions
|
||||
> = monadicMemoize(({ locale, format, ...options } = {}) => {
|
||||
locale = locale || getCurrentLocale()
|
||||
> = monadicMemoize(({ locale, format, ...options }) => {
|
||||
if (locale == null) {
|
||||
throw new Error('[svelte-i18n] A "locale" must be set to format dates')
|
||||
}
|
||||
@ -50,12 +49,12 @@ export const getDateFormatter: MemoizedIntlFormatter<
|
||||
|
||||
return new Intl.DateTimeFormat(locale, options)
|
||||
})
|
||||
export const getDateFormatter = ({ locale = getCurrentLocale(), ...args } = {}) => createDateFormatter({ locale, ...args })
|
||||
|
||||
export const getTimeFormatter: MemoizedIntlFormatter<
|
||||
const createTimeFormatter: MemoizedIntlFormatter<
|
||||
Intl.DateTimeFormat,
|
||||
Intl.DateTimeFormatOptions
|
||||
> = monadicMemoize(({ locale, format, ...options } = {}) => {
|
||||
locale = locale || getCurrentLocale()
|
||||
> = monadicMemoize(({ locale, format, ...options }) => {
|
||||
if (locale == null) {
|
||||
throw new Error(
|
||||
'[svelte-i18n] A "locale" must be set to format time values'
|
||||
@ -69,6 +68,7 @@ export const getTimeFormatter: MemoizedIntlFormatter<
|
||||
|
||||
return new Intl.DateTimeFormat(locale, options)
|
||||
})
|
||||
export const getTimeFormatter = ({ locale = getCurrentLocale(), ...args } = {}) => createTimeFormatter({ locale, ...args })
|
||||
|
||||
export const getMessageFormatter = monadicMemoize(
|
||||
(message: string, locale: string = getCurrentLocale()) =>
|
||||
|
@ -3,7 +3,8 @@ import {
|
||||
getDateFormatter,
|
||||
getTimeFormatter,
|
||||
getMessageFormatter,
|
||||
init
|
||||
init,
|
||||
locale
|
||||
} from '../../../src/runtime'
|
||||
|
||||
beforeEach(() => {
|
||||
@ -19,7 +20,7 @@ describe('number formatter', () => {
|
||||
)
|
||||
})
|
||||
|
||||
test('formats a date according to the current locale', () => {
|
||||
test('formats a number according to the current locale', () => {
|
||||
init({ fallbackLocale: 'en' })
|
||||
expect(getNumberFormatter().format(number)).toBe('123,123')
|
||||
})
|
||||
@ -49,6 +50,14 @@ describe('number formatter', () => {
|
||||
getNumberFormatter({ style: 'currency', currency: 'BRL' }).format(number)
|
||||
).toBe('R$123,123.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')
|
||||
})
|
||||
})
|
||||
|
||||
describe('date formatter', () => {
|
||||
@ -98,6 +107,14 @@ describe('date formatter', () => {
|
||||
getDateFormatter({ year: 'numeric', era: 'short' }).format(date)
|
||||
).toBe('2019 AD')
|
||||
})
|
||||
|
||||
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')
|
||||
})
|
||||
})
|
||||
|
||||
describe('time formatter', () => {
|
||||
@ -151,6 +168,14 @@ describe('time formatter', () => {
|
||||
}).format(time)
|
||||
).toBe('08:37:32 PM')
|
||||
})
|
||||
|
||||
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')
|
||||
})
|
||||
})
|
||||
|
||||
describe('message formatter', () => {
|
||||
|
Loading…
Reference in New Issue
Block a user