Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ export const changeLocale = (language, to) => {
}
const { routed } = window.___gatsbyIntl

const removePrefix = pathname => {
const base =
typeof __BASE_PATH__ !== `undefined` ? __BASE_PATH__ : __PATH_PREFIX__
if (base && pathname.indexOf(base) === 0) {
pathname = pathname.slice(base.length)
}
return pathname
}

const removeLocalePart = pathname => {
if (!routed) {
return pathname
Expand All @@ -63,7 +72,8 @@ export const changeLocale = (language, to) => {
return pathname.substring(i)
}

const pathname = to || removeLocalePart(window.location.pathname)
const pathname =
to || removeLocalePart(removePrefix(window.location.pathname))
// TODO: check slash
const link = `/${language}${pathname}${window.location.search}`
localStorage.setItem("gatsby-intl-language", language)
Expand Down
6 changes: 3 additions & 3 deletions src/wrap-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default ({ element, props }) => {

const { pageContext, location } = props
const { intl } = pageContext
const { language, languages, redirect, routed } = intl
const { language, languages, redirect, routed, originalPath } = intl

if (typeof window !== "undefined") {
window.___gatsbyIntl = intl
Expand All @@ -52,7 +52,7 @@ export default ({ element, props }) => {
const isRedirect = redirect && !routed

if (isRedirect) {
const { pathname, search } = location
const { search } = location

// Skip build, Browsers only
if (typeof window !== "undefined") {
Expand All @@ -68,7 +68,7 @@ export default ({ element, props }) => {
}

const queryParams = search || ""
const newUrl = withPrefix(`/${detected}${pathname}${queryParams}`)
const newUrl = withPrefix(`/${detected}${originalPath}${queryParams}`)
window.localStorage.setItem("gatsby-intl-language", detected)
window.location.replace(newUrl)
}
Expand Down