diff --git a/docs/content/docs/5.v9/2.guide/19.breaking-changes-in-v9.md b/docs/content/docs/5.v9/2.guide/19.breaking-changes-in-v9.md
index dac7ba993..7c558a050 100644
--- a/docs/content/docs/5.v9/2.guide/19.breaking-changes-in-v9.md
+++ b/docs/content/docs/5.v9/2.guide/19.breaking-changes-in-v9.md
@@ -64,3 +64,17 @@ Some properties have changed or swapped names to better fit their functionality,
## SEO - `useLocaleHead`
We have added a `addLangAttribute` property to the options parameter of `useLocaleHead` and `$localeHead`, originally this was not configurable on its own. If you want to keep the same behavior, if you were passing `addSeoAttributes`, you will also have to pass `addLangAttribute: true`. See [`useLocaleHead`](/docs/v9/api#useLocaleHead)
+
+## Nuxt context functions
+
+In v8 both the types and name of the injected context functions (such as `$localePath`, `$switchLocalePath` and [more](/docs/v9/api/nuxt)) did not work as intended. You may have found that these functions worked when using them without prefix (`$`) even when not assigning these functions from a composable.
+
+The types and names have been fixed in v9, if you have been using the unprefixed functions globally (without composable) in your project you will have to prefix these functions as they were originally intended.
+
+- `getRouteBaseName` -> `$getRouteBaseName`
+- `resolveRoute` -> `$resolveRoute`
+- `localePath` -> `$localePath`
+- `localeRoute` -> `$localeRoute`
+- `localeLocation` -> `$localeLocation`
+- `switchLocalePath` -> `$switchLocalePath`
+- `localeHead` -> `$localeHead`
diff --git a/playground/pages/[...catch].vue b/playground/pages/[...catch].vue
index c1f1b483f..8c99d4bbd 100644
--- a/playground/pages/[...catch].vue
+++ b/playground/pages/[...catch].vue
@@ -19,7 +19,7 @@ definePageMeta({
This page is catch all: '{{ route.params }}'
- {{ locale.name }} |
+ {{ locale.name }} |
diff --git a/playground/pages/about/index.vue b/playground/pages/about/index.vue
index 6c64a43e5..85f73ca60 100644
--- a/playground/pages/about/index.vue
+++ b/playground/pages/about/index.vue
@@ -40,11 +40,11 @@ export default defineComponent({
- Back to Home
+ Back to Home
- {{ locale.name }} |
+ {{ locale.name }} |
hello
@@ -54,6 +54,6 @@ export default defineComponent({
{{ switchableLocale }}
-
{{ localeHead({ addSeoAttributes: true }) }}
+
{{ $localeHead({ addSeoAttributes: true }) }}
diff --git a/playground/pages/category/[id].vue b/playground/pages/category/[id].vue
index fb326165d..e8a3005dd 100644
--- a/playground/pages/category/[id].vue
+++ b/playground/pages/category/[id].vue
@@ -19,7 +19,7 @@ definePageMeta({
This is cateory page on '{{ route.params.id }}'
- {{ locale.name }} |
+ {{ locale.name }} |
nuxtjs/i18n
diff --git a/specs/fixtures/basic/pages/blog/index.vue b/specs/fixtures/basic/pages/blog/index.vue
index 66f7e537d..d6f64a820 100644
--- a/specs/fixtures/basic/pages/blog/index.vue
+++ b/specs/fixtures/basic/pages/blog/index.vue
@@ -1,6 +1,6 @@
This is blog index page
-
article
+
article
diff --git a/specs/fixtures/basic/pages/define-i18n-route-false.vue b/specs/fixtures/basic/pages/define-i18n-route-false.vue
index 19f86c8dd..ef1380a9c 100644
--- a/specs/fixtures/basic/pages/define-i18n-route-false.vue
+++ b/specs/fixtures/basic/pages/define-i18n-route-false.vue
@@ -1,7 +1,7 @@
Page with disabled localized route
- Goto Home
+ Goto Home
diff --git a/specs/fixtures/different_domains/pages/blog/index.vue b/specs/fixtures/different_domains/pages/blog/index.vue
index 66f7e537d..d6f64a820 100644
--- a/specs/fixtures/different_domains/pages/blog/index.vue
+++ b/specs/fixtures/different_domains/pages/blog/index.vue
@@ -1,6 +1,6 @@
This is blog index page
-
article
+
article
diff --git a/specs/fixtures/multi_domains_locales/pages/blog/index.vue b/specs/fixtures/multi_domains_locales/pages/blog/index.vue
index 66f7e537d..d6f64a820 100644
--- a/specs/fixtures/multi_domains_locales/pages/blog/index.vue
+++ b/specs/fixtures/multi_domains_locales/pages/blog/index.vue
@@ -1,6 +1,6 @@
This is blog index page
-
article
+
article
diff --git a/src/runtime/injections.ts b/src/runtime/injections.ts
new file mode 100644
index 000000000..edad24c37
--- /dev/null
+++ b/src/runtime/injections.ts
@@ -0,0 +1,68 @@
+import type {
+ LocaleHeadFunction,
+ LocalePathFunction,
+ LocaleRouteFunction,
+ RouteBaseNameFunction,
+ SwitchLocalePathFunction
+} from '#i18n'
+import type {
+ getRouteBaseName,
+ localeHead,
+ localePath,
+ localeRoute,
+ switchLocalePath
+} from './routing/compatibles/index'
+
+export type NuxtI18nPluginInjections = {
+ /**
+ * Returns base name of current (if argument not provided) or passed in route.
+ *
+ * @remarks
+ * Base name is name of the route without locale suffix and other metadata added by nuxt i18n module
+ *
+ * @param givenRoute - A route.
+ *
+ * @returns The route base name. if cannot get, `undefined` is returned.
+ */
+ getRouteBaseName: (...args: Parameters) => ReturnType
+ /**
+ * Returns localized path for passed in route.
+ *
+ * @remarks
+ * If locale is not specified, uses current locale.
+ *
+ * @param route - A route.
+ * @param locale - A locale, optional.
+ *
+ * @returns A path of the current route.
+ */
+ localePath: (...args: Parameters) => ReturnType
+ /**
+ * Returns localized route for passed in `route` parameters.
+ *
+ * @remarks
+ * If `locale` is not specified, uses current locale.
+ *
+ * @param route - A route.
+ * @param locale - A {@link Locale | locale}, optional.
+ *
+ * @returns A route. if cannot resolve, `undefined` is returned.
+ */
+ localeRoute: (...args: Parameters) => ReturnType
+ /**
+ * Returns localized head properties for locale-related aspects.
+ *
+ * @param options - An options object, see `I18nHeadOptions`.
+ *
+ * @returns The localized head properties.
+ */
+ localeHead: (...args: Parameters) => ReturnType
+ /**
+ * Returns path of the current route for specified locale
+ *
+ * @param locale - A {@link Locale}
+ *
+ * @returns A path of the current route
+ */
+ switchLocalePath: (...args: Parameters) => ReturnType
+}
diff --git a/src/runtime/plugins/i18n.ts b/src/runtime/plugins/i18n.ts
index 368afcfa2..71663dc2b 100644
--- a/src/runtime/plugins/i18n.ts
+++ b/src/runtime/plugins/i18n.ts
@@ -33,18 +33,24 @@ import { extendI18n, createLocaleFromRouteGetter } from '../routing/extends'
import { setLocale, getLocale, mergeLocaleMessage, setLocaleProperty } from '../compatibility'
import { createLogger } from 'virtual:nuxt-i18n-logger'
+import type { NuxtI18nPluginInjections } from '../injections'
import type { Locale, I18nOptions } from 'vue-i18n'
import type { NuxtApp } from '#app'
-import type { getRouteBaseName, localePath, localeRoute, switchLocalePath, localeHead } from '../routing/compatibles'
-import type {
- LocaleHeadFunction,
- LocalePathFunction,
- LocaleRouteFunction,
- RouteBaseNameFunction,
- SwitchLocalePathFunction
-} from '../composables'
-
-export default defineNuxtPlugin({
+
+// from https://github.com/nuxt/nuxt/blob/2466af53b0331cdb8b17c2c3b08675c5985deaf3/packages/nuxt/src/core/templates.ts#L152
+type Decorate> = { [K in keyof T as K extends string ? `$${K}` : never]: T[K] }
+
+// TODO: use @nuxt/module-builder to stub/prepare types
+declare module '#app' {
+ interface NuxtApp extends Decorate {}
+}
+
+// declare module 'vue' {
+// interface ComponentCustomProperties extends Decorate {}
+// }
+
+// `NuxtI18nPluginInjections` should not have properties prefixed with `$`
+export default defineNuxtPlugin({
name: 'i18n:plugin',
parallel: parallelPlugin,
async setup(nuxt) {
@@ -383,59 +389,3 @@ export default defineNuxtPlugin({
)
}
})
-
-declare module '#app' {
- interface NuxtApp {
- /**
- * Returns base name of current (if argument not provided) or passed in route.
- *
- * @remarks
- * Base name is name of the route without locale suffix and other metadata added by nuxt i18n module
- *
- * @param givenRoute - A route.
- *
- * @returns The route base name. if cannot get, `undefined` is returned.
- */
- $getRouteBaseName: (...args: Parameters) => ReturnType
- /**
- * Returns localized path for passed in route.
- *
- * @remarks
- * If locale is not specified, uses current locale.
- *
- * @param route - A route.
- * @param locale - A locale, optional.
- *
- * @returns A path of the current route.
- */
- $localePath: (...args: Parameters) => ReturnType
- /**
- * Returns localized route for passed in `route` parameters.
- *
- * @remarks
- * If `locale` is not specified, uses current locale.
- *
- * @param route - A route.
- * @param locale - A {@link Locale | locale}, optional.
- *
- * @returns A route. if cannot resolve, `undefined` is returned.
- */
- $localeRoute: (...args: Parameters) => ReturnType
- /**
- * Returns localized head properties for locale-related aspects.
- *
- * @param options - An options object, see `I18nHeadOptions`.
- *
- * @returns The localized head properties.
- */
- $localeHead: (...args: Parameters) => ReturnType
- /**
- * Returns path of the current route for specified locale
- *
- * @param locale - A {@link Locale}
- *
- * @returns A path of the current route
- */
- $switchLocalePath: (...args: Parameters) => ReturnType
- }
-}
diff --git a/src/runtime/routing/extends/i18n.ts b/src/runtime/routing/extends/i18n.ts
index 22be352f2..7fed6c79e 100644
--- a/src/runtime/routing/extends/i18n.ts
+++ b/src/runtime/routing/extends/i18n.ts
@@ -89,13 +89,13 @@ export function extendI18n(i18n: I18n, { extendComposer, extendComposerInstance
const common = initCommonComposableOptions(i18n)
app.mixin({
methods: {
- getRouteBaseName: wrapComposable(getRouteBaseName, common),
- resolveRoute: wrapComposable(resolveRoute, common),
- localePath: wrapComposable(localePath, common),
- localeRoute: wrapComposable(localeRoute, common),
- localeLocation: wrapComposable(localeLocation, common),
- switchLocalePath: wrapComposable(switchLocalePath, common),
- localeHead: wrapComposable(localeHead, common)
+ $getRouteBaseName: wrapComposable(getRouteBaseName, common),
+ $resolveRoute: wrapComposable(resolveRoute, common),
+ $localePath: wrapComposable(localePath, common),
+ $localeRoute: wrapComposable(localeRoute, common),
+ $localeLocation: wrapComposable(localeLocation, common),
+ $switchLocalePath: wrapComposable(switchLocalePath, common),
+ $localeHead: wrapComposable(localeHead, common)
}
})
}