Skip to content

Commit 1fc1709

Browse files
BobbieGoedeBobbie Goede
andauthored
chore!: deprecate $localeHead and rename $getRouteBaseName (#3528)
Co-authored-by: Bobbie Goede <mailto:6649305+BobbieGoede@users.noreply.github.com>
1 parent 2c1a7f1 commit 1fc1709

11 files changed

Lines changed: 75 additions & 38 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ We have upgrade from Vue I18n v10 to v11, this major version bump deprecates the
1010

1111
Check the documentation detailing the breaking changes [here](https://vue-i18n.intlify.dev/guide/migration/breaking11.html).
1212

13+
### Deprecate `$localeHead()`{lang="ts"}
14+
The `$localeHead()`{lang="ts"} Nuxt context function has been deprecated and will be removed in v11.
15+
16+
This context function has limited use cases, the `useLocaleHead` composable offers the same functionality and is easier to use in combination with `useHead`.
17+
18+
### Deprecate `$getRouteBaseName()`{lang="ts"} in favor of `$routeBaseName()`{lang="ts"}
19+
The `$getRouteBaseName()`{lang="ts"} Nuxt context function has been deprecated and will be removed in v11.
20+
21+
We are renaming this function to `$routeBaseName()`{lang="ts"} to be consistent with the other context functions and their composable counterparts.
22+
1323
### Promote `experimental.autoImportTranslationFunctions` to `autoDeclare`
1424
This functionality is stable and is now enabled by default, the option has been moved out of the `experimental` configuration object and renamed to `autoDeclare`.
1525

docs/content/docs/04.api/05.vue.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ description: Extension of Vue.
99
The APIs listed are available in the Options API. They are kept for Nuxt2 to migration from `@nuxtjs/i18n`. we will be deprecated in the future.
1010
::
1111

12-
### `getRouteBaseName()`{lang="ts"}
12+
### `routeBaseName()`{lang="ts"}
1313

1414
- **Arguments**:
1515
- route (type: `string | Route`{lang="ts-type"}, default: current route)

docs/content/docs/04.api/06.nuxt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default defineNuxtPlugin(nuxtApp => {
2727
})
2828
```
2929

30-
### `$getRouteBaseName()`{lang="ts"}
30+
### `$routeBaseName()`{lang="ts"}
3131

3232
### `$switchLocalePath()`{lang="ts"}
3333

docs/content/docs/06.composables/06.use-route-base-name.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ declare function useRouteBaseName(
1717
```vue
1818
<script setup>
1919
const route = useRoute()
20-
const getRouteBaseName = useRouteBaseName()
21-
const baseRouteName = computed(() => getRouteBaseName(route))
20+
const routeBaseName = useRouteBaseName()
21+
const baseRouteName = computed(() => routeBaseName(route))
2222
// or
23-
const baseRouteNameString = computed(() => getRouteBaseName(route.name))
23+
const baseRouteNameString = computed(() => routeBaseName(route.name))
2424
</script>
2525
2626
<template>
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
// https://nuxt.com/docs/guide/directory-structure/nuxt.config
22
export default defineNuxtConfig({
33
modules: ['./layer-module', './installer-module', '@nuxtjs/i18n'],
4+
45
runtimeConfig: {
56
public: {
67
runtimeValue: 'Hello from runtime config!',
78
longTextTest: false
89
}
910
},
11+
1012
extends: [
1113
`../layers/layer-server`,
1214
`../layers/layer-lazy`,
1315
`../layers/layer-vueI18n-options/layer-simple`,
1416
`../layers/layer-vueI18n-options/layer-simple-secondary`
1517
],
18+
1619
plugins: [`../plugins/i18nHooks.ts`],
20+
1721
i18n: {
1822
baseUrl: 'http://localhost:3000',
1923
vueI18n: './config/i18n.config.ts',
2024
locales: ['en', 'fr'],
2125
defaultLocale: 'en'
22-
}
26+
},
27+
28+
compatibilityDate: '2025-04-06'
2329
})

specs/fixtures/basic_usage/pages/nuxt-context-extension.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
<template>
44
<div>
5-
<p id="get-route-base-name">{{ $nuxt.$getRouteBaseName($route) }}</p>
6-
<p id="get-route-base-name-string">{{ $nuxt.$getRouteBaseName($route.name) }}</p>
5+
<p id="get-route-base-name">{{ $nuxt.$routeBaseName($route) }}</p>
6+
<p id="get-route-base-name-string">{{ $nuxt.$routeBaseName($route.name) }}</p>
77
<p id="switch-locale-path">{{ $nuxt.$switchLocalePath('ja') }}</p>
88
<p id="locale-path">{{ $nuxt.$localePath('nuxt-context-extension', 'nl') }}</p>
99
<p id="locale-route">{{ JSON.stringify($nuxt.$localeRoute()) }}</p>

specs/fixtures/basic_usage_compat_4/app/pages/nuxt-context-extension.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
<template>
44
<div>
5-
<p id="get-route-base-name">{{ $nuxt.$getRouteBaseName($route) }}</p>
6-
<p id="get-route-base-name-string">{{ $nuxt.$getRouteBaseName($route.name) }}</p>
5+
<p id="get-route-base-name">{{ $nuxt.$routeBaseName($route) }}</p>
6+
<p id="get-route-base-name-string">{{ $nuxt.$routeBaseName($route.name) }}</p>
77
<p id="switch-locale-path">{{ $nuxt.$switchLocalePath('ja') }}</p>
88
<p id="locale-path">{{ $nuxt.$localePath('nuxt-context-extension', 'nl') }}</p>
99
<p id="locale-route">{{ JSON.stringify($nuxt.$localeRoute()) }}</p>

src/runtime/composables/index.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ref } from 'vue'
33
import { runtimeDetectBrowserLanguage, wrapComposable } from '../internal'
44
import { localeCodes } from '#build/i18n.options.mjs'
55
import { _useLocaleHead, _useSetI18nParams } from '../routing/head'
6-
import { getRouteBaseName, localePath, localeRoute, switchLocalePath } from '../routing/routing'
6+
import { routeBaseName, localePath, localeRoute, switchLocalePath } from '../routing/routing'
77
import type { Ref } from 'vue'
88
import type { Locale } from 'vue-i18n'
99
import type { I18nHeadMetaInfo, I18nHeadOptions, SeoAttributesOptions } from '#internal-i18n-types'
@@ -13,6 +13,40 @@ import type { RouteLocationGenericPath, I18nRouteMeta } from '../types'
1313
export * from 'vue-i18n'
1414
export * from './shared'
1515

