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

Fix example for next-i18next #1917 #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 17 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
**/node_modules
.yarn/*
!.yarn/patches
!.yarn/releases
!.yarn/plugins
!.yarn/sdks
!.yarn/versions
.pnp.*

# testing
/coverage
Expand Down Expand Up @@ -33,3 +38,12 @@ yarn-error.log*

# typescript
*.tsbuildinfo

# IDE
.idea/*
!.idea/modules.xml
!.idea/perso.iml
.project
.classpath
*.launch
*.sublime-workspace
805 changes: 805 additions & 0 deletions .yarn/releases/yarn-4.0.0-rc.25.cjs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions .yarnrc.yml
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
13 changes: 13 additions & 0 deletions components/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { FC, ReactNode } from "react";
Copy link
Author

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

import { useTranslation } from "next-i18next";

export const Layout: FC<{ children: ReactNode }> = ({ children }) => {
const { t } = useTranslation();

return (
<div>
<div>{t("hello-world")}</div>
{children}
</div>
);
};
17 changes: 17 additions & 0 deletions lib/i18n/getServerSideTranslations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
Copy link
Author

Choose a reason for hiding this comment

The 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);
};
1 change: 1 addition & 0 deletions lib/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { getServerSideTranslations } from './getServerSideTranslations';
11 changes: 9 additions & 2 deletions next-i18next.config.js
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" ?
Copy link
Author

Choose a reason for hiding this comment

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

@adrai Can I have your thoughts on this ?

Copy link

Choose a reason for hiding this comment

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

Why does this needs to be done?

Copy link
Author

Choose a reason for hiding this comment

The 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

  • Is localePath used/interpreted by i18next on the client side ? or only on server side ? (I mean by default)

Copy link

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

The 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'
};

10 changes: 0 additions & 10 deletions next.config.js

This file was deleted.

13 changes: 13 additions & 0 deletions next.config.mjs
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
Copy link
Author

Choose a reason for hiding this comment

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

Optional but recommended for future-proof

}
};

export default nextConfig;
20 changes: 13 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
}
18 changes: 4 additions & 14 deletions pages/_app.tsx
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';
Copy link
Author

Choose a reason for hiding this comment

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

Load config here


function MyApp({ Component, pageProps }: AppProps) {
return (
Expand All @@ -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);
Copy link
Author

Choose a reason for hiding this comment

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

Explicitly add config (possible too to create a file in ./lib/i18n`)

5 changes: 2 additions & 3 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Copy link
Author

Choose a reason for hiding this comment

The 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 (
Expand Down Expand Up @@ -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
},
};
Expand Down
Loading