Last active
December 17, 2018 12:35
-
-
Save ramoncardena/dcb80ad97af0aa4e249686761123e684 to your computer and use it in GitHub Desktop.
Function to generate hreflang meta tags (React/React Router v4)
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
import Url from 'url-parse'; | |
import * as config from '../config'; | |
function alternateLangs(url, currentLng) { | |
// Load all languages and filter current language | |
const languages = Array.from(config.ALL_LANGUAGES).filter( | |
lng => lng !== currentLng | |
); | |
// Load url object for current location | |
const currentUrl = new Url(url); | |
// Save origin http:// | |
const urlOrigin = currentUrl.origin; | |
// Save pathname /xx/xxxxx/... | |
const urlPathname = currentUrl.pathname; | |
var alternateLinks = []; | |
// If current language is the default language | |
if (currentLng === config.DEFAULT_LANGUAGE) { | |
languages.forEach(item => { | |
alternateLinks.push(urlOrigin + '/' + item + urlPathname); | |
}); | |
} else { | |
// If current language isn't the default language | |
languages.forEach(item => { | |
if (item === config.DEFAULT_LANGUAGE) { | |
alternateLinks.push( | |
urlOrigin + urlPathname.replace(currentLng + '/', '') | |
); | |
} else { | |
alternateLinks.push( | |
urlOrigin + | |
'/' + | |
item + | |
urlPathname.replace(currentLng, item) | |
); | |
} | |
}); | |
} | |
return alternateLinks; | |
} | |
export default alternateLangs; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment