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

Add translations #8

Merged
merged 5 commits into from
Jul 12, 2020
Merged
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
18 changes: 13 additions & 5 deletions components/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Navbar as BPNavbar, Alignment, AnchorButton, Menu, Popover } from "@blueprintjs/core"
import { Navbar as BPNavbar, Alignment, AnchorButton, Menu, Popover, HTMLSelect } from "@blueprintjs/core"
import Head from "next/head"
import { FunctionComponent, useContext } from "react"
import { Box } from "rebass/styled-components"
Expand All @@ -8,6 +8,7 @@ import styled from "styled-components"
import UserContext from "lib/UserProvider"
import { icon } from "lib/icons"
import createLoginUrl from "lib/url-helpers"
import i18n from 'i18n'

const Navbar = styled(BPNavbar)`
background-color: ${(props) => props.theme.backgroundColor}
Expand All @@ -16,9 +17,10 @@ const Navbar = styled(BPNavbar)`
const Layout: FunctionComponent = ({ children }) => {
const router = useRouter()
const user = useContext(UserContext)
const [t, { language }] = i18n.useTranslation()

const userMenu = <Menu>
<Menu.Item icon={icon("log-out")} text="Logout" href="/api/logout" />
<Menu.Item icon={icon("log-out")} text={t('logout')} href="/api/logout" />
</Menu>

return (
Expand All @@ -30,18 +32,24 @@ const Layout: FunctionComponent = ({ children }) => {
<Popover content={userMenu}>
<AnchorButton minimal={true} icon={icon("user")} text={user.nickname} />
</Popover>
: <AnchorButton minimal={true} icon={icon("log-in")} text="Login" href={createLoginUrl()} />}
: <AnchorButton minimal={true} icon={icon("log-in")} text={t('login')} href={createLoginUrl(language)} />}
</Navbar.Heading>

<Navbar.Divider />

<HTMLSelect minimal={true} options={['en', 'it', 'ja']} value={language} onChange={(event) => {
i18n.i18n.changeLanguage(event.target.value)
}}/>

{user &&
<>
<Navbar.Divider />

<Link href="/recipes">
<AnchorButton minimal={true} icon={icon("recipe")} text="Recipes" disabled={router.pathname === '/recipes'} />
<AnchorButton minimal={true} icon={icon("recipe")} text={t('recipes')} disabled={router.pathname === '/recipes'} />
</Link>
<Link href="/recipes/new">
<AnchorButton minimal={true} icon={icon("add")} text="Add" disabled={router.pathname === '/recipes/new'} />
<AnchorButton minimal={true} icon={icon("add")} text={t('add')} disabled={router.pathname === '/recipes/new'} />
</Link>
</>}
</Navbar.Group>
Expand Down
32 changes: 14 additions & 18 deletions components/RecipeDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FunctionComponent, useState } from 'react'
import { useRouter } from 'next/router'
import { RecipeFieldsFragment, IngredientGroup, IngredientFieldsFragment } from 'lib/graphql'
import { Box } from 'rebass/styled-components'
import { labelForIngredientGroup, ingredientTypeIcon, labelForIngredientType, starterIngredients as filterStarterIngredients, doughIngredients as filterDoughIngredients } from 'lib/ingredients'
import { ingredientTypeIcon, starterIngredients as filterStarterIngredients, doughIngredients as filterDoughIngredients } from 'lib/ingredients'
import Ingredient from './Ingredient'
import { round, lowerCase } from 'lodash'
import Totals from 'components/form/Totals'
Expand All @@ -12,6 +12,7 @@ import { Button } from 'components/base/Button'
import CopyToClipboard from 'react-copy-to-clipboard'
import { recipeShareUrl } from 'lib/url-helpers'
import { icon } from 'lib/icons'
import i18n from 'i18n'