16+
declare module '#app' {
17+
interface NuxtApp {
18+
$localePath: LocalePathFunction
19+
$localeRoute: LocaleRouteFunction
20+
$routeBaseName: RouteBaseNameFunction
21+
$switchLocalePath: SwitchLocalePathFunction
22+
/**
23+
* @deprecated use {@link useLocaleHead} instead
24+
*/
25+
$localeHead: LocaleHeadFunction
26+
/**
27+
* @deprecated use {@link $routeBaseName} instead
28+
*/
29+
$getRouteBaseName: RouteBaseNameFunction
30+
}
31+
}
32+
33+
declare module 'vue' {
34+
interface ComponentCustomProperties {
35+
$localePath: LocalePathFunction
36+
$localeRoute: LocaleRouteFunction
37+
$routeBaseName: RouteBaseNameFunction
38+
$switchLocalePath: SwitchLocalePathFunction
39+
/**
40+
* @deprecated use {@link useLocaleHead} instead
41+
*/
42+
$localeHead: LocaleHeadFunction
43+
/**
44+
* @deprecated use {@link $routeBaseName} instead
45+
*/
46+
$getRouteBaseName: RouteBaseNameFunction
47+
}
48+
}
49+
1650
/**
1751
* Used to set i18n params for the current route.
1852
*
@@ -82,7 +116,7 @@ export type RouteBaseNameFunction = <Name extends keyof RouteMap = keyof RouteMa
82116
* Returns a {@link RouteBaseNameFunction} used get the base name of a route.
83117
*/
84118
export function useRouteBaseName(): RouteBaseNameFunction {
85-
return wrapComposable(getRouteBaseName)
119+
return wrapComposable(routeBaseName)
86120
}
87121

