Skip to content

Commit f902e24

Browse files
authored
fix!: correct finalizePendingLocaleChange signature to be synchronous (#3626)
1 parent f4fc73f commit f902e24

10 files changed

Lines changed: 19 additions & 27 deletions

File tree

docs/content/docs/02.guide/08.lang-switcher.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ export default defineNuxtConfig({
9292
<script setup lang="ts">
9393
const { finalizePendingLocaleChange } = useI18n()
9494

95-
const onBeforeEnter = async () => {
96-
await finalizePendingLocaleChange()
95+
const onBeforeEnter = () => {
96+
finalizePendingLocaleChange()
9797
}
9898
</script>
9999

@@ -160,8 +160,8 @@ definePageMeta({
160160
}
161161
})
162162

163-
route.meta.pageTransition.onBeforeEnter = async () => {
164-
await finalizePendingLocaleChange()
163+
route.meta.pageTransition.onBeforeEnter = () => {
164+
finalizePendingLocaleChange()
165165
}
166166
</script>
167167

docs/content/docs/02.guide/90.migrating.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Check the documentation detailing the breaking changes [here](https://vue-i18n.i
1313
### Lazy load locale messages by default and removed `lazy` option
1414
The `lazy` option has been removed and lazy loading of locale messages is now the default behavior.
1515

16+
### Function signature corrected for `finalizePendingLocaleChange()`{lang="ts"}
17+
The function signature for `finalizePendingLocaleChange()`{lang="ts"} has been corrected from `() => Promise<void>`{lang="ts-type"} to `() => void`{lang="ts-type"}.
18+
This change was made since the function does not rely on any async operations and should not be awaited, and should prevent unnecessary function coloring.
19+
1620
### Option default changed `useLocaleHead()`{lang="ts"} and `$localeHead()`{lang="ts"}
1721
The default value for the `key` property has been changed from `'hid'` to `'key'`.
1822

docs/content/docs/04.api/04.vue-i18n.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ Returns browser locale code filtered against the ones defined in options.
5151

5252
- **Arguments**:
5353
- no arguments
54-
- **Returns**: `Promise<void>`{lang="ts-type"}
54+
- **Returns**: `void`{lang="ts-type"}
5555

56-
Switches to the pending locale that would have been set on navigate, but was prevented by the option [`skipSettingLocaleOnNavigate`](/docs/api/options#skipsettinglocaleonnavigate). See more information in [Wait for page transition](/docs/guide/lang-switcher#wait-for-page-transition).
56+
Switches locale to the pending locale, used when navigation locale switch is prevented by the [`skipSettingLocaleOnNavigate`](/docs/api/options#skipsettinglocaleonnavigate) option. See [Wait for page transition](/docs/guide/lang-switcher#wait-for-page-transition) for more information.
5757

5858
### `waitForPendingLocaleChange()`{lang="ts"}
5959

specs/fixtures/basic_usage/app.vue

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ const skipSettingLocale = useRuntimeConfig().public.i18n.skipSettingLocaleOnNavi
88
const pageTransition = {
99
name: 'my',
1010
mode: 'out-in',
11-
onBeforeEnter: async () => {
12-
await finalizePendingLocaleChange()
13-
}
11+
onBeforeEnter: () => finalizePendingLocaleChange()
1412
}
1513
</script>
1614

specs/fixtures/basic_usage/pages/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { computed, navigateTo, ref, useAppConfig, useAsyncData, useHead, watch }
44
import LangSwitcher from '../components/LangSwitcher.vue'
55
import LocalScope from '../components/LocalScope.vue'
66
7-
const { t, locale, locales, localeProperties, finalizePendingLocaleChange } = useI18n()
7+
const { t, locale, locales, localeProperties } = useI18n()
88
const localePath = useLocalePath()
99
const switchLocalePath = useSwitchLocalePath()
1010
const localeRoute = useLocaleRoute()

specs/fixtures/basic_usage_compat_4/app/app.vue

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ const skipSettingLocale = useRuntimeConfig().public.i18n.skipSettingLocaleOnNavi
88
const pageTransition = {
99
name: 'my',
1010
mode: 'out-in',
11-
onBeforeEnter: async () => {
12-
await finalizePendingLocaleChange()
13-
}
11+
onBeforeEnter: () => finalizePendingLocaleChange()
1412
}
1513
</script>
1614

specs/fixtures/basic_usage_compat_4/app/pages/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { computed, navigateTo, ref, useAppConfig, useAsyncData, useHead, watch }
44
import LangSwitcher from '../components/LangSwitcher.vue'
55
import LocalScope from '../components/LocalScope.vue'
66
7-
const { t, locale, locales, localeProperties, finalizePendingLocaleChange } = useI18n()
7+
const { t, locale, locales, localeProperties } = useI18n()
88
const localePath = useLocalePath()
99
const switchLocalePath = useSwitchLocalePath()
1010
const localeRoute = useLocaleRoute()

src/runtime/plugins/i18n.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ export default defineNuxtPlugin({
113113
composer.onLanguageSwitched = (oldLocale, newLocale) =>
114114
nuxt.callHook('i18n:localeSwitched', { oldLocale, newLocale }) as Promise<void>
115115

116-
// eslint-disable-next-line @typescript-eslint/require-await --- TODO: breaking - signature should be synchronous
117-
composer.finalizePendingLocaleChange = async () => {
116+
composer.finalizePendingLocaleChange = () => {
118117
if (!i18n.__pendingLocale) return
119118

120119
ctx.setLocale(i18n.__pendingLocale)

src/runtime/types.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ export interface ComposerCustomProperties<
143143
*/
144144
onLanguageSwitched: LanguageSwitchedHandler
145145
/**
146-
* Switches to the pending locale that would have been set on navigate, but was prevented by the `skipSettingLocaleOnNavigate` option.
146+
* Switches locale to the pending locale, used when navigation locale switch is prevented by the `skipSettingLocaleOnNavigate` option.
147147
*/
148-
finalizePendingLocaleChange: () => Promise<void>
148+
finalizePendingLocaleChange: () => void
149149
/**
150150
* Returns a promise that will be resolved once the pending locale is set.
151151
*/
@@ -167,13 +167,6 @@ declare module 'vue-i18n' {
167167
interface I18n {
168168
/** @internal */ __pendingLocale?: string
169169
/** @internal */ __pendingLocalePromise?: Promise<void>
170-
/**
171-
* Sets the value of the locale property on VueI18n or Composer instance
172-
*
173-
* This differs from the instance `setLocale` method in that it sets the
174-
* locale property directly without triggering other side effects
175-
* @internal
176-
*/
177-
__resolvePendingLocalePromise?: () => void
170+
/** @internal */ __resolvePendingLocalePromise?: () => void
178171
}
179172
}

src/runtime/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ export async function navigate(redirectPath: string, routePath: string, locale:
278278
const vueI18n = ctx.getVueI18n()
279279
vueI18n.__pendingLocale = locale
280280
vueI18n.__pendingLocalePromise = new Promise(resolve => {
281-
vueI18n.__resolvePendingLocalePromise = () => resolve()
281+
vueI18n.__resolvePendingLocalePromise = resolve
282282
})
283283
if (!force) {
284284
return

0 commit comments

Comments
 (0)