Created
October 19, 2021 08:56
-
-
Save aeadedoyin/872cec0151aaf76fd4ad801424084cc9 to your computer and use it in GitHub Desktop.
Localization Middleware Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default ({ $axios, store, i18n, switchLocalePath }) => { | |
// For none IP use | |
// return true | |
// For IP specific use | |
return $axios | |
.$get('https://www.cloudflare.com/cdn-cgi/trace') | |
.then((response) => { | |
const country = response | |
.trim() | |
.split('\n') | |
.reduce(function(obj, pair) { | |
pair = pair.split('=') | |
obj[pair[0]] = pair[1] | |
return obj | |
}, {}) | |
.loc.toLowerCase() | |
if (['in', 'ng'].includes(country)) { | |
const hrefExist = | |
i18n.locales.filter(e => document.location.href.includes(e.domain)) | |
.length > 0 | |
if (!hrefExist) { | |
window.location.assign(switchLocalePath(country)) | |
} | |
} else { | |
window.location.assign('SITEURL GOES HERE') | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment