-
Notifications
You must be signed in to change notification settings - Fork 1
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
Fix example for next-i18next #1917 #1
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
yarnPath: .yarn/releases/yarn-4.0.0-rc.25.cjs | ||
nodeLinker: "node-modules" | ||
nmMode: hardlinks-local |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { FC, ReactNode } from "react"; | ||
import { useTranslation } from "next-i18next"; | ||
|
||
export const Layout: FC<{ children: ReactNode }> = ({ children }) => { | ||
const { t } = useTranslation(); | ||
|
||
return ( | ||
<div> | ||
<div>{t("hello-world")}</div> | ||
{children} | ||
</div> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Create your own loader for getServerSideProps or getStatic... |
||
* Retrieve translations on server-side, wraps next-i18next.serverSideTranslations | ||
* and import next-i18next.config explicitly (prevent dual packaging hazards) | ||
*/ | ||
import type { SSRConfig, UserConfig } from 'next-i18next'; | ||
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'; | ||
import nextI18nextConfig from '../../next-i18next.config'; | ||
|
||
export const getServerSideTranslations = async ( | ||
locale: string, | ||
namespacesRequired?: string[] | Readonly<string[]> | undefined, | ||
configOverride?: UserConfig | null | ||
): Promise<SSRConfig> => { | ||
const override = configOverride ?? nextI18nextConfig; | ||
// Slice needed here cause serverSlideTranslations does not accept Readonly type | ||
return serverSideTranslations(locale, namespacesRequired?.slice(), override); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { getServerSideTranslations } from './getServerSideTranslations'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
module.exports = { | ||
i18n: { | ||
defaultLocale: "en", | ||
locales: ["en", "id"], | ||
defaultLocale: 'en', | ||
locales: ['en', 'id'], | ||
}, | ||
react: { | ||
useSuspense: false, | ||
}, | ||
localePath: typeof window === "undefined" ? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @adrai Can I have your thoughts on this ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this needs to be done? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The localePath for monorepos. The typeof window: A defensive way. Now that next-i18next config is imported in a client side page (_app.tsx), I'm not sure I would rely on webpack/nextjs to ensure it's dropped. I've been hit few times on older nextjs versions (next-i18next supports next > 10) But what I'm not sure
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i18next/next-i18next#1552 (comment) both: here on server side: https://github.com/i18next/next-i18next/blob/master/src/config/createConfig.ts#L128 (for i18next-fs-backend) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great ! So actually it seems it makes sense to test typeof window... Seems confirmed by i18next/next-i18next#1552 (comment) |
||
require('path').resolve('./public/locales'): | ||
'/public/locales' | ||
}; | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import nextI18nConfig from './next-i18next.config.js'; | ||
|
||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = { | ||
reactStrictMode: true, | ||
swcMinify: true, | ||
i18n: nextI18nConfig.i18n, | ||
experimental: { | ||
esmExternals: true, // optional | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional but recommended for future-proof |
||
} | ||
}; | ||
|
||
export default nextConfig; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,23 +3,29 @@ | |
"version": "0.1.0", | ||
"private": true, | ||
"scripts": { | ||
"clean": "rimraf '.next'", | ||
"nuke": "rimraf '.next' '**/node_modules'", | ||
"nuke:node_modules": "rimraf '**/node_modules'", | ||
"dev": "next dev", | ||
"build": "next build", | ||
"start": "next start", | ||
"lint": "next lint" | ||
}, | ||
"dependencies": { | ||
"next": "12.2.3", | ||
"next-i18next": "^11.3.0", | ||
"next": "12.3.1", | ||
"next-i18next": "12.1.0", | ||
"react": "18.2.0", | ||
"react-dom": "18.2.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "18.0.6", | ||
"@types/react": "18.0.15", | ||
"@types/react": "18.0.21", | ||
"@types/react-dom": "18.0.6", | ||
"eslint": "8.20.0", | ||
"eslint-config-next": "12.2.3", | ||
"typescript": "4.7.4" | ||
} | ||
"eslint": "8.25.0", | ||
"eslint-config-next": "12.3.1", | ||
"pnpm-deduplicate": "0.3.1", | ||
"rimraf": "3.0.2", | ||
"typescript": "4.8.4" | ||
}, | ||
"packageManager": "[email protected]" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import "../styles/globals.css"; | ||
import type { AppProps } from "next/app"; | ||
import { appWithTranslation, useTranslation } from "next-i18next"; | ||
import React from "react"; | ||
import { appWithTranslation } from "next-i18next"; | ||
import { Layout } from "../components/layout"; | ||
import nextI18nConfig from '../next-i18next.config'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Load config here |
||
|
||
function MyApp({ Component, pageProps }: AppProps) { | ||
return ( | ||
|
@@ -11,15 +12,4 @@ function MyApp({ Component, pageProps }: AppProps) { | |
); | ||
} | ||
|
||
const Layout: React.FC<{ children: React.ReactNode }> = ({ children }) => { | ||
const { t } = useTranslation(); | ||
|
||
return ( | ||
<div> | ||
<div>{t("hello-world")}</div> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
|
||
export default appWithTranslation(MyApp); | ||
export default appWithTranslation(MyApp, nextI18nConfig); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explicitly add config (possible too to create a file in ./lib/i18n`) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,7 @@ import type { NextPage } from "next"; | |
import Head from "next/head"; | ||
import Image from "next/image"; | ||
import styles from "../styles/Home.module.css"; | ||
|
||
import { serverSideTranslations } from "next-i18next/serverSideTranslations"; | ||
import {getServerSideTranslations} from "../lib/i18n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Import from our own getServerSideTranslation to load the config |
||
|
||
const Home: NextPage = () => { | ||
return ( | ||
|
@@ -76,7 +75,7 @@ export default Home; | |
export async function getStaticProps({ locale }: any) { | ||
return { | ||
props: { | ||
...(await serverSideTranslations(locale)), | ||
...(await getServerSideTranslations(locale)), | ||
// Will be passed to the page component as props | ||
}, | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved layout here, but it does not matter