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

feature(www): Translate text through js-lingui #21633

Merged
merged 28 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
170d72e
WIP lingui integration
tesseralis Feb 21, 2020
08dd899
translate and extract more stuff
tesseralis Feb 21, 2020
fdecff7
remove extraneous
tesseralis Feb 21, 2020
bbae55d
add toggle text
tesseralis Feb 21, 2020
3eb5989
egghead embed
tesseralis Feb 21, 2020
2c686bd
fix tests
tesseralis Feb 21, 2020
5e47b02
rename lingui functions
tesseralis Feb 21, 2020
17474b1
ignore translation files in typo check
tesseralis Feb 21, 2020
2d878dd
Merge branch 'master' into lingui
tesseralis Feb 21, 2020
a18e353
Merge branch 'master' into lingui
tesseralis Feb 21, 2020
b7c886b
build lingui in onPreInit
tesseralis Feb 21, 2020
2c1927e
finish everything
tesseralis Feb 21, 2020
701e375
remove babelrc
tesseralis Feb 21, 2020
5a95af0
add husky
tesseralis Feb 21, 2020
18b045d
add stuff
tesseralis Feb 21, 2020
f655152
redo navigation
tesseralis Feb 21, 2020
ab159be
try to do something
tesseralis Feb 21, 2020
e6e0b2a
remove husky and lint-saged for now
tesseralis Feb 22, 2020
b739698
fix stuff
tesseralis Feb 22, 2020
707083a
Merge branch 'master' into lingui
tesseralis Feb 26, 2020
a4f2134
move preset info to packages.json
tesseralis Feb 27, 2020
386754b
move preset to extractCoptions"
tesseralis Feb 27, 2020
5af54f3
Merge branch 'master' into lingui
tesseralis Mar 2, 2020
822055b
reorganize i18n stuff
tesseralis Mar 2, 2020
6fc6582
Merge branch 'master' into lingui
tesseralis Mar 9, 2020
8ead7e3
fix locale provider
tesseralis Mar 9, 2020
fe2d9e9
move build logic
tesseralis Mar 9, 2020
ce3d606
Merge branch 'master' into lingui
tesseralis Mar 10, 2020
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
4 changes: 4 additions & 0 deletions www/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"plugins": ["macros"],
"presets": [["babel-preset-gatsby"]]
}
4 changes: 4 additions & 0 deletions www/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ src/cache

# Env variables
.env*

