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

added language toggle support to starter #33

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
47 changes: 25 additions & 22 deletions gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require("dotenv").config({
const agilityConfig = {
guid: process.env.AGILITY_GUID,
apiKey: process.env.AGILITY_API_KEY,
isPreview: process.env.AGILITY_API_ISPREVIEW
isPreview: process.env.AGILITY_API_ISPREVIEW,
}

/**
Expand All @@ -19,47 +19,50 @@ const agilityConfig = {
module.exports = {
siteMetadata: {
title: "Agility CMS Gatsby Starter",

},
plugins: [
`gatsby-plugin-netlify`,
`gatsby-plugin-react-helmet`,
{
//the name of the plugin
resolve: "@agility/gatsby-source-agilitycms",
resolve: "@agility/gatsby-source-agilitycms",
//the options for our plugin
options: {
//your Agility Content Fetch API Guid
guid: agilityConfig.guid,
guid: agilityConfig.guid,
//your Agility Content Fetch API Key
apiKey: agilityConfig.apiKey,
apiKey: agilityConfig.apiKey,
//set this to true if you are using the preview API Key
isPreview: agilityConfig.isPreview,
isPreview: agilityConfig.isPreview,
//set this to true to see expanded traces in the build logs
debug: false,
//the languages you want to source content for
languages: [{
// The name of the language code
name: "English",
// The actual language code set in Agility CMS
code: "en-us",
// The name to be used in the URL path that represents the current language
path: "en"
}],
languages: [
{
// The name of the language code
name: "English",
// The actual language code set in Agility CMS
code: "en-us",
// The name to be used in the URL path that represents the current language
path: "en",
},
],
// The channels you want to include
channels: [{
// The reference name for the website channel as it is defined in Agility CMS
referenceName: "website"
}],
//the page template that will be used to render Agility CMS pages
masterPageTemplate: "./src/AgilityPage.js"
channels: [
{
// The reference name for the website channel as it is defined in Agility CMS
referenceName: "website",
},
],
//the page template that will be used to render Agility CMS pages
masterPageTemplate: "./src/AgilityPage.js",
},
},
`gatsby-plugin-sharp`,
`gatsby-transformer-sharp`,
{
{
resolve: `gatsby-source-filesystem`,
options: { name: `images`, path: `./src/img/` }
options: { name: `images`, path: `./src/img/` },
},
],
}
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\""
},
"dependencies": {
"@agility/gatsby-source-agilitycms": "^1.6.4",
"@agility/gatsby-source-agilitycms": "^1.6.6",
"dotenv": "^8.1.0",
"gatsby": "^2.21.4",
"gatsby-image": "^2.3.2",
Expand Down
75 changes: 49 additions & 26 deletions src/AgilityPage.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,63 @@
import React from 'react'
import React from "react"
import { graphql } from "gatsby"
import agilityUtils from './agility/utils'
import AgilityPageTemplate from './agility/components/AgilityPageTemplate'
import agilityUtils from "./agility/utils"
import AgilityPageTemplate from "./agility/components/AgilityPageTemplate"
//Some things we need for our layout
import LayoutTemplate from "./components/LayoutTemplate"
import PreviewBar from "./components/PreviewBar"
import GlobalHeader from './components/GlobalHeader'
import SEO from './components/SEO'

import GlobalHeader from "./components/GlobalHeader"
import SEO from "./components/SEO"

//Our query to get the our page data and check for a dynamic page item (agilityItem)
export const query = graphql`
query($pageID: Int!, $contentID: Int!, $languageCode: String!) {

agilitypage(languageCode: { eq: $languageCode }, itemID: { eq: $pageID }) {
pageJson
}
agilityitem(languageCode: {eq: $languageCode}, itemID: {eq: $contentID}) {
itemJson
pageJson
}
}
agilityitem(
languageCode: { eq: $languageCode }
itemID: { eq: $contentID }
) {
itemJson
}
pagesInOtherLanguages: allAgilitySitemapNode(
filter: {
languageCode: { ne: $languageCode }
pageID: { eq: $pageID }
contentID: { eq: $contentID }
}
) {
nodes {
pageID
pagePath
path
languageCode
contentID
title
}
}
}
`
const AgilityPage = ({ pageContext, data }) => {
const viewModel = agilityUtils.buildPageViewModel({ pageContext, data });
return (
<LayoutTemplate>
<SEO title={viewModel.page.title} description={viewModel.page.seo.metaDescription} />
<PreviewBar isPreview={viewModel.isPreview} />
<GlobalHeader languageCode={viewModel.languageCode} isMultiLanguage={viewModel.isMultiLanguage} />
<main className="main">
<AgilityPageTemplate {...viewModel} />
</main>
</LayoutTemplate>
);
const viewModel = agilityUtils.buildPageViewModel({ pageContext, data })
return (
<LayoutTemplate>
<SEO
title={viewModel.page.title}
description={viewModel.page.seo.metaDescription}
/>
<PreviewBar isPreview={viewModel.isPreview} />
<GlobalHeader
languageCode={viewModel.languageCode}
isMultiLanguage={viewModel.isMultiLanguage}
pagesInOtherLanguages={viewModel.pagesInOtherLanguages}
languages={viewModel.languages}
/>
<main className="main">
<AgilityPageTemplate {...viewModel} />
</main>
</LayoutTemplate>
)
}

export default AgilityPage;


export default AgilityPage
31 changes: 31 additions & 0 deletions src/agility/components/StaticMultiLangPreviewPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const StaticMultiLangPreviewPage = ({ pageContext }) => {
if (typeof window !== `undefined`) {
const requestedLangCode = getParameterByName({
name: "lang",
url: window.location.href,
})

const { languages } = pageContext

languages.map((language) => {
if (language.code === requestedLangCode) {
window.location = `/${language.path}${window.location.pathname}`
return null
}
})
}
//no render
return null
}

const getParameterByName = ({ name, url }) => {
if (!url) url = window.location.href
name = name.replace(/[[\]]/g, "\\$&")
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url)
if (!results) return null
if (!results[2]) return ""
return decodeURIComponent(results[2].replace(/\+/g, " "))
}

export default StaticMultiLangPreviewPage
Loading