88122
/**

src/runtime/plugins/i18n.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import { getDefaultLocaleForDomain, setupMultiDomainLocales } from '../domain'
2929
import type { Locale, I18nOptions, Composer } from 'vue-i18n'
3030
import type { NuxtApp } from '#app'
3131
import type { LocaleObject, I18nPublicRuntimeConfig } from '#internal-i18n-types'
32-
import type { LocaleHeadFunction } from '../composables'
3332

3433
export default defineNuxtPlugin({
3534
name: 'i18n:plugin',
@@ -202,17 +201,11 @@ export default defineNuxtPlugin({
202201
*/
203202
Object.defineProperty(nuxt, '$i18n', { get: () => getI18nTarget(i18n) })
204203

205-
return {
206-
provide: {
207-
/**
208-
* TODO: remove type assertions while type narrowing based on generated types
209-
*/
210-
localeHead: wrapComposable(localeHead) as LocaleHeadFunction,
211-
localePath: useLocalePath(),
212-
localeRoute: useLocaleRoute(),
213-
getRouteBaseName: useRouteBaseName(),
214-
switchLocalePath: useSwitchLocalePath()
215-
}
216-
}
204+
nuxt.provide('localeHead', wrapComposable(localeHead))
205+
nuxt.provide('localePath', useLocalePath())
206+
nuxt.provide('localeRoute', useLocaleRoute())
207+
nuxt.provide('routeBaseName', useRouteBaseName())
208+
nuxt.provide('getRouteBaseName', useRouteBaseName())
209+
nuxt.provide('switchLocalePath', useSwitchLocalePath())
217210
}
218211
})

src/runtime/routing/head.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { joinURL, withQuery, type QueryValue } from 'ufo'
22
import { computed, getCurrentScope, onScopeDispose, ref, unref, useHead, useNuxtApp, watch, type Ref } from '#imports'
33
import { assign, isObject, isString } from '@intlify/shared'
44

5-
import { getRouteBaseName, localeRoute, switchLocalePath } from './routing'
5+
import { routeBaseName, localeRoute, switchLocalePath } from './routing'
66
import { getComposer } from '../compatibility'
77
import { toArray } from '../utils'
88
import { DYNAMIC_PARAMS_KEY } from '#build/i18n.options.mjs'
@@ -226,10 +226,7 @@ function getHreflangLinks(common: CommonComposableOptions, ctx: HeadContext) {
226226

227227
function getCanonicalUrl(common: CommonComposableOptions, ctx: HeadContext) {
228228
const route = common.router.currentRoute.value
229-
const currentRoute = localeRoute(
230-
common,
231-
assign({}, route, { path: undefined, name: getRouteBaseName(common, route) })
232-
)
229+
const currentRoute = localeRoute(common, assign({}, route, { path: undefined, name: routeBaseName(common, route) }))
233230

234231
if (!currentRoute) return ''
235232

@@ -245,10 +242,7 @@ function getCanonicalLink(common: CommonComposableOptions, ctx: HeadContext): Me
245242

246243
function getCanonicalQueryParams(common: CommonComposableOptions, ctx: HeadContext) {
247244
const route = common.router.currentRoute.value
248-
const currentRoute = localeRoute(
249-
common,
250-
assign({}, route, { path: undefined, name: getRouteBaseName(common, route) })
251-
)
245+
const currentRoute = localeRoute(common, assign({}, route, { path: undefined, name: routeBaseName(common, route) }))
252246

253247
const canonicalQueries = (isObject(ctx.seo) && ctx.seo?.canonicalQueries) || []
254248
const currentRouteQuery = currentRoute?.query || {}

0 commit comments

Comments
 (0)