From 06aaaa22e99186ace81bdbf8ae4f981e7bf66082 Mon Sep 17 00:00:00 2001 From: Khaled Garbaya Date: Tue, 8 May 2018 16:31:49 +0200 Subject: [PATCH 1/3] Add fallback locale support --- .../gatsby-source-contentful/src/normalize.js | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/packages/gatsby-source-contentful/src/normalize.js b/packages/gatsby-source-contentful/src/normalize.js index 5c99d43ec6097..6c9483b45495f 100644 --- a/packages/gatsby-source-contentful/src/normalize.js +++ b/packages/gatsby-source-contentful/src/normalize.js @@ -11,18 +11,22 @@ const digest = str => const typePrefix = `Contentful` const makeTypeName = type => _.upperFirst(_.camelCase(`${typePrefix} ${type}`)) -const getLocalizedField = ({ field, defaultLocale, locale }) => { - if (!_.isUndefined(field[locale.code])) { - return field[locale.code] - } else if (!_.isUndefined(field[locale.fallbackCode])) { - return field[locale.fallbackCode] +const getLocalizedField = ({ field, localeCode, localesFallback }) => { + if (!_.isUndefined(field[localeCode])) { + return field[localeCode] + } else if (!_.isUndefined(localesFallback[localeCode])) { + return getLocalizedField({ field, localeCode: localesFallback[localeCode], localesFallback}) } else { return null } } - -const makeGetLocalizedField = ({ locale, defaultLocale }) => field => - getLocalizedField({ field, locale, defaultLocale }) +const buildFallbackChain = (locales) => { + const localesFallback = {} + _.each(locales, locale => localesFallback[locale.code] = locale.fallbackCode) + return localesFallback +} +const makeGetLocalizedField = ({ locale, localesFallback }) => field => + getLocalizedField({ field, localeCode: locale.code, localesFallback }) exports.getLocalizedField = getLocalizedField @@ -202,8 +206,9 @@ exports.createContentTypeNodes = ({ }) => { const contentTypeItemId = contentTypeItem.name locales.forEach(locale => { + const localesFallback = buildFallbackChain(locales) const mId = makeMakeId({ currentLocale: locale.code, defaultLocale }) - const getField = makeGetLocalizedField({ locale, defaultLocale }) + const getField = makeGetLocalizedField({ locale, localesFallback, defaultLocale }) // Warn about any field conflicts const conflictFields = [] @@ -401,8 +406,9 @@ exports.createAssetNodes = ({ locales, }) => { locales.forEach(locale => { + const localesFallback = buildFallbackChain(locales) const mId = makeMakeId({ currentLocale: locale.code, defaultLocale }) - const getField = makeGetLocalizedField({ locale, defaultLocale }) + const getField = makeGetLocalizedField({ locale, localesFallback, defaultLocale }) const localizedAsset = { ...assetItem } // Create a node for each asset. They may be referenced by Entries From a5f22e884dd14a68884e5209f57d3d2a80ae13a5 Mon Sep 17 00:00:00 2001 From: Khaled Garbaya Date: Tue, 8 May 2018 16:54:39 +0200 Subject: [PATCH 2/3] Fix linting --- packages/gatsby-source-contentful/src/normalize.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby-source-contentful/src/normalize.js b/packages/gatsby-source-contentful/src/normalize.js index 6c9483b45495f..af1ab3e44efff 100644 --- a/packages/gatsby-source-contentful/src/normalize.js +++ b/packages/gatsby-source-contentful/src/normalize.js @@ -15,7 +15,7 @@ const getLocalizedField = ({ field, localeCode, localesFallback }) => { if (!_.isUndefined(field[localeCode])) { return field[localeCode] } else if (!_.isUndefined(localesFallback[localeCode])) { - return getLocalizedField({ field, localeCode: localesFallback[localeCode], localesFallback}) + return getLocalizedField({ field, localeCode: localesFallback[localeCode], localesFallback }) } else { return null } From 866a669c17956bdf8a25e3fe867027d634316c98 Mon Sep 17 00:00:00 2001 From: Khaled Garbaya Date: Wed, 9 May 2018 13:15:42 +0200 Subject: [PATCH 3/3] test: Fix test --- .../src/__tests__/normalize.js | 18 ++++++--- .../gatsby-source-contentful/src/normalize.js | 39 ++++++++++++++----- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/packages/gatsby-source-contentful/src/__tests__/normalize.js b/packages/gatsby-source-contentful/src/__tests__/normalize.js index bff51753ca072..959e604d9d67f 100644 --- a/packages/gatsby-source-contentful/src/__tests__/normalize.js +++ b/packages/gatsby-source-contentful/src/__tests__/normalize.js @@ -97,11 +97,17 @@ describe(`Gets field value based on current locale`, () => { de: `Playsam Streamliner Klassisches Auto, Espresso`, "en-US": `Playsam Streamliner Classic Car, Espresso`, } + const locales = [ + { code: `en-US` }, + { code: `de`, fallbackCode: `en-US` }, + { code: `gsw_CH`, fallbackCode: `de` }, + ] + const localesFallback = normalize.buildFallbackChain(locales) it(`Gets the specified locale`, () => { expect( normalize.getLocalizedField({ field, - defaultLocale: `en-US`, + localesFallback, locale: { code: `en-US`, }, @@ -110,7 +116,7 @@ describe(`Gets field value based on current locale`, () => { expect( normalize.getLocalizedField({ field, - defaultLocale: `en-US`, + localesFallback, locale: { code: `de`, }, @@ -125,7 +131,7 @@ describe(`Gets field value based on current locale`, () => { expect( normalize.getLocalizedField({ field: falseyField, - defaultLocale: `en-US`, + localesFallback, locale: { code: `en-US`, }, @@ -135,7 +141,7 @@ describe(`Gets field value based on current locale`, () => { expect( normalize.getLocalizedField({ field: falseyField, - defaultLocale: `en-US`, + localesFallback, locale: { code: `de`, }, @@ -146,7 +152,7 @@ describe(`Gets field value based on current locale`, () => { expect( normalize.getLocalizedField({ field, - defaultLocale: `en-US`, + localesFallback, locale: { code: `gsw_CH`, fallbackCode: `de`, @@ -158,7 +164,7 @@ describe(`Gets field value based on current locale`, () => { expect( normalize.getLocalizedField({ field, - defaultLocale: `en-US`, + localesFallback, locale: { code: `es-US`, fallbackCode: `null`, diff --git a/packages/gatsby-source-contentful/src/normalize.js b/packages/gatsby-source-contentful/src/normalize.js index af1ab3e44efff..a3001cf55c409 100644 --- a/packages/gatsby-source-contentful/src/normalize.js +++ b/packages/gatsby-source-contentful/src/normalize.js @@ -11,24 +11,35 @@ const digest = str => const typePrefix = `Contentful` const makeTypeName = type => _.upperFirst(_.camelCase(`${typePrefix} ${type}`)) -const getLocalizedField = ({ field, localeCode, localesFallback }) => { - if (!_.isUndefined(field[localeCode])) { - return field[localeCode] - } else if (!_.isUndefined(localesFallback[localeCode])) { - return getLocalizedField({ field, localeCode: localesFallback[localeCode], localesFallback }) +const getLocalizedField = ({ field, locale, localesFallback }) => { + if (!_.isUndefined(field[locale.code])) { + return field[locale.code] + } else if ( + !_.isUndefined(locale.code) && + !_.isUndefined(localesFallback[locale.code]) + ) { + return getLocalizedField({ + field, + locale: { code: localesFallback[locale.code] }, + localesFallback, + }) } else { return null } } -const buildFallbackChain = (locales) => { +const buildFallbackChain = locales => { const localesFallback = {} - _.each(locales, locale => localesFallback[locale.code] = locale.fallbackCode) + _.each( + locales, + locale => (localesFallback[locale.code] = locale.fallbackCode) + ) return localesFallback } const makeGetLocalizedField = ({ locale, localesFallback }) => field => - getLocalizedField({ field, localeCode: locale.code, localesFallback }) + getLocalizedField({ field, locale, localesFallback }) exports.getLocalizedField = getLocalizedField +exports.buildFallbackChain = buildFallbackChain // If the id starts with a number, left-pad it with a c (for Contentful of // course :-)) @@ -208,7 +219,11 @@ exports.createContentTypeNodes = ({ locales.forEach(locale => { const localesFallback = buildFallbackChain(locales) const mId = makeMakeId({ currentLocale: locale.code, defaultLocale }) - const getField = makeGetLocalizedField({ locale, localesFallback, defaultLocale }) + const getField = makeGetLocalizedField({ + locale, + localesFallback, + defaultLocale, + }) // Warn about any field conflicts const conflictFields = [] @@ -408,7 +423,11 @@ exports.createAssetNodes = ({ locales.forEach(locale => { const localesFallback = buildFallbackChain(locales) const mId = makeMakeId({ currentLocale: locale.code, defaultLocale }) - const getField = makeGetLocalizedField({ locale, localesFallback, defaultLocale }) + const getField = makeGetLocalizedField({ + locale, + localesFallback, + defaultLocale, + }) const localizedAsset = { ...assetItem } // Create a node for each asset. They may be referenced by Entries