From b22cb7f0d21270bd19bb01126a986390eb7f8fbe Mon Sep 17 00:00:00 2001 From: Inesh Bose <56732164+ineshbose@users.noreply.github.com> Date: Sat, 11 Mar 2023 16:33:55 +0000 Subject: [PATCH] chore: wrapping up --- docs/content/2.guide/15.migrating.md | 4 ++++ docs/content/2.guide/2.runtime-hooks.md | 2 ++ docs/content/4.API/3.vue-i18n.md | 12 ------------ specs/fixtures/plugins/i18nHooks.ts | 15 +++++++++++++++ .../{callbacks.spec.ts => runtime_hooks.spec.ts} | 4 ++-- src/options.d.ts | 2 -- src/runtime/types.d.ts | 4 ++-- src/runtime/utils.ts | 8 ++++---- src/types.ts | 9 --------- test/__snapshots__/gen.test.ts.snap | 6 +++--- 10 files changed, 32 insertions(+), 34 deletions(-) create mode 100644 specs/fixtures/plugins/i18nHooks.ts rename specs/{callbacks.spec.ts => runtime_hooks.spec.ts} (93%) diff --git a/docs/content/2.guide/15.migrating.md b/docs/content/2.guide/15.migrating.md index 95c7523e7..3ef507554 100644 --- a/docs/content/2.guide/15.migrating.md +++ b/docs/content/2.guide/15.migrating.md @@ -149,6 +149,10 @@ About details, See also [Lang Switcher](/api/lang-switcher#dynamic-route-paramet This option is no longer necessary, because i18n custom block is supported by [unplugin-vue-i18n](https://github.com/intlify/bundle-tools/tree/main/packages/unplugin-vue-i18n) **as default**. +### Deprecated `onBeforeLanguageSwitch` and `onLanguageSwitched` function options + +These functions can now be triggered using Nuxt runtime hooks. Please refer to [runtime hooks](/guide/runtime-hooks) to see how to use these. + ### Change some export APIs name on Nuxt context The following API will be changed to `$`: diff --git a/docs/content/2.guide/2.runtime-hooks.md b/docs/content/2.guide/2.runtime-hooks.md index 8a94d1ad3..2bff2fe73 100644 --- a/docs/content/2.guide/2.runtime-hooks.md +++ b/docs/content/2.guide/2.runtime-hooks.md @@ -17,6 +17,8 @@ Parameters: - **initialSetup**: set to `true` if it's the initial locale switch that triggers on app load. It's a special case since the locale is not technically set yet so we're switching from no locale to locale. - **context**: the Nuxt app +Returns: `string` or nothing + ### `i18n:localeSwitched` Called right after the app's locale has been switched. diff --git a/docs/content/4.API/3.vue-i18n.md b/docs/content/4.API/3.vue-i18n.md index 35a322d53..34be1b467 100644 --- a/docs/content/4.API/3.vue-i18n.md +++ b/docs/content/4.API/3.vue-i18n.md @@ -102,15 +102,3 @@ Object of the current locale properties. - **Type**: `boolean` Whether `differentDomains` option is enabled. - -### onBeforeLanguageSwitch - -- **Type**: `Function` - -See [callbacks](/guide/callbacks) - -### onLanguageSwitched - -- **Type**: `Function` - -See [callbacks](/guide/callbacks) diff --git a/specs/fixtures/plugins/i18nHooks.ts b/specs/fixtures/plugins/i18nHooks.ts new file mode 100644 index 000000000..21222e603 --- /dev/null +++ b/specs/fixtures/plugins/i18nHooks.ts @@ -0,0 +1,15 @@ +import { defineNuxtPlugin } from '#imports' + +export default defineNuxtPlugin(nuxtApp => { + nuxtApp.hook('i18n:beforeLocaleSwitch', ({ oldLocale, newLocale, initialSetup }) => { + console.log('onBeforeLanguageSwitch', oldLocale, newLocale, initialSetup) + + if (newLocale === 'en') { + return 'fr' + } + }) + + nuxtApp.hook('i18n:localeSwitched', ({ oldLocale, newLocale }) => { + console.log('onLanguageSwitched', oldLocale, newLocale) + }) +}) diff --git a/specs/callbacks.spec.ts b/specs/runtime_hooks.spec.ts similarity index 93% rename from specs/callbacks.spec.ts rename to specs/runtime_hooks.spec.ts index 47bce8885..f5e190207 100644 --- a/specs/callbacks.spec.ts +++ b/specs/runtime_hooks.spec.ts @@ -8,12 +8,12 @@ await setup({ browser: true, // overrides nuxtConfig: { - plugins: [fileURLToPath(new URL(`../playground/plugins/i18n.ts`, import.meta.url))], + plugins: [fileURLToPath(new URL(`./fixtures/plugins/i18nHooks.ts`, import.meta.url))], i18n: { defaultLocale: 'en' } } }) -describe('onBeforeLanguageSwitch / onLanguageSwitched', () => { +describe('beforeLocaleSwitch / localeSwitched', () => { test('', async () => { const home = url('/') const page = await createPage() diff --git a/src/options.d.ts b/src/options.d.ts index dc86c65c9..97db103ba 100644 --- a/src/options.d.ts +++ b/src/options.d.ts @@ -37,7 +37,5 @@ export { NuxtI18nInternalOptions, DetectBrowserLanguageOptions, RootRedirectOptions, - LanguageSwitchedHandler, - BeforeLanguageSwitchHandler, LanguageSwitchedHandler } from './types' diff --git a/src/runtime/types.d.ts b/src/runtime/types.d.ts index 30c318123..1fd4ef14e 100644 --- a/src/runtime/types.d.ts +++ b/src/runtime/types.d.ts @@ -26,7 +26,7 @@ type BeforeLanguageSwitchHandler = ( newLocale: string, initialSetup: boolean, context: NuxtApp -) => Promise +) => Promise /** * Called after the app's locale is switched. @@ -34,7 +34,7 @@ type BeforeLanguageSwitchHandler = ( * @param oldLocale - The app's locale before the switch * @param newLocale - The app's locale after the switch. */ -type LanguageSwitchedHandler = (oldLocale: string, newLocale: string) => Promise +type LanguageSwitchedHandler = (oldLocale: string, newLocale: string) => Promise export interface ComposerCustomProperties { /** diff --git a/src/runtime/utils.ts b/src/runtime/utils.ts index 37861c922..68c17ebd2 100644 --- a/src/runtime/utils.ts +++ b/src/runtime/utils.ts @@ -171,7 +171,7 @@ export async function loadAndSetLocale( } // call onBeforeLanguageSwitch - const localeOverride = onBeforeLanguageSwitch(i18n, oldLocale, newLocale, initial, context) + const localeOverride = await onBeforeLanguageSwitch(i18n, oldLocale, newLocale, initial, context) const localeCodes = getLocaleCodes(i18n) if (localeOverride && localeCodes && localeCodes.includes(localeOverride)) { if (localeOverride === oldLocale) { @@ -201,11 +201,11 @@ export async function loadAndSetLocale( // set the locale if (useCookie) { - setCookieLocale(i18n, newLocale) + await setCookieLocale(i18n, newLocale) } - setLocale(i18n, newLocale) + await setLocale(i18n, newLocale) - onLanguageSwitched(i18n, oldLocale, newLocale) + await onLanguageSwitched(i18n, oldLocale, newLocale) ret = true return [ret, oldLocale] diff --git a/src/types.ts b/src/types.ts index bc37d75ed..a18fbe4bc 100644 --- a/src/types.ts +++ b/src/types.ts @@ -36,15 +36,6 @@ export type CustomRoutePages = { } } -export type BeforeLanguageSwitchHandler = ( - oldLocale: string, - newLocale: string, - initialSetup: boolean, - context: Context -) => Promise - -export type LanguageSwitchedHandler = (oldLocale: string, newLocale: string) => Promise - export type NuxtI18nOptions = { differentDomains?: boolean detectBrowserLanguage?: DetectBrowserLanguageOptions | false diff --git a/test/__snapshots__/gen.test.ts.snap b/test/__snapshots__/gen.test.ts.snap index fe1dc18a9..6e701c88b 100644 --- a/test/__snapshots__/gen.test.ts.snap +++ b/test/__snapshots__/gen.test.ts.snap @@ -24,7 +24,7 @@ export const resolveNuxtI18nOptions = async (context) => { return nuxtI18nOptions } -export const nuxtI18nOptionsDefault = Object({vueI18n: undefined,locales: [],defaultLocale: \\"\\",defaultDirection: \\"ltr\\",routesNameSeparator: \\"___\\",trailingSlash: false,defaultLocaleRouteNameSuffix: \\"default\\",strategy: \\"prefix_except_default\\",lazy: false,langDir: null,rootRedirect: null,detectBrowserLanguage: Object({\\"alwaysRedirect\\":false,\\"cookieCrossOrigin\\":false,\\"cookieDomain\\":null,\\"cookieKey\\":\\"i18n_redirected\\",\\"cookieSecure\\":false,\\"fallbackLocale\\":\\"\\",\\"redirectOn\\":\\"root\\",\\"useCookie\\":true}),differentDomains: false,baseUrl: \\"\\",dynamicRouteParams: false,customRoutes: \\"page\\",pages: Object({}),skipSettingLocaleOnNavigate: false,onBeforeLanguageSwitch: (() => \\"\\"),onLanguageSwitched: (() => null),types: undefined,debug: false}) +export const nuxtI18nOptionsDefault = Object({vueI18n: undefined,locales: [],defaultLocale: \\"\\",defaultDirection: \\"ltr\\",routesNameSeparator: \\"___\\",trailingSlash: false,defaultLocaleRouteNameSuffix: \\"default\\",strategy: \\"prefix_except_default\\",lazy: false,langDir: null,rootRedirect: null,detectBrowserLanguage: Object({\\"alwaysRedirect\\":false,\\"cookieCrossOrigin\\":false,\\"cookieDomain\\":null,\\"cookieKey\\":\\"i18n_redirected\\",\\"cookieSecure\\":false,\\"fallbackLocale\\":\\"\\",\\"redirectOn\\":\\"root\\",\\"useCookie\\":true}),differentDomains: false,baseUrl: \\"\\",dynamicRouteParams: false,customRoutes: \\"page\\",pages: Object({}),skipSettingLocaleOnNavigate: false,types: undefined,debug: false}) export const nuxtI18nInternalOptions = Object({__normalizedLocales: [Object({\\"code\\":\\"en\\"})]}) export const NUXT_I18N_MODULE_ID = \\"@nuxtjs/i18n\\" @@ -141,7 +141,7 @@ export const resolveNuxtI18nOptions = async (context) => { return nuxtI18nOptions } -export const nuxtI18nOptionsDefault = Object({vueI18n: undefined,locales: [],defaultLocale: \\"\\",defaultDirection: \\"ltr\\",routesNameSeparator: \\"___\\",trailingSlash: false,defaultLocaleRouteNameSuffix: \\"default\\",strategy: \\"prefix_except_default\\",lazy: false,langDir: null,rootRedirect: null,detectBrowserLanguage: Object({\\"alwaysRedirect\\":false,\\"cookieCrossOrigin\\":false,\\"cookieDomain\\":null,\\"cookieKey\\":\\"i18n_redirected\\",\\"cookieSecure\\":false,\\"fallbackLocale\\":\\"\\",\\"redirectOn\\":\\"root\\",\\"useCookie\\":true}),differentDomains: false,baseUrl: \\"\\",dynamicRouteParams: false,customRoutes: \\"page\\",pages: Object({}),skipSettingLocaleOnNavigate: false,onBeforeLanguageSwitch: (() => \\"\\"),onLanguageSwitched: (() => null),types: undefined,debug: false}) +export const nuxtI18nOptionsDefault = Object({vueI18n: undefined,locales: [],defaultLocale: \\"\\",defaultDirection: \\"ltr\\",routesNameSeparator: \\"___\\",trailingSlash: false,defaultLocaleRouteNameSuffix: \\"default\\",strategy: \\"prefix_except_default\\",lazy: false,langDir: null,rootRedirect: null,detectBrowserLanguage: Object({\\"alwaysRedirect\\":false,\\"cookieCrossOrigin\\":false,\\"cookieDomain\\":null,\\"cookieKey\\":\\"i18n_redirected\\",\\"cookieSecure\\":false,\\"fallbackLocale\\":\\"\\",\\"redirectOn\\":\\"root\\",\\"useCookie\\":true}),differentDomains: false,baseUrl: \\"\\",dynamicRouteParams: false,customRoutes: \\"page\\",pages: Object({}),skipSettingLocaleOnNavigate: false,types: undefined,debug: false}) export const nuxtI18nInternalOptions = Object({__normalizedLocales: [Object({\\"code\\":\\"en\\"})]}) export const NUXT_I18N_MODULE_ID = \\"@nuxtjs/i18n\\" @@ -172,7 +172,7 @@ export const resolveNuxtI18nOptions = async (context) => { return nuxtI18nOptions } -export const nuxtI18nOptionsDefault = Object({vueI18n: undefined,locales: [],defaultLocale: \\"\\",defaultDirection: \\"ltr\\",routesNameSeparator: \\"___\\",trailingSlash: false,defaultLocaleRouteNameSuffix: \\"default\\",strategy: \\"prefix_except_default\\",lazy: false,langDir: null,rootRedirect: null,detectBrowserLanguage: Object({\\"alwaysRedirect\\":false,\\"cookieCrossOrigin\\":false,\\"cookieDomain\\":null,\\"cookieKey\\":\\"i18n_redirected\\",\\"cookieSecure\\":false,\\"fallbackLocale\\":\\"\\",\\"redirectOn\\":\\"root\\",\\"useCookie\\":true}),differentDomains: false,baseUrl: \\"\\",dynamicRouteParams: false,customRoutes: \\"page\\",pages: Object({}),skipSettingLocaleOnNavigate: false,onBeforeLanguageSwitch: (() => \\"\\"),onLanguageSwitched: (() => null),types: undefined,debug: false}) +export const nuxtI18nOptionsDefault = Object({vueI18n: undefined,locales: [],defaultLocale: \\"\\",defaultDirection: \\"ltr\\",routesNameSeparator: \\"___\\",trailingSlash: false,defaultLocaleRouteNameSuffix: \\"default\\",strategy: \\"prefix_except_default\\",lazy: false,langDir: null,rootRedirect: null,detectBrowserLanguage: Object({\\"alwaysRedirect\\":false,\\"cookieCrossOrigin\\":false,\\"cookieDomain\\":null,\\"cookieKey\\":\\"i18n_redirected\\",\\"cookieSecure\\":false,\\"fallbackLocale\\":\\"\\",\\"redirectOn\\":\\"root\\",\\"useCookie\\":true}),differentDomains: false,baseUrl: \\"\\",dynamicRouteParams: false,customRoutes: \\"page\\",pages: Object({}),skipSettingLocaleOnNavigate: false,types: undefined,debug: false}) export const nuxtI18nInternalOptions = Object({__normalizedLocales: [Object({\\"code\\":\\"en\\"})]}) export const NUXT_I18N_MODULE_ID = \\"@nuxtjs/i18n\\"