Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions packages/vue-i18n-core/src/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ export interface ComposerNumberFormatting<
| Key
| ResourceKeys
| NumberOptions<Key | ResourceKeys, Locales>
): string
): string | Intl.NumberFormatPart[]
/**
* Number Formatting
*
Expand All @@ -1231,7 +1231,7 @@ export interface ComposerNumberFormatting<
| ResourceKeys
| NumberOptions<Key | ResourceKeys, Locales>,
locale: Locales
): string
): string | Intl.NumberFormatPart[]
}

/**
Expand Down Expand Up @@ -2287,14 +2287,17 @@ export function createComposer(options: any = {}): any {
}

// n
function n(...args: unknown[]): string {
return wrapWithDeps<{}, string>(
context => Reflect.apply(number, null, [context, ...args]) as string,
function n(...args: unknown[]): string | Intl.NumberFormatPart[] {
return wrapWithDeps<{}, string | Intl.NumberFormatPart[]>(
context =>
Reflect.apply(number, null, [context, ...args]) as
| string
| Intl.NumberFormatPart[],
() => parseNumberArgs(...args),
'number format',
root => Reflect.apply(root.n, root, [...args]),
() => MISSING_RESOLVE_VALUE,
val => isString(val)
val => isString(val) || isArray(val)
)
}

Expand Down
14 changes: 7 additions & 7 deletions packages/vue-i18n-core/test/composer.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,15 +353,15 @@ test('strict composer with direct options', () => {
strictDirectComposer.d(new Date(), 'custom' as any)
).toEqualTypeOf<string>()
expectTypeOf(strictDirectComposer.n(1)).toEqualTypeOf<string>()
expectTypeOf(
strictDirectComposer.n(1, 'currency', 'zh')
).toEqualTypeOf<string>()
expectTypeOf(strictDirectComposer.n(1, 'currency', 'zh')).toEqualTypeOf<
string | Intl.NumberFormatPart[]
>()
expectTypeOf(
strictDirectComposer.n(1, { key: 'currency', locale: 'en' })
).toEqualTypeOf<string>()
expectTypeOf(
strictDirectComposer.n(1, 'custom' as any)
).toEqualTypeOf<string>()
).toEqualTypeOf<string | Intl.NumberFormatPart[]>()
expectTypeOf(strictDirectComposer.n(1, 'custom' as any)).toEqualTypeOf<
string | Intl.NumberFormatPart[]
>()

// const noOptionsComposer = createComposer({ missingWarn: true })
const noOptionsComposer = createComposer({ locale: 'en' })
Expand Down
109 changes: 91 additions & 18 deletions packages/vue-i18n-core/test/composer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,41 @@

// utils
import * as shared from '@intlify/shared'
import { pluralRules as _pluralRules } from './helper'
vi.mock('@intlify/shared', async () => {
const actual = await vi.importActual<object>('@intlify/shared')
return {
...actual,
warn: vi.fn()
}
})
import { pluralRules as _pluralRules } from './helper'

import {
compile,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import statements were moved due to the file linting while committing new test cases

fallbackWithLocaleChain,
Locale,
MessageContext,
MessageFunction,
Path,
PathValue,
registerLocaleFallbacker,
registerMessageCompiler,
registerMessageResolver,
resolveValue
} from '@intlify/core-base'
import { createVNode, nextTick, Text, watch, watchEffect } from 'vue'
import {
ComposerOptions,
createComposer,
MissingHandler,
ComposerOptions,
VueMessageType
} from '../src/composer'
import {
TranslateVNodeSymbol,
DatetimePartsSymbol,
NumberPartsSymbol,
DatetimePartsSymbol
TranslateVNodeSymbol
} from '../src/symbols'
import { getWarnMessage, I18nWarnCodes } from '../src/warnings'
import { watch, watchEffect, nextTick, Text, createVNode } from 'vue'
import {
Locale,
compile,
registerMessageCompiler,
resolveValue,
registerMessageResolver,
fallbackWithLocaleChain,
registerLocaleFallbacker,
MessageContext,
Path,
PathValue,
MessageFunction
} from '@intlify/core-base'

beforeEach(() => {
registerMessageCompiler(compile)
Expand Down Expand Up @@ -1202,6 +1202,79 @@ describe('n', () => {
})
expect(n(0.99, { key: 'percent' })).toEqual('')
})

test('part formatting with n', () => {
const { n } = createComposer({
locale: 'en-US',
fallbackLocale: ['ja-JP'],
numberFormats: {
'en-US': {
currency: {
style: 'currency',
currency: 'USD',
currencyDisplay: 'symbol'
},
decimal: {
style: 'decimal',
useGrouping: true
}
},
'ja-JP': {
currency: {
style: 'currency',
currency: 'JPY' /*, currencyDisplay: 'symbol'*/
},
numeric: {
style: 'decimal',
useGrouping: false
},
percent: {
style: 'percent',
useGrouping: true
}
}
}
})
expect(n(0.99, { key: 'currency', part: true })).toEqual([
{ type: 'currency', value: '$' },
{ type: 'integer', value: '0' },
{ type: 'decimal', value: '.' },
{ type: 'fraction', value: '99' }
])
expect(
n(10100, {
key: 'currency',
locale: 'ja-JP',
part: true
})
).toEqual([
{ type: 'currency', value: '¥' },
{ type: 'integer', value: '10' },
{ type: 'group', value: ',' },
{ type: 'integer', value: '100' }
])
expect(n(12145281000, { key: 'percent', part: true })).toEqual([
{ type: 'integer', value: '1' },
{ type: 'group', value: ',' },
{ type: 'integer', value: '214' },
{ type: 'group', value: ',' },
{ type: 'integer', value: '528' },
{ type: 'group', value: ',' },
{ type: 'integer', value: '100' },
{ type: 'group', value: ',' },
{ type: 'integer', value: '000' },
{ type: 'percentSign', value: '%' }
])
expect(n(12145281111, { key: 'decimal', part: true })).toEqual([
{ type: 'integer', value: '12' },
{ type: 'group', value: ',' },
{ type: 'integer', value: '145' },
{ type: 'group', value: ',' },
{ type: 'integer', value: '281' },
{ type: 'group', value: ',' },
{ type: 'integer', value: '111' }
])
})
})

describe('tm', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/vue-i18n/src/vue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,10 @@ declare module 'vue' {
*
* @returns formatted value
*/
$n(value: number, options: NumberOptions): string
$n<OptionsType extends NumberOptions>(
value: number,
options: OptionsType
): OptionsType['type'] extends true ? Intl.NumberFormatPart[] : string
/**
* Locale messages getter
*
Expand Down