Skip to content

Commit fa612d3

Browse files
authored
fix: number and datetime options override formatting (#1056)
1 parent d9d4a5c commit fa612d3

File tree

7 files changed

+52
-48
lines changed

7 files changed

+52
-48
lines changed

packages/core-base/src/datetime.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ export function datetime<
226226
)
227227

228228
if (!isString(key) || key === '') {
229-
return new Intl.DateTimeFormat(locale).format(value)
229+
return new Intl.DateTimeFormat(locale, overrides).format(value)
230230
}
231231

232232
// resolve format
@@ -296,7 +296,8 @@ export function datetime<
296296
return !part ? formatter.format(value) : formatter.formatToParts(value)
297297
}
298298

299-
const DATETIME_FORMAT_OPTIONS_KEYS = [
299+
/** @internal */
300+
export const DATETIME_FORMAT_OPTIONS_KEYS = [
300301
'localeMatcher',
301302
'weekday',
302303
'era',

packages/core-base/src/number.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export function number<
221221
)
222222

223223
if (!isString(key) || key === '') {
224-
return new Intl.NumberFormat(locale).format(value)
224+
return new Intl.NumberFormat(locale, overrides).format(value)
225225
}
226226

227227
// resolve format
@@ -291,7 +291,8 @@ export function number<
291291
return !part ? formatter.format(value) : formatter.formatToParts(value)
292292
}
293293

