Skip to content

Commit

Permalink
Merge pull request #78 from daritelska-platforma/bug-37-i18n-ssr
Browse files Browse the repository at this point in the history
#37 SSR doesn't work for i18n
  • Loading branch information
kachar authored Mar 2, 2021
2 parents 7882bf4 + 0fb9073 commit 8fdd19e
Show file tree
Hide file tree
Showing 54 changed files with 265 additions and 2,080 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js', 'next.config.js'],
ignorePatterns: ['.eslintrc.js', 'next.config.js', 'next-i18next.config.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
Expand Down
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ A common way to sort the imports in the file is by their source: `external`, `ab

```tsx
import React, { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'

import Nav from 'components/layout/Nav'
import Layout from 'components/layout/Layout'
Expand Down Expand Up @@ -418,7 +418,7 @@ Usage of translation hook `useTranslation` is preferred over usage of `<Trans />
#### Usage in components

```tsx
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'

export default function CustomComponent() {
const { t } = useTranslation()
Expand All @@ -437,13 +437,13 @@ export default function CustomComponent() {

```tsx
import { GetStaticProps } from 'next'
import { serverSideTranslations } from 'common/useNextLocale'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'

import Page from 'components/forgottenPassword/ForgottenPasswordPage'

export const getStaticProps: GetStaticProps = async ({ locale }) => ({
props: {
i18nResources: await serverSideTranslations(locale, ['common', 'auth']), // List used namespaces
...(await serverSideTranslations(locale ?? 'bg', ['common', 'auth'])), // List used namespaces
},
})

Expand Down
7 changes: 7 additions & 0 deletions next-i18next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
i18n: {
localeDetection: false,
defaultLocale: 'bg',
locales: ['bg', 'en'],
},
}
7 changes: 2 additions & 5 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path')
const { i18n } = require('./next-i18next.config')

require('dotenv').config()

Expand All @@ -15,9 +16,5 @@ module.exports = {
API_URL: process.env.API_URL,
APP_URL: process.env.APP_URL,
},
i18n: {
localeDetection: false,
locales: ['bg', 'en'],
defaultLocale: 'bg',
},
i18n,
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
"formik": "^2.2.6",
"highcharts": "^9.0.1",
"highcharts-react-official": "^3.0.0",
"i18next": "^19.8.4",
"mobx": "^6.0.4",
"mobx-react": "^7.0.5",
"next": "^10.0.5",
"next-auth": "^3.4.1",
"next-i18next": "^8.0.6",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-i18next": "^11.8.5",
"react-in-viewport": "^1.0.0-alpha.16",
"sass": "^1.32.4",
"typewriter-effect": "^2.17.0",
Expand Down
3 changes: 3 additions & 0 deletions public/locales/bg/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"changePassword": "Смяна на парола",
"forgottenPassword": "Забравена парола?"
},
"meta": {
"title": "Подкрепи БГ"
},
"values": {
"voluntary": {
"heading": "Доброволно и про-боно",
Expand Down
3 changes: 3 additions & 0 deletions public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"changePassword": "Change your password",
"forgottenPassword": "Forgotten password?"
},
"meta": {
"title": "Podkrepi BG"
},
"values": {
"voluntary": {
"heading": "Voluntary and pro-bono",
Expand Down
2 changes: 1 addition & 1 deletion src/common/form/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
```tsx
import React from 'react'
import * as yup from 'yup'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'
import { Grid, TextField, Button } from '@material-ui/core'

import { AlertStore } from 'stores/AlertStore'
Expand Down
2 changes: 1 addition & 1 deletion src/common/form/validation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { setLocale } from 'yup'
import { TOptions } from 'i18next'
import { TFunction } from 'react-i18next'
import { TFunction } from 'next-i18next'

export type TranslatableField = (string | undefined) | { key: string; values?: TOptions }

Expand Down
81 changes: 0 additions & 81 deletions src/common/useNextLocale.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/components/about/AboutPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Typography } from '@material-ui/core'
import Layout from 'components/layout/Layout'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'

import styles from './about.module.scss'

Expand Down
2 changes: 1 addition & 1 deletion src/components/auth/changePassword/ChangePasswordPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'
import { Container } from '@material-ui/core'

import Layout from 'components/layout/Layout'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import * as yup from 'yup'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'
import { Typography, Grid } from '@material-ui/core'

import SubmitButton from 'components/common/form/SubmitButton'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'
import { Container } from '@material-ui/core'

import Layout from 'components/layout/Layout'
Expand Down
2 changes: 1 addition & 1 deletion src/components/auth/login/LoginForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from 'react'
import * as yup from 'yup'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'
import { Grid } from '@material-ui/core'
import { AlertStore } from 'stores/AlertStore'

Expand Down
2 changes: 1 addition & 1 deletion src/components/auth/login/LoginPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { signIn } from 'next-auth/client'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'
import { Container, Grid, Box, Button } from '@material-ui/core'

import Link from 'components/common/Link'
Expand Down
2 changes: 1 addition & 1 deletion src/components/auth/profile/ProfilePage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'
import {
Avatar,
Box,
Expand Down
2 changes: 1 addition & 1 deletion src/components/auth/register/RegisterPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'
import { Container } from '@material-ui/core'

import Layout from 'components/layout/Layout'
Expand Down
2 changes: 1 addition & 1 deletion src/components/common/form/FormTextField.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import { FormikValues } from 'formik'
import { TextField, TextFieldProps } from '@material-ui/core'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'

import { translateError } from 'common/form/useForm'
import { TranslatableField } from 'common/form/validation'
Expand Down
2 changes: 1 addition & 1 deletion src/components/common/form/SubmitButton.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'
import { makeStyles, createStyles, Theme } from '@material-ui/core/styles'
import { Button, ButtonProps, CircularProgress, CircularProgressProps } from '@material-ui/core'

Expand Down
2 changes: 1 addition & 1 deletion src/components/index/helpers/Typewriter.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Typewriter from 'typewriter-effect'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'

import { Typography } from '@material-ui/core'
import { makeStyles, createStyles } from '@material-ui/core/styles'
Expand Down
2 changes: 1 addition & 1 deletion src/components/index/helpers/chart/TeamPie.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import Highcharts from 'highcharts'
import HighchartsReact from 'highcharts-react-official'
import { teamPieData, teamPieOptions, TeamPieItem } from 'components/index/helpers/chart/pieData'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'
import handleViewport from 'react-in-viewport'

export type TeamPieProps = {
Expand Down
2 changes: 1 addition & 1 deletion src/components/index/sections/ActivitySection.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Grid, Typography } from '@material-ui/core'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'
import { makeStyles, createStyles } from '@material-ui/core/styles'

const useStyles = makeStyles((theme) =>
Expand Down
2 changes: 1 addition & 1 deletion src/components/index/sections/Jumbotron.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'

import { Grid, Typography } from '@material-ui/core'
import { makeStyles, createStyles } from '@material-ui/core/styles'
Expand Down
2 changes: 1 addition & 1 deletion src/components/index/sections/ProblemsToSolveSection.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Grid, Typography } from '@material-ui/core'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'
import { makeStyles, createStyles } from '@material-ui/core/styles'

import CommissionsIcon from '../icons/problems-to-solve-icons/CommissionsIcon'
Expand Down
2 changes: 1 addition & 1 deletion src/components/index/sections/SupportUsSection.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Grid, Typography } from '@material-ui/core'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'
import { makeStyles, createStyles } from '@material-ui/core/styles'

import FinancesIcon from '../icons/support-us-icons/FinancesIcon'
Expand Down
2 changes: 1 addition & 1 deletion src/components/index/sections/TeamChartSection.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Grid, Typography, Box } from '@material-ui/core'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'
import { makeStyles, createStyles } from '@material-ui/core/styles'

import TeamPie from 'components/index/helpers/chart/TeamPie'
Expand Down
2 changes: 1 addition & 1 deletion src/components/index/sections/TeamSection.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Grid, Typography } from '@material-ui/core'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'
import { makeStyles, createStyles } from '@material-ui/core/styles'

const useStyles = makeStyles((theme) =>
Expand Down
8 changes: 6 additions & 2 deletions src/components/layout/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Head from 'next/head'
import { useState } from 'react'
import { useMemo, useState } from 'react'
import { useTranslation } from 'next-i18next'
import {
Box,
Container,
Expand Down Expand Up @@ -49,12 +50,15 @@ export default function Layout({
...containerProps
}: LayoutProps) {
const classes = useStyles()
const { t } = useTranslation()
const [mobileOpen, setMobileOpen] = useState(false)
const navMenuToggle = () => setMobileOpen(!mobileOpen)
const suffix = t('meta.title')
const metaTitle = useMemo(() => (title ? `${title} | ${suffix}` : suffix), [title, suffix])
return (
<Container className={classes.layout} maxWidth={maxWidth} {...containerProps}>
<Head>
<title>{title}</title>
<title>{metaTitle}</title>
</Head>
<Box pt={4} pb={10}>
<AppNavBar navMenuToggle={navMenuToggle} />
Expand Down
2 changes: 1 addition & 1 deletion src/components/layout/LocaleButton.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useCallback } from 'react'
import { useRouter } from 'next/router'
import { Button } from '@material-ui/core'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'

export default function LocaleMenu() {
const router = useRouter()
Expand Down
2 changes: 1 addition & 1 deletion src/components/layout/LocaleMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useRouter } from 'next/router'
import { useCallback, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'
import { Button, Menu, MenuItem } from '@material-ui/core'

export default function LocaleMenu() {
Expand Down
2 changes: 1 addition & 1 deletion src/components/layout/LocaleSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback } from 'react'
import { useRouter } from 'next/router'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'
import { Box, Button, ButtonGroup } from '@material-ui/core'

export default function LocaleSwitcher() {
Expand Down
2 changes: 1 addition & 1 deletion src/components/layout/nav/MainNavMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { Grid } from '@material-ui/core'
import { useTranslation } from 'react-i18next'
import { useTranslation } from 'next-i18next'

import { routes } from 'common/routes'
import LinkButton from 'components/common/LinkButton'
Expand Down
Loading

0 comments on commit 8fdd19e

Please sign in to comment.