Skip to content

Commit

Permalink
breaking: drop fully getChoiceIndex on Legacy API (#1829)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon authored Apr 27, 2024
1 parent 1d9459a commit 0c92dcc
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 39 deletions.
1 change: 0 additions & 1 deletion docs/.ja/guide/advanced/composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -531,4 +531,3 @@ Below is the mapping table:
| `getNumberFormat` | `getNumberFormat` |
| `setNumberFormat` | `setNumberFormat` |
| `mergeNumberFormat` | `mergeNumberFormat` |
| `getChoiceIndex` | N/A |
1 change: 0 additions & 1 deletion docs/guide/advanced/composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -500,4 +500,3 @@ Below is the mapping table:
| `getNumberFormat` | `getNumberFormat` |
| `setNumberFormat` | `setNumberFormat` |
| `mergeNumberFormat` | `mergeNumberFormat` |
| `getChoiceIndex` | N/A |
3 changes: 3 additions & 0 deletions docs/guide/migration/breaking.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ Similar to *[Translation API return value](#translation-api-return-value)*, the

### Remove `getChoiceIndex`

> [!CAUTION]
> `getChoiceIndex` option implementation code is be going to fully remove in v10.
To customize the pluralization rules, Vue I18n v8.x extends `getChoiceIndex` of VueI18n class.

Vue I18n v8.x:
Expand Down
21 changes: 1 addition & 20 deletions packages/vue-i18n-core/src/legacy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { createComposer, DefineLocaleMessage } from './composer'
import { I18nWarnCodes, getWarnMessage } from './warnings'
import { createI18nError, I18nErrorCodes } from './errors'
import { EnableEmitter, DisableEmitter } from './symbols'
import { DEFAULT_LOCALE } from '@intlify/core-base'
Expand All @@ -12,8 +11,7 @@ import {
isBoolean,
isFunction,
isRegExp,
assign,
warn
assign
} from '@intlify/shared'

import type {
Expand Down Expand Up @@ -1236,15 +1234,6 @@ export interface VueI18n<
* @param format - A target number format
*/
mergeNumberFormat: Composition['mergeNumberFormat']
/**
* Get choice index
*
* @remarks
* Get pluralization index for current pluralizing number and a given amount of choices.
*
* @deprecated Use `pluralizationRules` option instead of `getChoiceIndex`.
*/
getChoiceIndex: (choice: Choice, choicesLength: number) => number
}

/**
Expand Down Expand Up @@ -1691,14 +1680,6 @@ export function createVueI18n(options: any = {}): any {
// mergeNumberFormat
mergeNumberFormat(locale: Locale, format: NumberFormat): void {
composer.mergeNumberFormat(locale, format)
},

// getChoiceIndex
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getChoiceIndex(choice: Choice, choicesLength: number): number {
__DEV__ &&
warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_GET_CHOICE_INDEX))
return -1
}
}

Expand Down
10 changes: 4 additions & 6 deletions packages/vue-i18n-core/src/warnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ const inc = incrementer(code)

export const I18nWarnCodes = {
FALLBACK_TO_ROOT: code, // 8
NOT_SUPPORTED_GET_CHOICE_INDEX: inc(), // 9
COMPONENT_NAME_LEGACY_COMPATIBLE: inc(), // 10
NOT_FOUND_PARENT_SCOPE: inc(), // 11
IGNORE_OBJ_FLATTEN: inc(), // 12
NOTICE_DROP_TRANSLATE_EXIST_COMPATIBLE_FLAG: inc() // 13
COMPONENT_NAME_LEGACY_COMPATIBLE: inc(), // 9
NOT_FOUND_PARENT_SCOPE: inc(), // 10
IGNORE_OBJ_FLATTEN: inc(), // 11
NOTICE_DROP_TRANSLATE_EXIST_COMPATIBLE_FLAG: inc() // 12
} as const

type I18nWarnCodes = (typeof I18nWarnCodes)[keyof typeof I18nWarnCodes]

export const warnMessages: { [code: number]: string } = {
[I18nWarnCodes.FALLBACK_TO_ROOT]: `Fall back to {type} '{key}' with root locale.`,
[I18nWarnCodes.NOT_SUPPORTED_GET_CHOICE_INDEX]: `Not supported 'getChoiceIndex'.`,
[I18nWarnCodes.COMPONENT_NAME_LEGACY_COMPATIBLE]: `Component name legacy compatible: '{name}' -> 'i18n'`,
[I18nWarnCodes.NOT_FOUND_PARENT_SCOPE]: `Not found parent scope. use the global scope.`,
[I18nWarnCodes.IGNORE_OBJ_FLATTEN]: `Ignore object flatten: '{key}' key has an string value`,
Expand Down
11 changes: 0 additions & 11 deletions packages/vue-i18n-core/test/legacy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,17 +567,6 @@ test('getNumberFormat / setNumberFormat / mergeNumberFormat', () => {
})
})

test('getChoiceIndex', () => {
const mockWarn = vi.spyOn(shared, 'warn')
mockWarn.mockImplementation(() => {})

const i18n = createVueI18n({})
i18n.getChoiceIndex(1, 2)
expect(mockWarn.mock.calls[0][0]).toEqual(
getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_GET_CHOICE_INDEX)
)
})

test('warnHtmlInMessage', () => {
const mockWarn = vi.spyOn(shared, 'warn')
mockWarn.mockImplementation(() => {})
Expand Down

0 comments on commit 0c92dcc

Please sign in to comment.