Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Commit

Permalink
feat(I18n): simplify page translation logic
Browse files Browse the repository at this point in the history
* generate page translations in gatsby-node upfront and feed them via fields
* add PageTranslations fragment
* no gatsby-config field mapping needed
* no constructing and sorting of page translation links in MainLayout in the users browser anymore
  • Loading branch information
CanRau committed Nov 11, 2018
1 parent b29290f commit 83ef404
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 123 deletions.
2 changes: 0 additions & 2 deletions gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ module.exports = {
version,
},
mapping: {
'MarkdownRemark.fields.translations': `MarkdownRemark`,
'JavascriptFrontmatter.fields.translations': `JavascriptFrontmatter`,
'MarkdownRemark.fields.suggested': `MarkdownRemark`,
'MarkdownRemark.fields.newer': `MarkdownRemark`,
'MarkdownRemark.fields.older': `MarkdownRemark`,
Expand Down
33 changes: 32 additions & 1 deletion gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,38 @@ exports.createPages = async ({ actions, getNodes, graphql }) => {
createNodeField({
node,
name: `translations`,
value: array.filter(n => n.id !== node.id).map(node => node.id),
value: array
.map(n => {
const lang = Languages.find(
x => x.frontmatter.id === n.frontmatter.lang
)
if (!lang) return null
return Object.assign({}, lang.frontmatter, {
to: n.fields.url,
fields: {
url: n.fields.url,
},
frontmatter: {
title: n.frontmatter.title,
lang: n.frontmatter.lang,
slug: n.frontmatter.slug,
cover: {
publicURL: n.frontmatter.cover
? n.frontmatter.cover.publicURL
: ``,
},
},
})
}) //.filter(n => n.id !== node.id).map(node => node.id),
.filter(n => n)
.sort(
(a, b) =>
a.frontmatter.lang < b.frontmatter.lang
? -1
: a.frontmatter.lang > b.frontmatter.lang
? 1
: 0
),
})

// set up short url redirects
Expand Down
20 changes: 20 additions & 0 deletions src/components/MainLayout/fragments.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,24 @@ export const Fragments = graphql`
}
}
}
fragment PageTranslations on JavascriptFrontmatter {
fields {
translations {
id
lc
title
titleShort
to
fields {
url
}
frontmatter {
title
lang
slug
}
}
}
}
`
31 changes: 3 additions & 28 deletions src/components/MainLayout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,6 @@ const generateMetaMenu = ({ translations, getLang, menuItems }) =>
// [lang]: { translations: messages }
// })

const getTranslations = page =>
[
...(page.fields.translations || []),
{
fields: {
url: page.fields.url,
},
frontmatter: {
lang: page.frontmatter.lang,
slug: page.frontmatter.slug,
title: page.frontmatter.title,
},
},
].sort(
(a, b) =>
a.frontmatter.lang < b.frontmatter.lang
? -1
: a.frontmatter.lang > b.frontmatter.lang
? 1
: 0
)

// i18n.init({
// initImmediate: false,
// fallbackLng: `en`,
Expand Down Expand Up @@ -147,7 +125,7 @@ class MainLayout extends Component {

const lang = pageContext.lang
// const i18nStore = getI18nStore(lang, pageContext.messages)
const translations = getTranslations(page)
const translations = page.fields.translations
const getLang = getLangFactory(languages.edges)
const menuItems = menu.edges || []
const mainMenu = generateMainMenu(menuItems)
Expand Down Expand Up @@ -196,11 +174,8 @@ class MainLayout extends Component {
property="og:url"
content={`${site.siteMetadata.siteUrl}${page.fields.url}`}
/>
<meta
property="og:locale"
content={getLang(page.frontmatter.lang).lc}
/>
{getLang(page.frontmatter.lang, true).map(x => (
<meta property="og:locale" content={getLang(lang).lc} />
{getLang(lang, true).map(x => (
<meta property="og:locale:alternate" content={x.lc} key={x.lc} />
))}
<meta
Expand Down
11 changes: 1 addition & 10 deletions src/templates/AboutPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,9 @@ export const query = graphql`
...Accounts
page: javascriptFrontmatter(frontmatter: { slug: { eq: $slug } }) {
...PageTranslations
fields {
url
translations {
fields {
url
}
frontmatter {
title
lang
slug
}
}
}
frontmatter {
title
Expand Down
11 changes: 1 addition & 10 deletions src/templates/BlogPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,18 +227,9 @@ export const query = graphql`
...Accounts
page: javascriptFrontmatter(frontmatter: { slug: { eq: $slug } }) {
...PageTranslations
fields {
url
translations {
fields {
url
}
frontmatter {
title
lang
slug
}
}
}
frontmatter {
title
Expand Down
11 changes: 1 addition & 10 deletions src/templates/ContactPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,9 @@ export const query = graphql`
...Accounts
page: javascriptFrontmatter(frontmatter: { slug: { eq: $slug } }) {
...PageTranslations
fields {
url
translations {
fields {
url
}
frontmatter {
title
lang
slug
}
}
}
frontmatter {
title
Expand Down
11 changes: 1 addition & 10 deletions src/templates/DonatePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,9 @@ export const query = graphql`
...SupportWidget
page: javascriptFrontmatter(frontmatter: { slug: { eq: $slug } }) {
...PageTranslations
fields {
url
translations {
fields {
url
}
frontmatter {
title
lang
slug
}
}
}
frontmatter {
title
Expand Down
10 changes: 1 addition & 9 deletions src/templates/ErrorPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,7 @@ export const query = graphql`
page: javascriptFrontmatter(
frontmatter: { slug: { eq: $slug }, lang: { eq: $lang } }
) {
fields {
translations {
frontmatter {
title
lang
slug
}
}
}
...PageTranslations
frontmatter {
title
lang
Expand Down
1 change: 1 addition & 0 deletions src/templates/GalleryPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@
// ...Paginator

// page: javascriptFrontmatter(frontmatter: { slug: { eq: $slug } }) {
// ...PageTranslations
// fields {
// url
// translations {
Expand Down
11 changes: 1 addition & 10 deletions src/templates/HomePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,18 +280,9 @@ export const query = graphql`
...SupportWidget
page: javascriptFrontmatter(fields: { url: { eq: $url } }) {
...PageTranslations
fields {
url
translations {
fields {
url
}
frontmatter {
title
lang
slug
}
}
}
frontmatter {
title
Expand Down
11 changes: 1 addition & 10 deletions src/templates/PaypalSuccessPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,9 @@ export const query = graphql`
...newstickerLandscape
page: javascriptFrontmatter(frontmatter: { slug: { eq: $slug } }) {
...PageTranslations
fields {
url
translations {
fields {
url
}
frontmatter {
title
lang
slug
}
}
}
frontmatter {
title
Expand Down
11 changes: 1 addition & 10 deletions src/templates/SimplePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,9 @@ export const query = graphql`
...Accounts
page: javascriptFrontmatter(frontmatter: { slug: { eq: $slug } }) {
...PageTranslations
fields {
url
translations {
fields {
url
}
frontmatter {
title
lang
slug
}
}
}
frontmatter {
title
Expand Down
14 changes: 1 addition & 13 deletions src/templates/SupportPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,21 +473,9 @@ export const query = graphql`
...SupportWidget
page: javascriptFrontmatter(frontmatter: { slug: { eq: $slug } }) {
...PageTranslations
fields {
url
translations {
fields {
url
}
frontmatter {
title
lang
slug
cover {
publicURL
}
}
}
}
frontmatter {
title
Expand Down

0 comments on commit 83ef404

Please sign in to comment.