Allow for alternative language detection
See original GitHub issueWhat problem does this feature solve?
Currently, Nuxt-i18n has an option to allow for browser detection which is great but not always accurate. It would be nice to allow for an alternative source to detect users language preference.
What does the proposed changes look like?
I’m proposing an option to allow for an alternative provider to detect a users language needs. For example, a geolocation service that provides an API that returns geodata.
In this particular instance my app is in SPA
mode.
nuxt.config.js
:
detectBrowserLanguage: {
api: "https://someAPI.com/location", // Just as an example of how this can be checked
useCookie: true,
cookieKey: 'i18n_redirected'
},
middleware.js
:
if (detectBrowserLanguage) {
let browserLocale
if (useCookie && (browserLocale = getCookie()) && browserLocale !== 1 && browserLocale !== '1') {
} else if (isSpa && apiOption) {
browserLocale = store.state.geoData.country.toLowerCase();
Something like that. I’ve made these edits to middleware.js
and it’s working for me. But I’m also seeing that an entry is made to the history then I’m redirected to the proper prefixed page.
What I would expect to happen for example if someone comes to ‘/’ that only one entry is made to the history. There needs to be some type of check if route is the same then no entry is made. Maybe this is something that needs to happen as a plugin?
<div align="right">This feature request is available on Nuxt community (#c196)</div>Issue Analytics
- State:
- Created 5 years ago
- Comments:6
Top GitHub Comments
I’d say it would be too heavy to do it for every page load request but when used with conjunction with cookie storage, it would be OK and useful. Maybe let’s reopen this?
Yeap, it woul’d be nice