From 4099917101c6461d7196833aa726c814ca7b65e8 Mon Sep 17 00:00:00 2001 From: Pierre-Michel Brown Date: Sun, 15 Nov 2020 10:16:48 +0100 Subject: [PATCH 1/7] Add option to skip setting locale on navigate Useful if you want to wait for the page transition to enter before setting the locale --- src/helpers/constants.js | 1 + src/templates/plugin.main.js | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/helpers/constants.js b/src/helpers/constants.js index d5c371fe6..edf8dbab2 100644 --- a/src/helpers/constants.js +++ b/src/helpers/constants.js @@ -54,6 +54,7 @@ exports.DEFAULT_OPTIONS = { }, parsePages: true, pages: {}, + skipSettingLocaleOnNavigate: false, beforeLanguageSwitch: () => null, onLanguageSwitched: () => null } diff --git a/src/templates/plugin.main.js b/src/templates/plugin.main.js index 107145dc9..53d5f08c9 100644 --- a/src/templates/plugin.main.js +++ b/src/templates/plugin.main.js @@ -18,6 +18,7 @@ import { onLanguageSwitched, rootRedirect, routesNameSeparator, + skipSettingLocaleOnNavigate, STRATEGIES, strategy, vueI18n, @@ -72,11 +73,12 @@ export default async (context) => { return } - // Abort if newLocale did not change - if (newLocale === app.i18n.locale) { + // Abort if newLocale is falsy or did not change + if (!newLocale || newLocale === app.i18n.locale) { return } + app.i18n.__pendingLocale = null const oldLocale = app.i18n.locale if (!initialSetup) { @@ -196,7 +198,11 @@ export default async (context) => { (detectBrowserLanguage && doDetectBrowserLanguage(route)) || getLocaleFromRoute(route) || app.i18n.locale || app.i18n.defaultLocale || '' - await app.i18n.setLocale(finalLocale) + if (skipSettingLocaleOnNavigate) { + app.i18n.__pendingLocale = finalLocale + } else { + await app.i18n.setLocale(finalLocale) + } return [null, null] } @@ -261,6 +267,7 @@ export default async (context) => { i18n.setLocaleCookie = locale => setLocaleCookie(locale, res, { useCookie, cookieDomain, cookieKey, cookieSecure, cookieCrossOrigin }) i18n.getLocaleCookie = () => getLocaleCookie(req, { useCookie, cookieKey, localeCodes }) i18n.setLocale = (locale) => loadAndSetLocale(locale) + i18n.setPendingLocale = () => loadAndSetLocale(app.i18n.__pendingLocale) i18n.getBrowserLocale = () => getBrowserLocale() i18n.__baseUrl = app.i18n.__baseUrl } From 578f42ace279f768ecdc98ddf8f6ca27a02d828f Mon Sep 17 00:00:00 2001 From: Pierre-Michel Brown Date: Sun, 15 Nov 2020 15:28:08 +0100 Subject: [PATCH 2/7] Add helper to wait for pending locale --- src/templates/plugin.main.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/templates/plugin.main.js b/src/templates/plugin.main.js index 53d5f08c9..bbfa7afa6 100644 --- a/src/templates/plugin.main.js +++ b/src/templates/plugin.main.js @@ -73,12 +73,11 @@ export default async (context) => { return } - // Abort if newLocale is falsy or did not change - if (!newLocale || newLocale === app.i18n.locale) { + // Abort if newLocale did not change + if (newLocale === app.i18n.locale) { return } - app.i18n.__pendingLocale = null const oldLocale = app.i18n.locale if (!initialSetup) { @@ -200,6 +199,9 @@ export default async (context) => { if (skipSettingLocaleOnNavigate) { app.i18n.__pendingLocale = finalLocale + app.i18n.__pendingLocalePromise = new Promise(resolve => { + app.i18n.__resolvePendingLocalePromise = resolve + }) } else { await app.i18n.setLocale(finalLocale) } @@ -207,6 +209,19 @@ export default async (context) => { return [null, null] } + const setPendingLocale = async () => { + if (!app.i18n.__pendingLocale) { + return + } + await app.i18n.setLocale(app.i18n.__pendingLocale) + app.i18n.__resolvePendingLocalePromise() + app.i18n.__pendingLocale = null + } + + const waitForPendingLocale = async () => { + return !app.i18n.__pendingLocale || (await app.i18n.__pendingLocalePromise) + } + const getBrowserLocale = () => { if (process.client && typeof navigator !== 'undefined' && navigator.languages) { // Get browser language either from navigator if running on client side, or from the headers @@ -267,7 +282,8 @@ export default async (context) => { i18n.setLocaleCookie = locale => setLocaleCookie(locale, res, { useCookie, cookieDomain, cookieKey, cookieSecure, cookieCrossOrigin }) i18n.getLocaleCookie = () => getLocaleCookie(req, { useCookie, cookieKey, localeCodes }) i18n.setLocale = (locale) => loadAndSetLocale(locale) - i18n.setPendingLocale = () => loadAndSetLocale(app.i18n.__pendingLocale) + i18n.setPendingLocale = setPendingLocale + i18n.waitForPendingLocale = waitForPendingLocale i18n.getBrowserLocale = () => getBrowserLocale() i18n.__baseUrl = app.i18n.__baseUrl } From 03af633e50b80e09ed8b41de0c19720132a51c3d Mon Sep 17 00:00:00 2001 From: Pierre-Michel Brown Date: Tue, 17 Nov 2020 15:42:44 +0100 Subject: [PATCH 3/7] Address feedback on pending locale --- src/templates/plugin.main.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/templates/plugin.main.js b/src/templates/plugin.main.js index bbfa7afa6..8e0bd47a4 100644 --- a/src/templates/plugin.main.js +++ b/src/templates/plugin.main.js @@ -219,7 +219,9 @@ export default async (context) => { } const waitForPendingLocale = async () => { - return !app.i18n.__pendingLocale || (await app.i18n.__pendingLocalePromise) + if (app.i18n.__pendingLocale) { + await app.i18n.__pendingLocalePromise + } } const getBrowserLocale = () => { @@ -286,6 +288,9 @@ export default async (context) => { i18n.waitForPendingLocale = waitForPendingLocale i18n.getBrowserLocale = () => getBrowserLocale() i18n.__baseUrl = app.i18n.__baseUrl + i18n.__pendingLocale = app.i18n.__pendingLocale + i18n.__pendingLocalePromise = app.i18n.__pendingLocalePromise + i18n.__resolvePendingLocalePromise = app.i18n.__resolvePendingLocalePromise } // Set instance options From 3f12610932ac17ab53cc6d0d4f4f189b52aee02d Mon Sep 17 00:00:00 2001 From: Pierre-Michel Brown Date: Mon, 11 Jan 2021 16:37:02 +0100 Subject: [PATCH 4/7] Update documentation for skipSettingLocaleOnNavigate --- docs/content/en/api.md | 16 +++++++++++ docs/content/en/lang-switcher.md | 43 ++++++++++++++++++++++++++++ docs/content/en/options-reference.md | 7 +++++ docs/content/es/api.md | 16 +++++++++++ docs/content/es/lang-switcher.md | 43 ++++++++++++++++++++++++++++ docs/content/es/options-reference.md | 7 +++++ 6 files changed, 132 insertions(+) diff --git a/docs/content/en/api.md b/docs/content/en/api.md index e570280b6..acfe6b166 100644 --- a/docs/content/en/api.md +++ b/docs/content/en/api.md @@ -108,6 +108,22 @@ Instance of [VueI18n class](http://kazupon.github.io/vue-i18n/api/#vuei18n-class Switches locale of the app to specified locale code. If `useCookie` option is enabled, locale cookie will be updated with new value. If prefixes are enabled (`strategy` other than `no_prefix`), will navigate to new locale's route. +#### setPendingLocale + + - **Arguments**: + - no arguments + - **Returns**: `Promise` + + Switches to the pending locale that would have been set on navigate, but was prevented by the option [`skipSettingLocaleOnNavigate`](./options-reference#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). + +#### waitForPendingLocale + + - **Arguments**: + - no arguments + - **Returns**: `Promise` + + Returns a promise that will be resolved once the pending locale is set. + #### getBrowserLocale - **Arguments**: diff --git a/docs/content/en/lang-switcher.md b/docs/content/en/lang-switcher.md index 0602357bb..da769726c 100644 --- a/docs/content/en/lang-switcher.md +++ b/docs/content/en/lang-switcher.md @@ -98,3 +98,46 @@ export default { **nuxt-i18n** won't reset parameters translations for you, this means that if you use identical parameters for different routes, navigating between those routes might result in conflicting parameters. Make sure you always set params translations in such cases. + +## Wait for page transition + +By default, the locale will be changed right away when calling [`switchLocalePath(path)`](./api#switchlocalepath) which means that if you have a page transition, it will fade out the page with the text already switched to the new language and fade back in with the same content. + +To work around the issue, you can set the option [`skipSettingLocaleOnNavigate`](./options-reference#skipsettinglocaleonnavigate) to `true` and handle setting the locale yourself from a `beforeEnter` transition hook defined in a plugin. + +```js{}[nuxt.config.js] +export default { + plugins: ['~/plugins/router'], + + i18n: { + skipSettingLocaleOnNavigate: true, + } +} +``` + +```js{}[plugins/router.js] +export default ({ app }) => { + app.nuxt.defaultTransition.beforeEnter = () => { + app.i18n.setPendingLocale() + } + + // Optional: wait for locale before scrolling for a smoother transition + app.router.options.scrollBehavior = async (to, from, savedPosition) => { + // Make sure the route has changed + if (to.name !== from.name) await app.i18n.waitForPendingLocale() + return savedPosition || { x: 0, y: 0 } + } +} +``` + +If you have a specific transition defined in a page component, you would also need to call `setPendingLocale` from there. + +```js{}[pages/foo.vue] +export default { + transition: { + beforeEnter() { + this.$i18n.setPendingLocale() + } + } +} +``` diff --git a/docs/content/en/options-reference.md b/docs/content/en/options-reference.md index 70457dd34..c439a6b65 100644 --- a/docs/content/en/options-reference.md +++ b/docs/content/en/options-reference.md @@ -275,6 +275,13 @@ A listener called after app's locale has changed. Internal suffix added to generated route names for default locale, if strategy is `prefix_and_default`. You shouldn't need to change this. +## `skipSettingLocaleOnNavigate` + +- type: `boolean` +- default: `false` + +If true, the locale will not be set when navigating to a new locale. This is useful if you want to wait for the page transition to end before setting the locale yourself using [`setPendingLocale`](./api#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). + ## `routesNameSeparator` - type: `string` diff --git a/docs/content/es/api.md b/docs/content/es/api.md index bcd0acf40..c9caa5a22 100644 --- a/docs/content/es/api.md +++ b/docs/content/es/api.md @@ -108,6 +108,22 @@ Instance of [VueI18n class](http://kazupon.github.io/vue-i18n/api/#vuei18n-class Switches locale of the app to specified locale code. If `useCookie` option is enabled, locale cookie will be updated with new value. If prefixes are enabled (`strategy` other than `no_prefix`), will navigate to new locale's route. +#### setPendingLocale + + - **Arguments**: + - no arguments + - **Returns**: `Promise` + + Switches to the pending locale that would have been set on navigate, but was prevented by the option [`skipSettingLocaleOnNavigate`](./options-reference#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). + +#### waitForPendingLocale + + - **Arguments**: + - no arguments + - **Returns**: `Promise` + + Returns a promise that will be resolved once the pending locale is set. + #### getBrowserLocale - **Parámetros**: diff --git a/docs/content/es/lang-switcher.md b/docs/content/es/lang-switcher.md index 1f8814e0d..72ac22cd0 100644 --- a/docs/content/es/lang-switcher.md +++ b/docs/content/es/lang-switcher.md @@ -96,3 +96,46 @@ export default { **nuxt-i18n** no restablecerá las traducciones de parámetros por usted, esto significa que si utiliza parámetros idénticos para diferentes rutas, navegar entre esas rutas podría generar parámetros conflictivos. Asegúrese de establecer siempre traducciones de parámetros en tales casos. + +## Wait for page transition + +By default, the locale will be changed right away when calling [`switchLocalePath(path)`](./api#switchlocalepath) which means that if you have a page transition, it will fade out the page with the text already switched to the new language and fade back in with the same content. + +To work around the issue, you can set the option [`skipSettingLocaleOnNavigate`](./options-reference#skipsettinglocaleonnavigate) to `true` and handle setting the locale yourself from a `beforeEnter` transition hook defined in a plugin. + +```js{}[nuxt.config.js] +export default { + plugins: ['~/plugins/router'], + + i18n: { + skipSettingLocaleOnNavigate: true, + } +} +``` + +```js{}[plugins/router.js] +export default ({ app }) => { + app.nuxt.defaultTransition.beforeEnter = () => { + app.i18n.setPendingLocale() + } + + // Optional: wait for locale before scrolling for a smoother transition + app.router.options.scrollBehavior = async (to, from, savedPosition) => { + // Make sure the route has changed + if (to.name !== from.name) await app.i18n.waitForPendingLocale() + return savedPosition || { x: 0, y: 0 } + } +} +``` + +If you have a specific transition defined in a page component, you would also need to call `setPendingLocale` from there. + +```js{}[pages/foo.vue] +export default { + transition: { + beforeEnter() { + this.$i18n.setPendingLocale() + } + } +} +``` diff --git a/docs/content/es/options-reference.md b/docs/content/es/options-reference.md index f2bc1976e..9359e0bf5 100644 --- a/docs/content/es/options-reference.md +++ b/docs/content/es/options-reference.md @@ -273,6 +273,13 @@ A listener called after app's locale has changed. Internal suffix added to generated route names for default locale, if strategy is `prefix_and_default`. You shouldn't need to change this. +## `skipSettingLocaleOnNavigate` + +- type: `boolean` +- default: `false` + +If true, the locale will not be set when navigating to a new locale. This is useful if you want to wait for the page transition to end before setting the locale yourself using [`setPendingLocale`](./api#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). + ## `routesNameSeparator` - type: `string` From 032953c899b879786a47e970ccea582cd3d9a356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ch=C5=82odnicki?= Date: Tue, 2 Feb 2021 23:36:08 +0100 Subject: [PATCH 5/7] minor formatting changes + added types --- docs/content/en/api.md | 16 ++++++++-------- docs/content/en/lang-switcher.md | 13 ++++++++----- docs/content/en/options-reference.md | 14 +++++++------- docs/content/es/api.md | 20 ++++++++++---------- docs/content/es/lang-switcher.md | 13 ++++++++----- docs/content/es/options-reference.md | 14 +++++++------- src/templates/plugin.main.js | 2 +- types/nuxt-i18n.d.ts | 1 + types/vue.d.ts | 10 ++++++---- 9 files changed, 56 insertions(+), 47 deletions(-) diff --git a/docs/content/en/api.md b/docs/content/en/api.md index acfe6b166..0fcaa2c15 100644 --- a/docs/content/en/api.md +++ b/docs/content/en/api.md @@ -108,29 +108,29 @@ Instance of [VueI18n class](http://kazupon.github.io/vue-i18n/api/#vuei18n-class Switches locale of the app to specified locale code. If `useCookie` option is enabled, locale cookie will be updated with new value. If prefixes are enabled (`strategy` other than `no_prefix`), will navigate to new locale's route. -#### setPendingLocale +#### getBrowserLocale - **Arguments**: - no arguments - - **Returns**: `Promise` + - **Returns**: `string | undefined` - Switches to the pending locale that would have been set on navigate, but was prevented by the option [`skipSettingLocaleOnNavigate`](./options-reference#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). + Returns browser locale code filtered against the ones defined in options. -#### waitForPendingLocale +#### setPendingLocale v6.20.0+ - **Arguments**: - no arguments - **Returns**: `Promise` - Returns a promise that will be resolved once the pending locale is set. + Switches to the pending locale that would have been set on navigate, but was prevented by the option [`skipSettingLocaleOnNavigate`](./options-reference#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). -#### getBrowserLocale +#### waitForPendingLocale v6.20.0+ - **Arguments**: - no arguments - - **Returns**: `string | undefined` + - **Returns**: `Promise` - Returns browser locale code filtered against the ones defined in options. + Returns a promise that will be resolved once the pending locale is set. ### Properties diff --git a/docs/content/en/lang-switcher.md b/docs/content/en/lang-switcher.md index da769726c..d60f25695 100644 --- a/docs/content/en/lang-switcher.md +++ b/docs/content/en/lang-switcher.md @@ -101,21 +101,22 @@ export default { ## Wait for page transition -By default, the locale will be changed right away when calling [`switchLocalePath(path)`](./api#switchlocalepath) which means that if you have a page transition, it will fade out the page with the text already switched to the new language and fade back in with the same content. +By default, the locale will be changed right away when navigating to a route with a different locale which means that if you have a page transition, it will fade out the page with the text already switched to the new language and fade back in with the same content. To work around the issue, you can set the option [`skipSettingLocaleOnNavigate`](./options-reference#skipsettinglocaleonnavigate) to `true` and handle setting the locale yourself from a `beforeEnter` transition hook defined in a plugin. -```js{}[nuxt.config.js] +```js {}[nuxt.config.js] export default { plugins: ['~/plugins/router'], i18n: { + // ... your other options skipSettingLocaleOnNavigate: true, } } ``` -```js{}[plugins/router.js] +```js {}[~/plugins/router.js] export default ({ app }) => { app.nuxt.defaultTransition.beforeEnter = () => { app.i18n.setPendingLocale() @@ -124,7 +125,9 @@ export default ({ app }) => { // Optional: wait for locale before scrolling for a smoother transition app.router.options.scrollBehavior = async (to, from, savedPosition) => { // Make sure the route has changed - if (to.name !== from.name) await app.i18n.waitForPendingLocale() + if (to.name !== from.name) { + await app.i18n.waitForPendingLocale() + } return savedPosition || { x: 0, y: 0 } } } @@ -132,7 +135,7 @@ export default ({ app }) => { If you have a specific transition defined in a page component, you would also need to call `setPendingLocale` from there. -```js{}[pages/foo.vue] +```js {}[~/pages/foo.vue] export default { transition: { beforeEnter() { diff --git a/docs/content/en/options-reference.md b/docs/content/en/options-reference.md index c439a6b65..2e6f44ca1 100644 --- a/docs/content/en/options-reference.md +++ b/docs/content/en/options-reference.md @@ -268,6 +268,13 @@ A listener called right before app's locale changes. A listener called after app's locale has changed. +## `skipSettingLocaleOnNavigate` v6.20.0+ + +- type: `boolean` +- default: `false` + +If `true`, the locale will not be set when navigating to a new locale. This is useful if you want to wait for the page transition to end before setting the locale yourself using [`setPendingLocale`](./api#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). + ## `defaultLocaleRouteNameSuffix` - type: `string` @@ -275,13 +282,6 @@ A listener called after app's locale has changed. Internal suffix added to generated route names for default locale, if strategy is `prefix_and_default`. You shouldn't need to change this. -## `skipSettingLocaleOnNavigate` - -- type: `boolean` -- default: `false` - -If true, the locale will not be set when navigating to a new locale. This is useful if you want to wait for the page transition to end before setting the locale yourself using [`setPendingLocale`](./api#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). - ## `routesNameSeparator` - type: `string` diff --git a/docs/content/es/api.md b/docs/content/es/api.md index c9caa5a22..8ee44264b 100644 --- a/docs/content/es/api.md +++ b/docs/content/es/api.md @@ -108,7 +108,15 @@ Instance of [VueI18n class](http://kazupon.github.io/vue-i18n/api/#vuei18n-class Switches locale of the app to specified locale code. If `useCookie` option is enabled, locale cookie will be updated with new value. If prefixes are enabled (`strategy` other than `no_prefix`), will navigate to new locale's route. -#### setPendingLocale +#### getBrowserLocale + + - **Parámetros**: + - sin parámetros + - **Devuelve**: `string | undefined` + + Devuelve el código del idioma que utiliza el navegador, filtrado según los códigos definidos en las opciones. + +#### setPendingLocale v6.20.0+ - **Arguments**: - no arguments @@ -116,7 +124,7 @@ Instance of [VueI18n class](http://kazupon.github.io/vue-i18n/api/#vuei18n-class Switches to the pending locale that would have been set on navigate, but was prevented by the option [`skipSettingLocaleOnNavigate`](./options-reference#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). -#### waitForPendingLocale +#### waitForPendingLocale v6.20.0+ - **Arguments**: - no arguments @@ -124,14 +132,6 @@ Instance of [VueI18n class](http://kazupon.github.io/vue-i18n/api/#vuei18n-class Returns a promise that will be resolved once the pending locale is set. -#### getBrowserLocale - - - **Parámetros**: - - sin parámetros - - **Devuelve**: `string | undefined` - - Devuelve el código del idioma que utiliza el navegador, filtrado según los códigos definidos en las opciones. - ### Properties #### defaultDirection v6.19.0+ diff --git a/docs/content/es/lang-switcher.md b/docs/content/es/lang-switcher.md index 72ac22cd0..92a65e7b9 100644 --- a/docs/content/es/lang-switcher.md +++ b/docs/content/es/lang-switcher.md @@ -99,21 +99,22 @@ export default { ## Wait for page transition -By default, the locale will be changed right away when calling [`switchLocalePath(path)`](./api#switchlocalepath) which means that if you have a page transition, it will fade out the page with the text already switched to the new language and fade back in with the same content. +By default, the locale will be changed right away when navigating to a route with a different locale which means that if you have a page transition, it will fade out the page with the text already switched to the new language and fade back in with the same content. To work around the issue, you can set the option [`skipSettingLocaleOnNavigate`](./options-reference#skipsettinglocaleonnavigate) to `true` and handle setting the locale yourself from a `beforeEnter` transition hook defined in a plugin. -```js{}[nuxt.config.js] +```js {}[nuxt.config.js] export default { plugins: ['~/plugins/router'], i18n: { + // ... your other options skipSettingLocaleOnNavigate: true, } } ``` -```js{}[plugins/router.js] +```js {}[~/plugins/router.js] export default ({ app }) => { app.nuxt.defaultTransition.beforeEnter = () => { app.i18n.setPendingLocale() @@ -122,7 +123,9 @@ export default ({ app }) => { // Optional: wait for locale before scrolling for a smoother transition app.router.options.scrollBehavior = async (to, from, savedPosition) => { // Make sure the route has changed - if (to.name !== from.name) await app.i18n.waitForPendingLocale() + if (to.name !== from.name) { + await app.i18n.waitForPendingLocale() + } return savedPosition || { x: 0, y: 0 } } } @@ -130,7 +133,7 @@ export default ({ app }) => { If you have a specific transition defined in a page component, you would also need to call `setPendingLocale` from there. -```js{}[pages/foo.vue] +```js {}[~/pages/foo.vue] export default { transition: { beforeEnter() { diff --git a/docs/content/es/options-reference.md b/docs/content/es/options-reference.md index 9359e0bf5..0d9efbaea 100644 --- a/docs/content/es/options-reference.md +++ b/docs/content/es/options-reference.md @@ -266,6 +266,13 @@ A listener called right before app's locale changes. A listener called after app's locale has changed. +## `skipSettingLocaleOnNavigate` v6.20.0+ + +- type: `boolean` +- default: `false` + +If true, the locale will not be set when navigating to a new locale. This is useful if you want to wait for the page transition to end before setting the locale yourself using [`setPendingLocale`](./api#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). + ## `defaultLocaleRouteNameSuffix` - type: `string` @@ -273,13 +280,6 @@ A listener called after app's locale has changed. Internal suffix added to generated route names for default locale, if strategy is `prefix_and_default`. You shouldn't need to change this. -## `skipSettingLocaleOnNavigate` - -- type: `boolean` -- default: `false` - -If true, the locale will not be set when navigating to a new locale. This is useful if you want to wait for the page transition to end before setting the locale yourself using [`setPendingLocale`](./api#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). - ## `routesNameSeparator` - type: `string` diff --git a/src/templates/plugin.main.js b/src/templates/plugin.main.js index 8e0bd47a4..e55976cf4 100644 --- a/src/templates/plugin.main.js +++ b/src/templates/plugin.main.js @@ -284,9 +284,9 @@ export default async (context) => { i18n.setLocaleCookie = locale => setLocaleCookie(locale, res, { useCookie, cookieDomain, cookieKey, cookieSecure, cookieCrossOrigin }) i18n.getLocaleCookie = () => getLocaleCookie(req, { useCookie, cookieKey, localeCodes }) i18n.setLocale = (locale) => loadAndSetLocale(locale) + i18n.getBrowserLocale = () => getBrowserLocale() i18n.setPendingLocale = setPendingLocale i18n.waitForPendingLocale = waitForPendingLocale - i18n.getBrowserLocale = () => getBrowserLocale() i18n.__baseUrl = app.i18n.__baseUrl i18n.__pendingLocale = app.i18n.__pendingLocale i18n.__pendingLocalePromise = app.i18n.__pendingLocalePromise diff --git a/types/nuxt-i18n.d.ts b/types/nuxt-i18n.d.ts index 86d7e8da5..bd4248f82 100644 --- a/types/nuxt-i18n.d.ts +++ b/types/nuxt-i18n.d.ts @@ -80,6 +80,7 @@ declare namespace NuxtVueI18n { rootRedirect?: string | null | RootRedirectInterface routesNameSeparator?: string seo?: boolean + skipSettingLocaleOnNavigate?: boolean, strategy?: Strategies vueI18n?: VueI18n.I18nOptions | string vueI18nLoader?: boolean diff --git a/types/vue.d.ts b/types/vue.d.ts index a1bc4f528..ea6bfdd1f 100644 --- a/types/vue.d.ts +++ b/types/vue.d.ts @@ -12,10 +12,12 @@ declare module 'vue-i18n' { // it is necessary for the $i18n property in Vue interface: "readonly $i18n: VueI18n & IVueI18n" interface IVueI18n extends NuxtVueI18n.Options.NuxtI18nInterface { localeProperties: NuxtVueI18n.Options.LocaleObject - getLocaleCookie() : string | undefined - setLocaleCookie(locale: string) : undefined - setLocale(locale: string) : Promise - getBrowserLocale() : string | undefined + getLocaleCookie(): string | undefined + setLocaleCookie(locale: string): undefined + setLocale(locale: string): Promise + getBrowserLocale(): string | undefined + setPendingLocale(): Promise + waitForPendingLocale(): Promise } } From a4c0808d7993ec9686a5cf232695fcaf65973b2f Mon Sep 17 00:00:00 2001 From: Pierre-Michel Brown Date: Wed, 3 Feb 2021 08:28:54 +0100 Subject: [PATCH 6/7] Rename pending locale methods to avoid confusion --- docs/content/en/api.md | 4 ++-- docs/content/en/lang-switcher.md | 8 ++++---- docs/content/en/options-reference.md | 4 ++-- docs/content/es/api.md | 4 ++-- docs/content/es/lang-switcher.md | 8 ++++---- docs/content/es/options-reference.md | 2 +- src/templates/plugin.main.js | 8 ++++---- types/vue.d.ts | 4 ++-- 8 files changed, 21 insertions(+), 21 deletions(-) diff --git a/docs/content/en/api.md b/docs/content/en/api.md index 0fcaa2c15..c3befa55e 100644 --- a/docs/content/en/api.md +++ b/docs/content/en/api.md @@ -116,7 +116,7 @@ Instance of [VueI18n class](http://kazupon.github.io/vue-i18n/api/#vuei18n-class Returns browser locale code filtered against the ones defined in options. -#### setPendingLocale v6.20.0+ +#### finalizePendingLocaleChange v6.20.0+ - **Arguments**: - no arguments @@ -124,7 +124,7 @@ Instance of [VueI18n class](http://kazupon.github.io/vue-i18n/api/#vuei18n-class Switches to the pending locale that would have been set on navigate, but was prevented by the option [`skipSettingLocaleOnNavigate`](./options-reference#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). -#### waitForPendingLocale v6.20.0+ +#### waitForPendingLocaleChange v6.20.0+ - **Arguments**: - no arguments diff --git a/docs/content/en/lang-switcher.md b/docs/content/en/lang-switcher.md index d60f25695..057037610 100644 --- a/docs/content/en/lang-switcher.md +++ b/docs/content/en/lang-switcher.md @@ -119,27 +119,27 @@ export default { ```js {}[~/plugins/router.js] export default ({ app }) => { app.nuxt.defaultTransition.beforeEnter = () => { - app.i18n.setPendingLocale() + app.i18n.finalizePendingLocaleChange() } // Optional: wait for locale before scrolling for a smoother transition app.router.options.scrollBehavior = async (to, from, savedPosition) => { // Make sure the route has changed if (to.name !== from.name) { - await app.i18n.waitForPendingLocale() + await app.i18n.waitForPendingLocaleChange() } return savedPosition || { x: 0, y: 0 } } } ``` -If you have a specific transition defined in a page component, you would also need to call `setPendingLocale` from there. +If you have a specific transition defined in a page component, you would also need to call `finalizePendingLocaleChange` from there. ```js {}[~/pages/foo.vue] export default { transition: { beforeEnter() { - this.$i18n.setPendingLocale() + this.$i18n.finalizePendingLocaleChange() } } } diff --git a/docs/content/en/options-reference.md b/docs/content/en/options-reference.md index 2e6f44ca1..18dd91e8f 100644 --- a/docs/content/en/options-reference.md +++ b/docs/content/en/options-reference.md @@ -93,7 +93,7 @@ export default { - type: `string` - default: `ltr` -The app's default direction. Will only be used when `dir` is not specified. +The app's default direction. Will only be used when `dir` is not specified. ## `defaultLocale` @@ -273,7 +273,7 @@ A listener called after app's locale has changed. - type: `boolean` - default: `false` -If `true`, the locale will not be set when navigating to a new locale. This is useful if you want to wait for the page transition to end before setting the locale yourself using [`setPendingLocale`](./api#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). +If `true`, the locale will not be set when navigating to a new locale. This is useful if you want to wait for the page transition to end before setting the locale yourself using [`finalizePendingLocaleChange`](./api#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). ## `defaultLocaleRouteNameSuffix` diff --git a/docs/content/es/api.md b/docs/content/es/api.md index 8ee44264b..2c41e7d1c 100644 --- a/docs/content/es/api.md +++ b/docs/content/es/api.md @@ -116,7 +116,7 @@ Instance of [VueI18n class](http://kazupon.github.io/vue-i18n/api/#vuei18n-class Devuelve el código del idioma que utiliza el navegador, filtrado según los códigos definidos en las opciones. -#### setPendingLocale v6.20.0+ +#### finalizePendingLocaleChange v6.20.0+ - **Arguments**: - no arguments @@ -124,7 +124,7 @@ Instance of [VueI18n class](http://kazupon.github.io/vue-i18n/api/#vuei18n-class Switches to the pending locale that would have been set on navigate, but was prevented by the option [`skipSettingLocaleOnNavigate`](./options-reference#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). -#### waitForPendingLocale v6.20.0+ +#### waitForPendingLocaleChange v6.20.0+ - **Arguments**: - no arguments diff --git a/docs/content/es/lang-switcher.md b/docs/content/es/lang-switcher.md index 92a65e7b9..2e1ca4c90 100644 --- a/docs/content/es/lang-switcher.md +++ b/docs/content/es/lang-switcher.md @@ -117,27 +117,27 @@ export default { ```js {}[~/plugins/router.js] export default ({ app }) => { app.nuxt.defaultTransition.beforeEnter = () => { - app.i18n.setPendingLocale() + app.i18n.finalizePendingLocaleChange() } // Optional: wait for locale before scrolling for a smoother transition app.router.options.scrollBehavior = async (to, from, savedPosition) => { // Make sure the route has changed if (to.name !== from.name) { - await app.i18n.waitForPendingLocale() + await app.i18n.waitForPendingLocaleChange() } return savedPosition || { x: 0, y: 0 } } } ``` -If you have a specific transition defined in a page component, you would also need to call `setPendingLocale` from there. +If you have a specific transition defined in a page component, you would also need to call `finalizePendingLocaleChange` from there. ```js {}[~/pages/foo.vue] export default { transition: { beforeEnter() { - this.$i18n.setPendingLocale() + this.$i18n.finalizePendingLocaleChange() } } } diff --git a/docs/content/es/options-reference.md b/docs/content/es/options-reference.md index 0d9efbaea..0155e173c 100644 --- a/docs/content/es/options-reference.md +++ b/docs/content/es/options-reference.md @@ -271,7 +271,7 @@ A listener called after app's locale has changed. - type: `boolean` - default: `false` -If true, the locale will not be set when navigating to a new locale. This is useful if you want to wait for the page transition to end before setting the locale yourself using [`setPendingLocale`](./api#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). +If true, the locale will not be set when navigating to a new locale. This is useful if you want to wait for the page transition to end before setting the locale yourself using [`finalizePendingLocaleChange`](./api#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). ## `defaultLocaleRouteNameSuffix` diff --git a/src/templates/plugin.main.js b/src/templates/plugin.main.js index e55976cf4..fa3636f48 100644 --- a/src/templates/plugin.main.js +++ b/src/templates/plugin.main.js @@ -209,7 +209,7 @@ export default async (context) => { return [null, null] } - const setPendingLocale = async () => { + const finalizePendingLocaleChange = async () => { if (!app.i18n.__pendingLocale) { return } @@ -218,7 +218,7 @@ export default async (context) => { app.i18n.__pendingLocale = null } - const waitForPendingLocale = async () => { + const waitForPendingLocaleChange = async () => { if (app.i18n.__pendingLocale) { await app.i18n.__pendingLocalePromise } @@ -285,8 +285,8 @@ export default async (context) => { i18n.getLocaleCookie = () => getLocaleCookie(req, { useCookie, cookieKey, localeCodes }) i18n.setLocale = (locale) => loadAndSetLocale(locale) i18n.getBrowserLocale = () => getBrowserLocale() - i18n.setPendingLocale = setPendingLocale - i18n.waitForPendingLocale = waitForPendingLocale + i18n.finalizePendingLocaleChange = finalizePendingLocaleChange + i18n.waitForPendingLocaleChange = waitForPendingLocaleChange i18n.__baseUrl = app.i18n.__baseUrl i18n.__pendingLocale = app.i18n.__pendingLocale i18n.__pendingLocalePromise = app.i18n.__pendingLocalePromise diff --git a/types/vue.d.ts b/types/vue.d.ts index ea6bfdd1f..8ffb36977 100644 --- a/types/vue.d.ts +++ b/types/vue.d.ts @@ -16,8 +16,8 @@ declare module 'vue-i18n' { setLocaleCookie(locale: string): undefined setLocale(locale: string): Promise getBrowserLocale(): string | undefined - setPendingLocale(): Promise - waitForPendingLocale(): Promise + finalizePendingLocaleChange(): Promise + waitForPendingLocaleChange(): Promise } } From 088cada3c905edc09bdf056b5b2e0b656d77fe4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ch=C5=82odnicki?= Date: Wed, 3 Feb 2021 09:02:13 +0100 Subject: [PATCH 7/7] Minor tweaks --- docs/content/en/options-reference.md | 4 +++- docs/content/es/options-reference.md | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/content/en/options-reference.md b/docs/content/en/options-reference.md index 18dd91e8f..2034a82c7 100644 --- a/docs/content/en/options-reference.md +++ b/docs/content/en/options-reference.md @@ -270,10 +270,12 @@ A listener called after app's locale has changed. ## `skipSettingLocaleOnNavigate` v6.20.0+ +v6.20.0+ + - type: `boolean` - default: `false` -If `true`, the locale will not be set when navigating to a new locale. This is useful if you want to wait for the page transition to end before setting the locale yourself using [`finalizePendingLocaleChange`](./api#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). +If `true`, the locale will not be set when navigating to a new locale. This is useful if you want to wait for the page transition to end before setting the locale yourself using [`skipSettingLocaleOnNavigate`](./api#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). ## `defaultLocaleRouteNameSuffix` diff --git a/docs/content/es/options-reference.md b/docs/content/es/options-reference.md index 0155e173c..6f5095205 100644 --- a/docs/content/es/options-reference.md +++ b/docs/content/es/options-reference.md @@ -268,10 +268,12 @@ A listener called after app's locale has changed. ## `skipSettingLocaleOnNavigate` v6.20.0+ +v6.20.0+ + - type: `boolean` - default: `false` -If true, the locale will not be set when navigating to a new locale. This is useful if you want to wait for the page transition to end before setting the locale yourself using [`finalizePendingLocaleChange`](./api#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). +If `true`, the locale will not be set when navigating to a new locale. This is useful if you want to wait for the page transition to end before setting the locale yourself using [`skipSettingLocaleOnNavigate`](./api#skipsettinglocaleonnavigate). See more information in [Wait for page transition](./lang-switcher#wait-for-page-transition). ## `defaultLocaleRouteNameSuffix`