mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-06-30 18:54:49 +02: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.`)
|
throw new Error(`[svelte-i18n] Unknown "${name}" ${type} format.`)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getNumberFormatter: MemoizedIntlFormatter<
|
const createNumberFormatter: MemoizedIntlFormatter<
|
||||||
Intl.NumberFormat,
|
Intl.NumberFormat,
|
||||||
Intl.NumberFormatOptions
|
Intl.NumberFormatOptions
|
||||||
> = monadicMemoize(({ locale, format, ...options } = {}) => {
|
> = monadicMemoize(({ locale, format, ...options }) => {
|
||||||
locale = locale || getCurrentLocale()
|
|
||||||
if (locale == null) {
|
if (locale == null) {
|
||||||
throw new Error('[svelte-i18n] A "locale" must be set to format numbers')
|
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)
|
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.DateTimeFormat,
|
||||||
Intl.DateTimeFormatOptions
|
Intl.DateTimeFormatOptions
|
||||||
> = monadicMemoize(({ locale, format, ...options } = {}) => {
|
> = monadicMemoize(({ locale, format, ...options }) => {
|
||||||
locale = locale || getCurrentLocale()
|
|
||||||
if (locale == null) {
|
if (locale == null) {
|
||||||
throw new Error('[svelte-i18n] A "locale" must be set to format dates')
|
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)
|
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.DateTimeFormat,
|
||||||
Intl.DateTimeFormatOptions
|
Intl.DateTimeFormatOptions
|
||||||
> = monadicMemoize(({ locale, format, ...options } = {}) => {
|
> = monadicMemoize(({ locale, format, ...options }) => {
|
||||||
locale = locale || getCurrentLocale()
|
|
||||||
if (locale == null) {
|
if (locale == null) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'[svelte-i18n] A "locale" must be set to format time values'
|
'[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)
|
return new Intl.DateTimeFormat(locale, options)
|
||||||
})
|
})
|
||||||
|
export const getTimeFormatter = ({ locale = getCurrentLocale(), ...args } = {}) => createTimeFormatter({ locale, ...args })
|
||||||
|
|
||||||
export const getMessageFormatter = monadicMemoize(
|
export const getMessageFormatter = monadicMemoize(
|
||||||
(message: string, locale: string = getCurrentLocale()) =>
|
(message: string, locale: string = getCurrentLocale()) =>
|
||||||
|
|
|
@ -3,7 +3,8 @@ import {
|
||||||
getDateFormatter,
|
getDateFormatter,
|
||||||
getTimeFormatter,
|
getTimeFormatter,
|
||||||
getMessageFormatter,
|
getMessageFormatter,
|
||||||
init
|
init,
|
||||||
|
locale
|
||||||
} from '../../../src/runtime'
|
} from '../../../src/runtime'
|
||||||
|
|
||||||
beforeEach(() => {
|
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' })
|
init({ fallbackLocale: 'en' })
|
||||||
expect(getNumberFormatter().format(number)).toBe('123,123')
|
expect(getNumberFormatter().format(number)).toBe('123,123')
|
||||||
})
|
})
|
||||||
|
@ -49,6 +50,14 @@ describe('number formatter', () => {
|
||||||
getNumberFormatter({ style: 'currency', currency: 'BRL' }).format(number)
|
getNumberFormatter({ style: 'currency', currency: 'BRL' }).format(number)
|
||||||
).toBe('R$123,123.00')
|
).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', () => {
|
describe('date formatter', () => {
|
||||||
|
@ -98,6 +107,14 @@ describe('date formatter', () => {
|
||||||
getDateFormatter({ year: 'numeric', era: 'short' }).format(date)
|
getDateFormatter({ year: 'numeric', era: 'short' }).format(date)
|
||||||
).toBe('2019 AD')
|
).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', () => {
|
describe('time formatter', () => {
|
||||||
|
@ -151,6 +168,14 @@ describe('time formatter', () => {
|
||||||
}).format(time)
|
}).format(time)
|
||||||
).toBe('08:37:32 PM')
|
).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', () => {
|
describe('message formatter', () => {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user