# Lingui
src/data/locales/**/messages.js
src/data/locales/_build
7 changes: 7 additions & 0 deletions www/.linguirc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sourceLocale": "en",
"localeDir": "src/data/locales",
"srcPathDirs": ["src/components", "src/pages", "src/templates", "src/views"],
"format": "po",
"sorting": "origin"
}
9 changes: 1 addition & 8 deletions www/i18n.json
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
[
{ "code": "es", "name": "Spanish" },
{ "code": "id", "name": "Indonesian" },
{ "code": "ja", "name": "Japanese" },
{ "code": "pl", "name": "Polish" },
{ "code": "pt-BR", "name": "Brazilian Portuguese" },
{ "code": "zh-Hans", "name": "Simplified Chinese" }
]
[{ "code": "ja", "name": "Japanese" }]
13 changes: 11 additions & 2 deletions www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
"version": "2.0.0",
"author": "Kyle Mathews <mathews.kyle@gmail.com>",
"dependencies": {
"@babel/core": "^7.8.4",
"@emotion/styled": "^10.0.23",
"@jamo/graphql-request": "2.0.2",
"@lingui/react": "^2.9.1",
"@loadable/component": "^5.11.0",
"@mdx-js/mdx": "^1.5.1",
"@reach/skip-nav": "^0.6.2",
Expand Down Expand Up @@ -111,7 +113,7 @@
"main": "n/a",
"private": true,
"scripts": {
"build": "gatsby build",
"build": "lingui compile && gatsby build",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the right place to put this? Can we do prebuild or something?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could install husky and lint-staged to run a pre-commit hook:

gatsby/package.json

Lines 68 to 86 in 3894840

"lint-staged": {
"*.{js,jsx}": [
"eslint --cache --ext .js,.jsx,.ts,.tsx --fix",
"git add"
],
"*.{md,css,scss,yaml,yml}": [
"prettier --write",
"git add"
],
"*.svg": [
"svgo --pretty --indent=2 --config=svgo.yml --multipass",
"git add"
]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged || node scripts/on-lint-error.js"
}
},

Or does this only need to be run before a build and not necessarily a commit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lingui extract is the thing that generates the .po files -- those need to be in the codebase so that translators know what they need to translate, so it needs to be pre-commit.

Should I install husky on www or is there some way to use the one configured on top level?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

www is separate so to speak, so yeah, go ahead with installing it there 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added husky and lint-staged but ran into two issues:

  • I can’t figure out how to tell lint-stage to commit the generated po files.
  • lingui extract takes a few seconds to run, and doesn’t have any options to only run for specific files.

I don’t think its good to run it on every commit, especially since most www changes will not be string related. I was thinking that on build we could watch the translation files and fail build if there are any changes?

"deploy": "gatsby build --prefix-paths && gh-pages -d public",
"develop": "cross-env GATSBY_GRAPHQL_IDE=playground gatsby develop",
"start": "npm run develop",
Expand All @@ -121,11 +123,18 @@
"scrapeStarters": "cd src/data/StarterShowcase && node scraper.js",
"stylelint": "stylelint './src/**/*.js'",
"forestry:preview": "gatsby develop -p 8080 -H 0.0.0.0",
"postinstall": "cd plugins/gatsby-transformer-gatsby-api-calls && npm install"
"postinstall": "cd plugins/gatsby-transformer-gatsby-api-calls && npm install",
"lingui:add-locale": "lingui add-locale",
"lingui:extract": "NODE_ENV=test lingui extract",
"lingui:compile": "lingui compile"
},
"devDependencies": {
"@lingui/cli": "^2.9.1",
"@lingui/macro": "^2.9.1",
"@testing-library/react": "^8.0.9",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^24.9.0",
"babel-plugin-macros": "^2.8.0",
"babel-preset-gatsby": "^0.2.23",
"cross-env": "^5.2.1",
"front-matter": "^2.3.0",
Expand Down
11 changes: 7 additions & 4 deletions www/src/components/dark-mode-toggle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from "react"
import styled from "@emotion/styled"
import { useColorMode } from "theme-ui"
import { t } from "@lingui/macro"
import { withI18n } from "@lingui/react"

import { mediaQueries } from "gatsby-design-tokens/dist/theme-gatsbyjs-org"

Expand Down Expand Up @@ -123,26 +125,27 @@ const MoonMask = styled.div`
width: 24px;
`

function DarkModeToggle() {
function DarkModeToggle({ i18n }) {
const [colorMode, setColorMode] = useColorMode()
const isDark = colorMode === `dark`

function toggleColorMode(event) {
event.preventDefault()
setColorMode(isDark ? `light` : `dark`)
}
const label = isDark ? t`Activate light mode` : t`Activate dark mode`

return (
<IconWrapper
isDark={isDark}
onClick={toggleColorMode}
aria-label={isDark ? `Activate light mode` : `Activate dark mode`}
title={isDark ? `Activate light mode` : `Activate dark mode`}
aria-label={label}
title={label}
>
<MoonOrSun isDark={isDark} />
<MoonMask isDark={isDark} />
</IconWrapper>
)
}

export default DarkModeToggle
export default withI18n(DarkModeToggle)
8 changes: 6 additions & 2 deletions www/src/components/docs-table-of-contents.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/** @jsx jsx */
import { jsx } from "theme-ui"
import { Link } from "gatsby"
import { colors, mediaQueries } from "gatsby-design-tokens/dist/theme-gatsbyjs-org"
import { Trans } from "@lingui/macro"
import {
colors,
mediaQueries,
} from "gatsby-design-tokens/dist/theme-gatsbyjs-org"

function isUnderDepthLimit(depth, maxDepth) {
if (maxDepth === null) {
Expand Down Expand Up @@ -86,7 +90,7 @@ function TableOfContents({ page, location }) {
textTransform: `uppercase`,
}}
>
Table of Contents
<Trans>Table of Contents</Trans>
</h2>
<ul
sx={{
Expand Down
5 changes: 4 additions & 1 deletion www/src/components/guide-list.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react"
import { Trans } from "@lingui/macro"

import docsHierarchy from "../data/sidebars/doc-links.yaml"
import tutorialHierarchy from "../data/sidebars/tutorial-links.yaml"
Expand Down Expand Up @@ -42,7 +43,9 @@ const GuideList = ({ slug }) => {
))
const toc = subitemList.length ? (
<>
<h2>In this section:</h2>
<h2>
<Trans>In this section:</Trans>
</h2>
<ul>{subitemList}</ul>
</>
) : null
Expand Down
7 changes: 4 additions & 3 deletions www/src/components/markdown-page-footer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** @jsx jsx */
import { jsx } from "theme-ui"
import React from "react"
import { Trans } from "@lingui/macro"
import { graphql } from "gatsby"
import EditIcon from "react-icons/lib/md/create"

Expand All @@ -20,9 +21,9 @@ export default class MarkdownPageFooter extends React.Component {
this.props.packagePage ? `packages` : `docs`
}/${this.props.page ? this.props.page.parent.relativePath : ``}`}
>
<EditIcon sx={{ marginRight: 2 }} />
{` `}
Edit this page on GitHub
<Trans>
<EditIcon sx={{ marginRight: 2 }} /> Edit this page on GitHub
</Trans>
</a>
)}
</>
Expand Down
26 changes: 19 additions & 7 deletions www/src/components/navigation-mobile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/** @jsx jsx */
import { jsx } from "theme-ui"
import { Link } from "gatsby"
import { t } from "@lingui/macro"
import { withI18n } from "@lingui/react"

import {
BlogIcon,
Expand Down Expand Up @@ -39,8 +41,15 @@ const MobileNavItem = ({ linkTo, label, icon }) => (
<div>{label}</div>
</Link>
)
const navItems = [
{ id: "docs", text: t`Docs`, icon: DocsIcon },
{ id: `tutorial`, text: t`Tutorials`, icon: TutorialIcon },
{ id: `plugins`, text: t`Plugins`, icon: PluginsIcon },
{ id: `blog`, text: t`Blog`, icon: BlogIcon },
{ id: `showcase`, text: t`Showcase`, icon: ShowcaseIcon },
]

const MobileNavigation = () => (
const MobileNavigation = ({ i18n }) => (
<div
sx={{
alignItems: `center`,
Expand All @@ -62,15 +71,18 @@ const MobileNavigation = () => (
},
}}
>
<MobileNavItem linkTo="/docs/" label="Docs" icon={DocsIcon} />
<MobileNavItem linkTo="/tutorial/" label="Tutorials" icon={TutorialIcon} />
<MobileNavItem linkTo="/plugins/" label="Plugins" icon={PluginsIcon} />
<MobileNavItem linkTo="/blog/" label="Blog" icon={BlogIcon} />
<MobileNavItem linkTo="/showcase/" label="Showcase" icon={ShowcaseIcon} />
{navItems.map(({ id, text, icon }) => (
<MobileNavItem
linkTo={`/${id}/`}
key={id}
text={i18n._(text)}
icon={icon}
/>
))}
</div>
)

export default MobileNavigation
export default withI18n(MobileNavigation)

const styles = {
svg: {
Expand Down
35 changes: 22 additions & 13 deletions www/src/components/navigation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/** @jsx jsx */
import { jsx } from "theme-ui"
import { useColorMode } from "theme-ui"
import { withI18n } from "@lingui/react"
import { t, Trans } from "@lingui/macro"
import GithubIcon from "react-icons/lib/go/mark-github"
import TwitterIcon from "react-icons/lib/fa/twitter"

Expand Down Expand Up @@ -80,7 +82,17 @@ const SocialNavItem = ({ href, title, children }) => (
</a>
)

const Navigation = ({ pathname }) => {
const navItems = [
{ id: "docs", text: t`Docs` },
{ id: "tutorial", text: t`Tutorial` },
{ id: "plugins", text: t`Plugins` },
{ id: "features", text: t`Features` },
{ id: "blog", text: t`Blog` },
{ id: "showcase", text: t`Showcase` },
{ id: "contributing", text: t`Contributing` },
]

const Navigation = ({ pathname, i18n }) => {
const [colorMode] = useColorMode()
const isHomepage = pathname === `/`

Expand Down Expand Up @@ -149,7 +161,7 @@ const Navigation = ({ pathname }) => {
width: [`24px`, null, `auto`],
overflow: [`hidden`, null, `visible`],
}}
aria-label="Gatsby, Back to homepage"
aria-label={i18n._(t`Gatsby, Back to homepage`)}
>
<img
src={colorMode === `light` ? logo : logoInverted}
Expand All @@ -159,13 +171,13 @@ const Navigation = ({ pathname }) => {
maxWidth: `none`,
m: 0,
}}
alt="Gatsby Logo"
alt={i18n._(t`Gatsby Logo`)}
aria-hidden="true"
/>
</Link>
<nav
className="navigation"
aria-label="Primary Navigation"
aria-label={i18n._(t`Primary Navigation`)}
sx={{
display: `none`,
[mediaQueries.md]: {
Expand Down Expand Up @@ -193,14 +205,11 @@ const Navigation = ({ pathname }) => {
},
}}
>
<NavItem linkTo="/docs/">Docs</NavItem>
<NavItem linkTo="/tutorial/">Tutorials</NavItem>
<NavItem linkTo="/plugins/">Plugins</NavItem>
<NavItem linkTo="/features/">Features</NavItem>
<NavItem linkTo="/blog/">Blog</NavItem>
<NavItem linkTo="/showcase/">Showcase</NavItem>
<NavItem linkTo="/contributing/">Contributing</NavItem>
{/* <NavItem linkTo="/starters/">Starters</NavItem> */}
{navItems.map(({ id, text }) => (
<NavItem key={id} linkTo={`/${id}/`}>
<Trans id={text} />
</NavItem>
))}
</ul>
</nav>
<SearchForm key="SearchForm" offsetVertical={navItemTopOffset} />
Expand Down Expand Up @@ -254,4 +263,4 @@ const Navigation = ({ pathname }) => {
)
}

export default Navigation
export default withI18n(Navigation)
16 changes: 11 additions & 5 deletions www/src/components/prev-and-next.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/** @jsx jsx */
import { jsx } from "theme-ui"
import { t, Trans } from "@lingui/macro"
import { withI18n } from "@lingui/react"
import Link from "./localized-link"
import ArrowForwardIcon from "react-icons/lib/md/arrow-forward"
import ArrowBackIcon from "react-icons/lib/md/arrow-back"
Expand All @@ -25,14 +27,14 @@ const prevNextLabelStyles = {
mt: 0,
}

const PrevAndNext = ({ prev = null, next = null, ...props }) => {
const PrevAndNext = ({ prev = null, next = null, i18n, ...props }) => {
if (!prev && !next) {
return null
}

return (
<nav
aria-label="pagination"
aria-label={i18n._(t`pagination`)}
sx={{
[mediaQueries.sm]: {
display: `flex`,
Expand All @@ -45,7 +47,9 @@ const PrevAndNext = ({ prev = null, next = null, ...props }) => {
<div css={{ [mediaQueries.sm]: { width: `48%` } }}>
{prev && (
<Link to={prev.link} sx={prevNextLinkStyles}>
<p sx={prevNextLabelStyles}>Previous</p>
<p sx={prevNextLabelStyles}>
<Trans>Previous</Trans>
</p>
<span
sx={{
[mediaQueries.md]: {
Expand Down Expand Up @@ -76,7 +80,9 @@ const PrevAndNext = ({ prev = null, next = null, ...props }) => {
>
{next && (
<Link to={next.link} sx={prevNextLinkStyles}>
<p sx={prevNextLabelStyles}>Next</p>
<p sx={prevNextLabelStyles}>
<Trans>Next</Trans>
</p>
<span
sx={{
[mediaQueries.md]: {
Expand All @@ -102,4 +108,4 @@ const PrevAndNext = ({ prev = null, next = null, ...props }) => {
)
}

export default PrevAndNext
export default withI18n(PrevAndNext)
12 changes: 8 additions & 4 deletions www/src/components/search-form/search-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { jsx } from "theme-ui"
import React from "react"
import { navigate } from "gatsby"
import { withI18n } from "@lingui/react"
import { t } from "@lingui/macro"

import SearchIcon from "../search-icon"
import { themedInput } from "../../utils/styles"
Expand All @@ -13,7 +15,7 @@ import { Global } from "@emotion/core"

import algoliaStyles from "./algolia-styles"

export default function SearchForm() {
function SearchForm({ i18n }) {
const [focused, setFocus] = React.useState(false)
const [isInit, setInit] = React.useState(false)
const searchInput = React.useRef(null)
Expand Down Expand Up @@ -144,9 +146,9 @@ export default function SearchForm() {
`width ${t.transition.speed.default} ${t.transition.curve.default}, padding ${t.transition.speed.default} ${t.transition.curve.default}`,
}}
type="search"
placeholder={`Search gatsbyjs.org`}
aria-label="Search gatsbyjs.org"
title="Hit 's' to search docs"
placeholder={i18n._(t`Search gatsbyjs.org`)}
aria-label={i18n._(t`Search gatsbyjs.org`)}
title={i18n._(t`Hit 's' to search docs`)}
onFocus={() => setFocus(true)}
onBlur={() => setFocus(false)}
ref={searchInput}
Expand All @@ -156,3 +158,5 @@ export default function SearchForm() {
</form>
)
}

export default withI18n(SearchForm)
Loading