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

refactor: next-i18next serverSideTranslations wrapper #429

Merged
merged 1 commit into from
Jun 23, 2023
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
56 changes: 56 additions & 0 deletions src/lib/utils/next-i18next-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { FlatNamespace } from 'i18next'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'

import nextI18nextConfig from '../../../next-i18next.config'

/**
* The default namespaces for translations.
*/
export const defaultNamespaces: Array<FlatNamespace> = ['common']

/**
* Retrieves namespaces based on the provided requirements.
* Note: The namespace from {@link defaultNamespaces} will always be added.
* @param namespacesRequired The namespaces required.
* @returns The retrieved namespaces.
*/
export const getNamespaces = (
namespacesRequired:
| Readonly<FlatNamespace>
| ReadonlyArray<FlatNamespace>
| undefined = undefined
) => {
// default with namespaces to always needed
const namespaces: Array<FlatNamespace> = defaultNamespaces

if (!namespacesRequired) {
return namespaces
}

if (typeof namespacesRequired === 'string') {
return [...new Set([...namespaces, namespacesRequired])]
}

namespacesRequired
return [...new Set([...namespaces, ...namespacesRequired])]
}

/**
* A wrapper function for server-side translations using next-i18next.
* @param locale - The locale to use for translations. If not provided, the default locale specified in `nextI18nextConfig` will be used.
* @param namespacesRequired - The namespaces required for translations. It can be a single `FlatNamespace` or an array of `FlatNamespace`. If not provided, {@link defaultNamespaces} will be used.
* @returns {Promise<object>} - A Promise that resolves to an object containing the translations for the specified locale and namespaces.
*/
export const pageWithServerSideTranslations = async (
locale?: string,
namespacesRequired:
| FlatNamespace
| Array<FlatNamespace>
| undefined = undefined
) => {
return serverSideTranslations(
locale ?? nextI18nextConfig.i18n.defaultLocale,
getNamespaces(namespacesRequired),
nextI18nextConfig
)
}
4 changes: 2 additions & 2 deletions src/pages/email.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
import { useFormik, validateYupSchema, yupToFormErrors } from 'formik'
import { GetServerSideProps } from 'next'
import { Trans, useTranslation } from 'next-i18next'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
import { NextSeo } from 'next-seo'
import { useRouter } from 'next/router'
import * as Yup from 'yup'
Expand All @@ -32,6 +31,7 @@ import Layout from '../components/Layout'
import Modal from '../components/Modal'
import { EmailEsrfApiRequestBody } from '../lib/types'
import useEmailEsrf from '../lib/useEmailEsrf'
import { pageWithServerSideTranslations } from '../lib/utils/next-i18next-utils'
import { getDCTermsTitle } from '../lib/utils/seo-utils'

const initialValues: EmailEsrfApiRequestBody = {
Expand Down Expand Up @@ -330,7 +330,7 @@ const Email: FC = () => {

export const getServerSideProps: GetServerSideProps = async ({ locale }) => ({
props: {
...(await serverSideTranslations(locale ?? 'default', ['common', 'email'])),
...(await pageWithServerSideTranslations(locale, 'email')),
},
})

Expand Down
10 changes: 5 additions & 5 deletions src/pages/expectations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { FC, MouseEventHandler, useCallback } from 'react'
import { setCookie } from 'cookies-next'
import { GetServerSideProps } from 'next'
import { Trans, useTranslation } from 'next-i18next'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
import { NextSeo } from 'next-seo'
import Router from 'next/router'

import ActionButton from '../components/ActionButton'
import AlertSection from '../components/AlertSection'
import ExternalLink from '../components/ExternalLink'
import Layout from '../components/Layout'
import { pageWithServerSideTranslations } from '../lib/utils/next-i18next-utils'
import { getDCTermsTitle } from '../lib/utils/seo-utils'

const Expectations: FC = () => {
Expand Down Expand Up @@ -113,10 +113,10 @@ const Expectations: FC = () => {

export const getServerSideProps: GetServerSideProps = async ({ locale }) => ({
props: {
...(await serverSideTranslations(locale ?? 'default', [
'common',
'expectations',
])),
...(await pageWithServerSideTranslations(
locale ?? 'default',
'expectations'
)),
},
})

Expand Down
7 changes: 2 additions & 5 deletions src/pages/landing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { FC } from 'react'

import { GetServerSideProps } from 'next'
import { Trans, useTranslation } from 'next-i18next'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
import { NextSeo } from 'next-seo'
import Link from 'next/link'

import Collapse from '../components/Collapse'
import ExampleImage from '../components/ExampleImage'
import Layout from '../components/Layout'
import LinkButton from '../components/LinkButton'
import { pageWithServerSideTranslations } from '../lib/utils/next-i18next-utils'
import { getDCTermsTitle } from '../lib/utils/seo-utils'

const Landing: FC = () => {
Expand Down Expand Up @@ -126,10 +126,7 @@ const Landing: FC = () => {

export const getServerSideProps: GetServerSideProps = async ({ locale }) => ({
props: {
...(await serverSideTranslations(locale ?? 'default', [
'common',
'landing',
])),
...(await pageWithServerSideTranslations(locale, 'landing')),
},
})

Expand Down
7 changes: 2 additions & 5 deletions src/pages/status.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
import { useFormik, validateYupSchema, yupToFormErrors } from 'formik'
import { GetServerSideProps } from 'next'
import { Trans, useTranslation } from 'next-i18next'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
import { NextSeo } from 'next-seo'
import Link from 'next/link'
import { useRouter } from 'next/router'
Expand All @@ -34,6 +33,7 @@ import Layout from '../components/Layout'
import Modal from '../components/Modal'
import { CheckStatusApiRequestQuery } from '../lib/types'
import { useCheckStatus } from '../lib/useCheckStatus'
import { pageWithServerSideTranslations } from '../lib/utils/next-i18next-utils'
import { getDCTermsTitle } from '../lib/utils/seo-utils'

const initialValues: CheckStatusApiRequestQuery = {
Expand Down Expand Up @@ -311,10 +311,7 @@ const Status: FC = () => {

export const getServerSideProps: GetServerSideProps = async ({ locale }) => ({
props: {
...(await serverSideTranslations(locale ?? 'default', [
'common',
'status',
])),
...(await pageWithServerSideTranslations(locale, 'status')),
},
})

Expand Down