interface Props {
recipe: RecipeFieldsFragment,
Expand All @@ -28,11 +29,13 @@ const RecipeDetail: FunctionComponent<Props> = ({ recipe, shared }) => {

const ingredientItem = (ingredient: IngredientFieldsFragment) => {
return <Ingredient
name={ingredient.name ?? lowerCase(labelForIngredientType(ingredient.type))}
name={ingredient.name ?? lowerCase(t(ingredient.type))}
weight={`${round(ingredient.weight)} g`}
icon={ingredientTypeIcon(ingredient.type, { size: 25, style: { marginRight: 15 } })} />
}

const [t] = i18n.useTranslation()

return (
<>
{recipe &&
Expand All @@ -49,12 +52,12 @@ const RecipeDetail: FunctionComponent<Props> = ({ recipe, shared }) => {
}} />

<Box mt={4}>
<H2>Ingredients</H2>
<H2>{t('ingredients')}</H2>
</Box>

{starterIngredients.length > 0 &&
<Box mt={4}>
<H3>{labelForIngredientGroup(IngredientGroup.starter)}</H3>
<H3>{t(IngredientGroup.starter)}</H3>

<Box mt={3}>
{starterIngredients.map((ingredient, index) => {
Expand All @@ -68,7 +71,7 @@ const RecipeDetail: FunctionComponent<Props> = ({ recipe, shared }) => {

{doughIngredients.length > 0 &&
<Box mt={4}>
<H3>{labelForIngredientGroup(IngredientGroup.dough)}</H3>
<H3>{t(IngredientGroup.dough)}</H3>

<Box mt={3}>
{doughIngredients.map((ingredient, index) => {
Expand All @@ -81,18 +84,11 @@ const RecipeDetail: FunctionComponent<Props> = ({ recipe, shared }) => {
}

<Box mt={4}>
<H2>Steps</H2>
<H2>{t('steps')}</H2>
</Box>

<Box mt={4}>
<HTMLTable striped={true}>
<thead>
<tr>
<th>Description</th>
<th>Time</th>
</tr>
</thead>

<tbody>
{recipe.steps.length > 0 && (
recipe.steps.sort((a, b) => a.position - b.position).map((step) => (
Expand All @@ -118,18 +114,18 @@ const RecipeDetail: FunctionComponent<Props> = ({ recipe, shared }) => {
<Box mt={4}>
<ButtonGroup vertical={true}>
<Button icon={icon("edit")} onClick={() => router.push('/recipes/[id]/edit', `/recipes/${recipe.id}/edit`)}>
Edit
{t('edit')}
</Button>

<Button icon={icon("share")} intent="primary" onClick={() => setIsDialogOpen(true)}>
Share
{t('share')}
</Button>
</ButtonGroup>

<Dialog
icon={icon("share")}
onClose={() => setIsDialogOpen(false)}
title={`Share ${recipe.name}`}
title={t('share-title', { name: recipe.name })}
isOpen={isDialogOpen}
>
<div className={Classes.DIALOG_BODY}>
Expand All @@ -138,9 +134,9 @@ const RecipeDetail: FunctionComponent<Props> = ({ recipe, shared }) => {

<div className={Classes.DIALOG_FOOTER}>
<div className={Classes.DIALOG_FOOTER_ACTIONS}>
<Button onClick={() => setIsDialogOpen(false)}>Close</Button>
<Button onClick={() => setIsDialogOpen(false)}>{t('close')}</Button>
<CopyToClipboard text={recipeShareUrl(recipe.token)} onCopy={() => setIsDialogOpen(false)}>
<Button icon={icon("share")}>Copy</Button>
<Button icon={icon("share")}>{t('copy')}</Button>
</CopyToClipboard>
</div>
</div>
Expand Down
13 changes: 9 additions & 4 deletions components/RecipeList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import { Spinner, NonIdealState, AnchorButton } from '@blueprintjs/core'
import RecipeItem from './RecipeItem'
import Link from 'next/link'
import { icon } from 'lib/icons'
import i18n from 'i18n'

const EmptyList = () => {
const [t] = i18n.useTranslation()

return (
<NonIdealState
icon={icon("recipe")}
title="You have no saved recipes"
description="Create a new recipe"
action={<Link href="/recipes/new"><AnchorButton intent="primary" text="Create"/></Link>} />
title={t('recipes-empty-state.title')}
description={t('recipes-empty-state.description') as string}
action={<Link href="/recipes/new"><AnchorButton intent="primary" text={t('add')}/></Link>} />
)
}

Expand All @@ -21,8 +24,10 @@ const RecipeList = () => {
error
} = useGetRecipesQuery()

const [t] = i18n.useTranslation()

if (loading) return <Spinner />
if (error) return <p>Error Loading Recipes</p>
if (error) return <p>{t('recipes-error')}</p>

const recipes = data?.recipes

Expand Down
6 changes: 4 additions & 2 deletions components/form/DeleteButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Classes, Dialog } from "@blueprintjs/core"
import { useRouter } from "next/router"
import { Button } from "components/base/Button"
import { icon } from "lib/icons"
import i18n from "i18n"

interface Props {
recipe: RecipeFieldsFragment
Expand All @@ -13,6 +14,7 @@ const DeleteButton: FunctionComponent<Props> = ({ recipe }) => {
const [removeRecipe, { loading }] = useRemoveRecipeMutation()
const router = useRouter()
const [isDialogOpen, setIsDialogOpen] = useState(false)
const [t] = i18n.useTranslation()

const handleRemoveClick = () => {
removeRecipe({
Expand Down Expand Up @@ -42,7 +44,7 @@ const DeleteButton: FunctionComponent<Props> = ({ recipe }) => {
return (
<>
<Button icon={icon("trash")} intent="danger" onClick={() => setIsDialogOpen(true)}>
Delete
{t('delete')}
</Button>

<Dialog
Expand All @@ -60,7 +62,7 @@ const DeleteButton: FunctionComponent<Props> = ({ recipe }) => {
<Button onClick={() => setIsDialogOpen(false)}>Cancel</Button>

<Button icon={icon("trash")} loading={loading} intent="danger" onClick={handleRemoveClick}>
Delete
{t('delete')}
</Button>
</div>
</div>
Expand Down
9 changes: 6 additions & 3 deletions components/form/IngredientField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FunctionComponent } from "react";
import { IngredientType } from "lib/graphql";
import { FastField as Field, FieldProps, getIn, FormikHelpers, FormikErrors, FormikTouched } from "formik";
import { Tag } from "@blueprintjs/core";
import { labelForIngredientType, nameRequiredForType } from "lib/ingredients";
import { nameRequiredForType } from "lib/ingredients";
import { FormValues, IngredientFormField } from "./RecipeForm";
import { Flex, Box } from "rebass/styled-components";
import { lowerCase } from "lodash";
Expand All @@ -11,6 +11,7 @@ import { Button } from "components/base/Button";
import { icon } from "lib/icons";
import { wrapNullableValue } from "lib/field-helpers";
import { TextInput } from "components/base/TextInput";
import i18n from "i18n";

interface Props {
prefix: string,
Expand All @@ -30,6 +31,8 @@ export const IngredientField: FunctionComponent<Props> = ({ prefix, index, setFi
const nameIntent = getIn(errors, `${prefix}Ingredients.${index}.name`) && getIn(touched, `${prefix}Ingredients.${index}.name`) ? "danger" : "none"
const disabled = typeof onRemove === 'undefined'

const [t] = i18n.useTranslation()

return (
<Flex mb={2} alignItems="center">
<Box mr={2}>
Expand Down Expand Up @@ -59,7 +62,7 @@ export const IngredientField: FunctionComponent<Props> = ({ prefix, index, setFi

{type !== IngredientType.other &&
<Box mr={2}>
of {lowerCase(labelForIngredientType(type))}
{lowerCase(t(type))}
</Box>}
</Flex>
</Box>
Expand All @@ -74,7 +77,7 @@ export const IngredientField: FunctionComponent<Props> = ({ prefix, index, setFi
onChange={field.onChange}
onBlur={field.onBlur}
name={field.name}
placeholder="Name" />
placeholder={type === IngredientType.flour ? t('flour-placeholder') : t('other-placeholder')} />
)}
</Field>
</Box>}
Expand Down
Loading