How to enable i18n in next.js #4983
Replies: 11 comments 22 replies
-
What are the versions of |
Beta Was this translation helpful? Give feedback.
-
I have a similar issue while implementint next-i18next in a nx workspace, but works outside nx workspace. next: 10.2.0 At first I got the same error : next.config.js
next-i18next.config.js
_app.tsx
index.tsx
|
Beta Was this translation helpful? Give feedback.
-
I'm experiencing the same problem: "Error: next-i18next was unable to find a user config" "next": "10.0.1", next.config.js
next-i18next.config.js
_app.tsx
index.tsx
|
Beta Was this translation helpful? Give feedback.
-
Because |
Beta Was this translation helpful? Give feedback.
-
had the same problem with nx and nextjs. and migrated to next-translate. Yes migration was a bit of a drag but in the end everything works ok |
Beta Was this translation helpful? Give feedback.
-
This is my current work around:
And so far I have to use this config in |
Beta Was this translation helpful? Give feedback.
-
I created a sample repo nx-next-i18next-standalone that have next-i18next "well-behave" within docker image. Thanks to all comments from this thread! |
Beta Was this translation helpful? Give feedback.
-
Managed to do in this way import { UserConfig } from 'next-i18next';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
import nextI18NextConfig from '../../../next-i18next.config';
/**
* ssr translations with default config
* @param locale
* @param namespacesRequired
* @param configOverride
* @param extraLocales
*/
export const ssrTranslations = async (
locale: string,
namespacesRequired?: string[] | undefined,
configOverride?: UserConfig | null,
extraLocales?: string[] | false,
) => serverSideTranslations(locale, namespacesRequired, nextI18NextConfig ?? configOverride, extraLocales); usage in page // and i can use my util in page
export async function getStaticProps({ locale }) {
return {
props: {
...(await ssrTranslations(locale, ['common', 'home'])),
// Will be passed to the page component as props
},
};
} This is my 'next-i18next.config.js' file module.exports = {
i18n: {
defaultLocale: 'en',
locales: ['en'],
},
reloadOnPrerender: process.env.NODE_ENV === 'development',
localePath: 'apps/app-name/public/locales', //note: using absolute path
}; |
Beta Was this translation helpful? Give feedback.
-
For me it works with the following line in the
together with this
|
Beta Was this translation helpful? Give feedback.
-
If you're using getServerSideProps and facing issues with next-i18next in a production environment like Docker, // next-i18next.config.js
const { resolve } = require('path');
/** @type {import('next-i18next').UserConfig} */
module.exports = {
i18n: {
locales: ['zh-TW', 'cn-CH', 'en-US', 'ja-JP', 'ko-KR'],
defaultLocale: 'zh-TW',
},
//* The default value is 'common', set it to the name of one of our existing JSON files, otherwise it will complain that it can't read 'common.json'
defaultNS: 'Warning',
//* When running nx serve or nx build, use the following line to read i18n JSON files
localePath: resolve('./apps/app-name/public/locales'),
//* When building a Docker image or container, use the line below; otherwise, getServerSideProps won't read the JSON files
// localePath:
// typeof window === 'undefined' ? resolve('./public/locales') : '/locales',
}; |
Beta Was this translation helpful? Give feedback.
-
According to current next.js documentation to add i18n support describe as following,
Adding i18n to config.js,
module.exports = withNx({ i18n: { defaultLocale: "en", locales: ["en", "de"], }, })
Adding,
export default appWithTranslation(CustomApp)
to _app.tsx
And
export const getStaticProps = async ({ locale }) => ({ props: { ...(await serverSideTranslations(locale, ["common"])), }, })
to specific page and usage by,const { t } = useTranslation("common")
However this produce following error,
Error: Initial locale argument was not passed into serverSideTranslations
Beta Was this translation helpful? Give feedback.
All reactions