From c573b1a54a41baad007799c5bb37b9f6aa11c7eb Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Tue, 30 Apr 2024 19:07:32 +0900 Subject: [PATCH 1/3] breaking: change `$t` overloaded signature for Legacy API mode --- docs/.vitepress/config.mts | 8 +- docs/api/injection.md | 125 ++++---------- docs/guide/migration/breaking.md | 2 +- docs/guide/migration/breaking10.md | 102 ++++++++++++ docs/guide/migration/features.md | 2 +- examples/legacy/components/translation.html | 8 +- packages/petite-vue-i18n/src/vue.d.ts | 173 ++----------------- packages/vue-i18n-core/src/legacy.ts | 32 +--- packages/vue-i18n-core/test/i18n.test.ts | 99 +++++++++++ packages/vue-i18n-core/test/legacy.test.ts | 19 --- packages/vue-i18n/src/vue.d.ts | 175 ++------------------ 11 files changed, 274 insertions(+), 471 deletions(-) create mode 100644 docs/guide/migration/breaking10.md diff --git a/docs/.vitepress/config.mts b/docs/.vitepress/config.mts index cb63e9656..1bfd663cf 100644 --- a/docs/.vitepress/config.mts +++ b/docs/.vitepress/config.mts @@ -195,11 +195,15 @@ function sidebarGuide() { collapsible: true, items: [ { - text: 'Breaking Changes', + text: 'Breaking Changes in v9', link: '/guide/migration/breaking' }, { - text: 'New Features', + text: 'Breaking Changes in v10', + link: '/guide/migration/breaking10' + }, + { + text: 'New Features in v9', link: '/guide/migration/features' }, { diff --git a/docs/api/injection.md b/docs/api/injection.md index 0301d5cb3..15c271c8d 100644 --- a/docs/api/injection.md +++ b/docs/api/injection.md @@ -90,6 +90,10 @@ Translation message ### $t(key, locale) +:::danger NOTICE +**This API signature is available in Legacy API mode only and is supported until v9**. +::: + Locale message translation **Signature:** @@ -113,6 +117,10 @@ Translation message ### $t(key, locale, list) +:::danger NOTICE +**This API signature is available in Legacy API mode only and is supported until v9**. +::: + Locale message translation **Signature:** @@ -137,6 +145,10 @@ Translation message ### $t(key, locale, named) +:::danger NOTICE +**This API signature is available in Legacy API mode only and is supported until v9**. +::: + Locale message translation **Signature:** @@ -159,86 +171,17 @@ Overloaded `$t`. About details, see the [$t](injection#t-key) remarks. Translation message -### $t(key, list) - -Locale message translation - -**Signature:** -```typescript -$t(key: Path, list: unknown[]): TranslateResult; -``` - -**Details** - -Overloaded `$t`. About details, see the [$t](injection#t-key) remarks. - -#### Parameters -| Parameter | Type | Description | -| --- | --- | --- | -| key | Path | A target locale message key | -| list | unknown[] | A values of list interpolation | - -#### Returns - -Translation message - -### $t(key, named) - -Locale message translation - -**Signature:** -```typescript -$t(key: Path, named: Record): TranslateResult; -``` - -**Details** - -Overloaded `$t`. About details, see the [$t](injection#t-key) remarks. - -#### Parameters -| Parameter | Type | Description | -| --- | --- | --- | -| key | Path | A target locale message key | -| locale | Locale | A locale, override locale that global scope or local scope | -| named | Record<string, unknown> | A values of named interpolation | - -#### Returns - -Translation message - -### $t(key) - -Locale message translation - -**Signature:** -```typescript -$t(key: Path): string; -``` - -**Details** - -Overloaded `$t`. About details, see the [$t](injection#t-key) remarks. - -#### Parameters -| Parameter | Type | Description | -| --- | --- | --- | -| key | Path | A target locale message key | - -#### Returns - -Translation message - ### $t(key, plural) Locale message translation **Signature:** ```typescript -$t(key: Path, plural: number): string; +$t(key: Path, plural: number): TranslateResult; ``` :::tip NOTE -Supported for **Composition API mode only**. +Supported for **Composition API mode only until v9. v10 or later can also be used in Legacy API mode**. ::: **Details** @@ -261,11 +204,11 @@ Locale message translation **Signature:** ```typescript -$t(key: Path, plural: number, options: TranslateOptions): string; +$t(key: Path, plural: number, options: TranslateOptions): TranslateResult; ``` :::tip NOTE -Supported for **Composition API mode only**. +Supported for **Composition API mode only until v9. v10 or later can also be used in Legacy API mode**. ::: **Details** @@ -289,11 +232,11 @@ Locale message translation **Signature:** ```typescript -$t(key: Path, defaultMsg: string): string; +$t(key: Path, defaultMsg: string): TranslateResult; ``` :::tip NOTE -Supported for **Composition API mode only**. +Supported for **Composition API mode only until v9. v10 or later can also be used in Legacy API mode**. ::: **Details** @@ -316,11 +259,11 @@ Locale message translation **Signature:** ```typescript -$t(key: Path, defaultMsg: string, options: TranslateOptions): string; +$t(key: Path, defaultMsg: string, options: TranslateOptions): TranslateResult; ``` :::tip NOTE -Supported for **Composition API mode only**. +Supported for **Composition API mode only until v9. v10 or later can also be used in Legacy API mode**. ::: **Details** @@ -344,7 +287,7 @@ Locale message translation **Signature:** ```typescript -$t(key: Path, list: unknown[]): string; +$t(key: Path, list: unknown[]): TranslateResult; ``` **Details** @@ -367,11 +310,11 @@ Locale message translation **Signature:** ```typescript -$t(key: Path, list: unknown[], plural: number): string; +$t(key: Path, list: unknown[], plural: number): TranslateResult; ``` :::tip NOTE -Supported for **Composition API mode only**. +Supported for **Composition API mode only until v9. v10 or later can also be used in Legacy API mode**. ::: **Details** @@ -395,11 +338,11 @@ Locale message translation **Signature:** ```typescript -$t(key: Path, list: unknown[], defaultMsg: string): string; +$t(key: Path, list: unknown[], defaultMsg: string): TranslateResult; ``` :::tip NOTE -Supported for **Composition API mode only**. +Supported for **Composition API mode only until v9. v10 or later can also be used in Legacy API mode**. ::: **Details** @@ -423,11 +366,11 @@ Locale message translation **Signature:** ```typescript -$t(key: Path, list: unknown[], options: TranslateOptions): string; +$t(key: Path, list: unknown[], options: TranslateOptions): TranslateResult; ``` :::tip NOTE -Supported for **Composition API mode only**. +Supported for **Composition API mode only until v9. v10 or later can also be used in Legacy API mode**. ::: **Details** @@ -451,7 +394,7 @@ Locale message translation **Signature:** ```typescript -$t(key: Path, named: NamedValue): string; +$t(key: Path, named: NamedValue): TranslateResult; ``` **Details** @@ -474,11 +417,11 @@ Locale message translation **Signature:** ```typescript -$t(key: Path, named: NamedValue, plural: number): string; +$t(key: Path, named: NamedValue, plural: number): TranslateResult; ``` :::tip NOTE -Supported for **Composition API mode only**. +Supported for **Composition API mode only until v9. v10 or later can also be used in Legacy API mode**. ::: **Details** @@ -502,11 +445,11 @@ Locale message translation **Signature:** ```typescript -$t(key: Path, named: NamedValue, defaultMsg: string): string; +$t(key: Path, named: NamedValue, defaultMsg: string): TranslateResult; ``` :::tip NOTE -Supported for **Composition API mode only**. +Supported for **Composition API mode only until v9. v10 or later can also be used in Legacy API mode**. ::: **Details** @@ -530,11 +473,11 @@ Locale message translation **Signature:** ```typescript -$t(key: Path, named: NamedValue, options: TranslateOptions): string; +$t(key: Path, named: NamedValue, options: TranslateOptions): TranslateResult; ``` :::tip NOTE -Supported for **Composition API mode only**. +Supported for **Composition API mode only until v9. v10 or later can also be used in Legacy API mode**. ::: **Details** diff --git a/docs/guide/migration/breaking.md b/docs/guide/migration/breaking.md index 131904fe7..630d7f2a4 100644 --- a/docs/guide/migration/breaking.md +++ b/docs/guide/migration/breaking.md @@ -1,4 +1,4 @@ -# Breaking Changes +# Breaking Changes in v9 Most of the APIs offered in Vue I18n v9 (for Vue 3) strive to maintain compatibility, to ease the pain of migration from v8 (for Vue 2). But there are still a few breaking changes that you might encounter while migrating your application. This guide is how to adapt your application to make it work with Vue I18n v9. diff --git a/docs/guide/migration/breaking10.md b/docs/guide/migration/breaking10.md new file mode 100644 index 000000000..d2d3fd6e0 --- /dev/null +++ b/docs/guide/migration/breaking10.md @@ -0,0 +1,102 @@ +# Breaking Changes in v10 + +:::warning NOTICE +Vue I18n v10 **is still an alpha version**. +::: + +## APIs + +### Change `$t` overloaded signature for Legacy API mode + +In Vue I18n v9, it has a different interface from the Composition API mode and Legacy API mode of the `$t` overloade signature. + +Especially, `$t` signature in Legacy API mode has fewer overloaded signatures than in Composition API mode, as shown below: + +| `$t` overloaded signatures | Legacy API v9 | Legacy API v10 | Composition API v9 & v10 | +| -------------------------------------------------------------------------------- | ------------- | -------------- | ------------------------ | +| `$t(key: Path): TranslateResult;` | ✅ | ✅ | ✅ | +| `$t(key: Path, locale: Locale): TranslateResult;` | ✅ | - | - | +| `$t(key: Path, locale: Locale, list: unknown[]): TranslateResult;` | ✅ | - | - | +| `$t(key: Path, locale: Locale, named: NamedValue): TranslateResult;` | ✅ | - | - | +| `$t(key: Path, plural: number): TranslateResult;` | - | ✅ | ✅ | +| `$t(key: Path, plural: number, options: TranslateOptions): TranslateResult;` | - | ✅ | ✅ | +| `$t(key: Path, defaultMsg: string): TranslateResult;` | - | ✅ | ✅ | +| `$t(key: Path, defaultMsg: string, options: TranslateOptions): TranslateResult;` | - | ✅ | ✅ | +| `$t(key: Path, list: unknown[]): TranslateResult;` | ✅ | ✅ | ✅ | +| `$t(key: Path, list: unknown[], plural: number): TranslateResult;` | - | ✅ | ✅ | +| `$t(key: Path, list: unknown[], defaultMsg: string): TranslateResult;` | - | ✅ | ✅ | +| `$t(key: Path, list: unknown[], options: TranslateOptions): TranslateResult;` | - | ✅ | ✅ | +| `$t(key: Path, named: Record): TranslateResult;` | ✅ | ✅ | ✅ | +| `$t(key: Path, named: NamedValue, plural: number): TranslateResult;` | - | ✅ | ✅ | +| `$t(key: Path, named: NamedValue, defaultMsg: string): TranslateResult;` | - | ✅ | ✅ | +| `$t(key: Path, named: NamedValue, options: TranslateOptions): TranslateResult;` | - | ✅ | ✅ | + +Starting from v10, Legacy API mode can use the same `$t` overload signature as Composition API mode. + +**Reason**: After that migration, when migrating to Composition API mode, we sometimes fall into a pitfall due to the different signature of `$t`. + +If you are using the following APIs in Legacy API mode, you must change to another signature because of the breaking changes: + +- `$t(key: Path, locale: Locale): TranslateResult;` +- `$t(key: Path, locale: Locale, list: unknown[]): TranslateResult;` +- `$t(key: Path, locale: Locale, named: NamedValue): TranslateResult;` + +#### `$t(key: Path, locale: Locale): TranslateResult;` + +Vue I18n v9.x: + +```vue + +``` + +Vue I18n v10 or later: + +use `$t(key: Path, list: unknown[], options: TranslateOptions): TranslateResult;` or `$t(key: Path, named: NamedValue, options: TranslateOptions): TranslateResult;` + +```vue + +``` + +#### `$t(key: Path, locale: Locale, list: unknown[]): TranslateResult;` + +Vue I18n v9.x: + +```vue + +``` + +Vue I18n v10 or later: + +use `$t(key: Path, list: unknown[], options: TranslateOptions): TranslateResult;` + +```vue + +``` + +#### `$t(key: Path, locale: Locale, named: NamedValue): TranslateResult;` + +Vue I18n v9.x: + +```vue + +``` + +Vue I18n v10 or later: + +use `$t(key: Path, named: NamedValue, options: TranslateOptions): TranslateResult;` + +```vue + +``` diff --git a/docs/guide/migration/features.md b/docs/guide/migration/features.md index 3540e9f61..dc5f28ea8 100644 --- a/docs/guide/migration/features.md +++ b/docs/guide/migration/features.md @@ -1,4 +1,4 @@ -# New Features +# New Features in v9 Vue I18n v9 offers not only Vue 3 support, but this version also is included new features. diff --git a/examples/legacy/components/translation.html b/examples/legacy/components/translation.html index b0369461a..93fc0cc28 100644 --- a/examples/legacy/components/translation.html +++ b/examples/legacy/components/translation.html @@ -19,7 +19,9 @@

localize with slot contents:

localize with DOM contents:

- {{ $t('message.language','en') }} + {{ $t('message.language', [], { locale: 'en' }) }}

localize with using linked:

@@ -31,7 +33,9 @@

localize with using linked:

localize with using plural:

- +