Skip to content

Instantly share code, notes, and snippets.

@aeadedoyin
Created October 19, 2021 08:56
Show Gist options
  • Save aeadedoyin/872cec0151aaf76fd4ad801424084cc9 to your computer and use it in GitHub Desktop.
Save aeadedoyin/872cec0151aaf76fd4ad801424084cc9 to your computer and use it in GitHub Desktop.
Localization Middleware Sample
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