294-
const NUMBER_FORMAT_OPTIONS_KEYS = [
294+
/** @internal */
295+
export const NUMBER_FORMAT_OPTIONS_KEYS = [
295296
'localeMatcher',
296297
'style',
297298
'currency',
@@ -307,7 +308,11 @@ const NUMBER_FORMAT_OPTIONS_KEYS = [
307308
'notation',
308309
'signDisplay',
309310
'unit',
310-
'unitDisplay'
311+
'unitDisplay',
312+
'roundingMode',
313+
'roundingPriority',
314+
'roundingIncrement',
315+
'trailingZeroDisplay'
311316
]
312317

313318
/** @internal */

packages/core-base/test/number.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ test('override format options with number function options', () => {
158158
expect(
159159
number(ctx, 10100, { key: 'currency', locale: 'ja-JP', currency: 'EUR' })
160160
).toEqual('€10,100.00')
161+
expect(
162+
number(ctx, 123456.789, {
163+
style: 'currency',
164+
currency: 'USD',
165+
signDisplay: 'always'
166+
})
167+
).toEqual('+$123,456.79')
161168
})
162169

163170
test('fallback', () => {

packages/vue-i18n-core/src/components/DatetimeFormat.ts

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { DatetimePartsSymbol } from '../symbols'
33
import { renderFormatter } from './formatRenderer'
44
import { baseFormatProps } from './base'
55
import { assign } from '@intlify/shared'
6+
import { DATETIME_FORMAT_OPTIONS_KEYS } from '@intlify/core-base'
67

78
/* eslint-enable @typescript-eslint/no-unused-vars */
89
import type { DateTimeOptions } from '@intlify/core-base'
@@ -19,29 +20,6 @@ export type DatetimeFormatProps = FormattableProps<
1920
Intl.DateTimeFormatOptions
2021
>
2122

22-
const DATETIME_FORMAT_KEYS = [
23-
'dateStyle',
24-
'timeStyle',
25-
'fractionalSecondDigits',
26-
'calendar',
27-
'dayPeriod',
28-
'numberingSystem',
29-
'localeMatcher',
30-
'timeZone',
31-
'hour12',
32-
'hourCycle',
33-
'formatMatcher',
34-
'weekday',
35-
'era',
36-
'year',
37-
'month',
38-
'day',
39-
'hour',
40-
'minute',
41-
'second',
42-
'timeZoneName'
43-
]
44-
4523
/**
4624
* Datetime Format Component
4725
*
@@ -91,7 +69,7 @@ export const DatetimeFormat = /* #__PURE__*/ /*defineComponent */ {
9169
>(
9270
props as DatetimeFormatProps,
9371
context,
94-
DATETIME_FORMAT_KEYS,
72+
DATETIME_FORMAT_OPTIONS_KEYS,
9573
(...args: unknown[]) =>
9674
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9775
(i18n as any)[DatetimePartsSymbol](...args)

packages/vue-i18n-core/src/components/NumberFormat.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { NumberPartsSymbol } from '../symbols'
33
import { renderFormatter } from './formatRenderer'
44
import { baseFormatProps } from './base'
55
import { assign } from '@intlify/shared'
6+
import { NUMBER_FORMAT_OPTIONS_KEYS } from '@intlify/core-base'
67

78
import type { NumberOptions } from '@intlify/core-base'
89
import type { Composer, ComposerInternal } from '../composer'
@@ -18,24 +19,6 @@ export type NumberFormatProps = FormattableProps<
1819
Intl.NumberFormatOptions
1920
>
2021

21-
const NUMBER_FORMAT_KEYS = [
22-
'localeMatcher',
23-
'style',
24-
'unit',
25-
'unitDisplay',
26-
'currency',
27-
'currencyDisplay',
28-
'useGrouping',
29-
'numberingSystem',
30-
'minimumIntegerDigits',
31-
'minimumFractionDigits',
32-
'maximumFractionDigits',
33-
'minimumSignificantDigits',
34-
'maximumSignificantDigits',
35-
'notation',
36-
'formatMatcher'
37-
]
38-
3922
/**
4023
* Number Format Component
4124
*
@@ -85,7 +68,7 @@ export const NumberFormat = /* #__PURE__*/ /* defineComponent */ {
8568
>(
8669
props as NumberFormatProps,
8770
context,
88-
NUMBER_FORMAT_KEYS,
71+
NUMBER_FORMAT_OPTIONS_KEYS,
8972
(...args: unknown[]) =>
9073
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9174
(i18n as any)[NumberPartsSymbol](...args)

packages/vue-i18n-core/test/__snapshots__/issues.test.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
exports[`issue #968 1`] = `"<div class=\\"col-auto text-h6\\">£</div><div class=\\"col-auto text-h3\\">1</div><div class=\\"col-auto text-subtitle1 self-end text-amber\\">,</div><div class=\\"col-auto text-h3\\">150</div><div class=\\"col-auto text-subtitle1 self-end text-amber\\">,</div><div class=\\"col-auto text-h3\\">001</div><div class=\\"col-auto text-subtitle2 self-end text-primary\\">.</div><div class=\\"col-auto text-subtitle1 self-end text-red\\">20</div>"`;
44
55
exports[`issue #1014 1`] = `"<span>Add</span>"`;
6+
7+
exports[`issue #1054, #1053 1`] = `"<p>+$123,456.79</p><span>+$123,456.79</span>"`;

packages/vue-i18n-core/test/issues.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,31 @@ test('issue #1014', async () => {
558558

559559
expect(wrapper.html()).toMatchSnapshot()
560560
})
561+
562+
test('issue #1054, #1053', async () => {
563+
const i18n = createI18n({
564+
legacy: false,
565+
locale: 'en-US',
566+
datetimeFormats: {}
567+
})
568+
const App = defineComponent({
569+
setup() {
570+
return {
571+
amount: 123456.789,
572+
format: {
573+
style: 'currency',
574+
currency: 'USD',
575+
signDisplay: 'always'
576+
}
577+
}
578+
},
579+
template: `
580+
<p>{{ $n(amount, format) }}</p>
581+
<i18n-n tag="span" :value="amount" :format="format" scope="global"></i18n-n>
582+
`
583+
})
584+
585+
const wrapper = await mount(App, i18n)
586+
587+
expect(wrapper.html()).toMatchSnapshot()
588+
})

0 commit comments

Comments
 (0)