Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP feat: Implement slug solution #120

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,21 @@ exports.onCreatePage = async ({ page, actions }, pluginOptions) => {
}
}

// Return all languages slug for this page
const getSlugs = (path) => {
const currentPage = page.path.replace(/\//g, "")
var slugs = {}
languages.forEach(language => {
var messages = getMessages(path, language)
slugs[language] = messages[`${currentPage}.slug`] ? `/${messages[currentPage + '.slug']}` : `/${currentPage}`
})
return slugs
}

const generatePage = (routed, language) => {
const messages = getMessages(path, language)
const newPath = routed ? `/${language}${page.path}` : page.path
const slugs = getSlugs(path);
var newPath = routed ? `/${language}${slugs[language]}` : `${slugs[language]}`
return {
...page,
path: newPath,
Expand All @@ -81,6 +93,7 @@ exports.onCreatePage = async ({ page, actions }, pluginOptions) => {
intl: {
language,
languages,
slugs,
messages,
routed,
originalPath: page.path,
Expand Down
37 changes: 13 additions & 24 deletions src/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ import PropTypes from "prop-types"
import { Link as GatsbyLink, navigate as gatsbyNavigate } from "gatsby"
import { IntlContextConsumer } from "./intl-context"

const getLink = (language, to, routed, messages) => {
const currentPage = to.replace(/\//g, "")
const slugTo = messages[`${currentPage}.slug`] ? messages[`${currentPage}.slug`] : to
const link = routed || language ? `/${language}/${slugTo}` : `${slugTo}`

return link;
}

const Link = ({ to, language, children, onClick, ...rest }) => (
<IntlContextConsumer>
{intl => {
const languageLink = language || intl.language
const link = intl.routed || language ? `/${languageLink}${to}` : `${to}`
const link = getLink(languageLink, to, intl.routed, intl.messages)

const handleClick = e => {
if (language) {
Expand Down Expand Up @@ -44,38 +52,19 @@ export const navigate = (to, options) => {
return
}

const { language, routed } = window.___gatsbyIntl
const link = routed ? `/${language}${to}` : `${to}`
const { language, routed, messages } = window.___gatsbyIntl
const link = getLink(language, to, routed, messages)
gatsbyNavigate(link, options)
}

export const changeLocale = (language, to) => {
if (typeof window === "undefined") {
return
}
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
}
const i = pathname.indexOf(`/`, 1)
return pathname.substring(i)
}
const { slugs } = window.___gatsbyIntl

const pathname =
to || removeLocalePart(removePrefix(window.location.pathname))
// TODO: check slash
const link = `/${language}${pathname}${window.location.search}`
const link = `/${language}${slugs[language]}${window.location.search}`
localStorage.setItem("gatsby-intl-language", language)
gatsbyNavigate(link)
}