Skip to content

Commit

Permalink
value to check if i18n is enabled for this build (#22956)
Browse files Browse the repository at this point in the history
  • Loading branch information
tesseralis committed Apr 9, 2020
1 parent 5037e9d commit 7f6b27f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
10 changes: 3 additions & 7 deletions www/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const path = require(`path`)
require(`dotenv`).config({
path: `.env.${process.env.NODE_ENV}`,
})
const { langCodes } = require(`./src/utils/i18n`)
const { i18nEnabled, langCodes } = require(`./src/utils/i18n`)

const GA = {
identifier: `UA-93349937-5`,
Expand Down Expand Up @@ -53,12 +53,8 @@ if (process.env.AIRTABLE_API_KEY) {
})
}

// true if `env.LOCALES` has a defined list of languages
if (langCodes.length > 0) {
const naughtyFiles = [
`docs/docs/graphql-api.md`,
`docs/docs/data-fetching.md`,
]
if (i18nEnabled) {
const naughtyFiles = [`docs/docs/data-fetching.md`]
dynamicPlugins.push(
...langCodes.map(code => ({
resolve: `gatsby-source-git`,
Expand Down
3 changes: 2 additions & 1 deletion www/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const child_process = require(`child_process`)
const startersRedirects = require(`./starter-redirects.json`)
const yaml = require(`js-yaml`)
const redirects = yaml.load(fs.readFileSync(`./redirects.yaml`))
const { i18nEnabled } = require(`./src/utils/i18n`)

const docs = require(`./src/utils/node/docs.js`)
const showcase = require(`./src/utils/node/showcase.js`)
Expand Down Expand Up @@ -41,7 +42,7 @@ exports.onCreateNode = helpers => {

exports.onPostBootstrap = () => {
// Compile language strings if locales are enabled
if (!!process.env.LOCALES) {
if (i18nEnabled) {
child_process.execSync(`yarn lingui:build`)
}
}
Expand Down
4 changes: 2 additions & 2 deletions www/src/components/I18nContext.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from "react"
import { defaultLang } from "../utils/i18n"
import { i18nEnabled, defaultLang } from "../utils/i18n"
import { I18nProvider as LinguiProvider } from "@lingui/react"

// Lingui doesn't give access to the locale, so we need our own provider
// to pass it down to child components
const LocaleContext = React.createContext(defaultLang)

export function I18nProvider({ locale = defaultLang, children }) {
const catalog = !!process.env.LOCALES
const catalog = i18nEnabled
? require(`../data/locales/${locale}/messages.js`)
: {}
return (
Expand Down
3 changes: 3 additions & 0 deletions www/src/utils/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ function getLanguages(localeStr) {
const langs = getLanguages(process.env.LOCALES)
const langCodes = langs.map(lang => lang.code)

const i18nEnabled = langs.length > 0

function isDefaultLang(locale) {
return locale === defaultLang
}
Expand Down Expand Up @@ -78,6 +80,7 @@ function getLocaleAndBasePath(path, codes = langCodes) {
}

module.exports = {
i18nEnabled,
langCodes,
langs,
defaultLang,
Expand Down

0 comments on commit 7f6b27f

Please sign in to comment.