Replies: 6 comments 8 replies
-
I would also like to have this in the
|
Beta Was this translation helpful? Give feedback.
-
@maktouch I opened an issue (which got moved to a discussion) for that specific case here #18329 |
Beta Was this translation helpful? Give feedback.
-
@Timer @tim-phillips @ijjk Is there any reason that this is not in Next 10.0.x? I will try to take some time to contribute to this, I think that although is better to use getServerSideProps instead of getInitialProps, this blocks large projects from having to continue using i18n in a custom server instead of using this i18n routing feature. This is because changing the getInitialProps on all pages can be very difficult: either because you use HOCs from third party libraries, or because you have different logic between client and server (e.g. skeletons for SPA navigation and displaying all content rendered from SSR). |
Beta Was this translation helpful? Give feedback.
-
I needed this feature too, because I wanted to stick to DRY; Repeating i18n data fetching in every getStaticProps seemed like a bad practice. So I came up with a workaround, a bit hacky, but maybe it helps someone:
import Head from 'next/head';
import type { AppProps } from 'next/app';
export default (
function FeedbaxApp(props: AppProps): JSX.Element {
const { Component, pageProps } = props;
return (
<>
<Head>
<meta data-locale={props.router.locale} />
</Head>
<Component {...pageProps} />
</>
);
}
);
import Document from 'next/document';
import { Html, Head, Main, NextScript } from 'next/document';
import fs from 'fs';
import path from 'path';
import type { DocumentContext } from 'next/document';
export default (
class FeedbaxDocument extends Document<{ translation: unknown }> {
static async getInitialProps(context: DocumentContext) {
const initialProps = await Document.getInitialProps(context);
const currentLocaleElement = initialProps.head?.find((el) => 'data-locale' in el?.props);
const currentLocale = currentLocaleElement?.props['data-locale'];
const translationsDir = path.resolve(process.cwd(), 'src/utils/i18n/locales');
const translationDir = path.join(translationsDir, currentLocale);
const translationPath = path.join(translationDir, '__generated/translation.json');
const translationData = fs.readFileSync(translationPath, 'utf-8');
const translation = JSON.parse(translationData);
return { ...initialProps, translation };
}
render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
<script
id="__I18N_DATA__"
dangerouslySetInnerHTML={{
__html: `window.translation = ${JSON.stringify(this.props.translation)};`,
}}
/>
</body>
</Html>
)
}
}
); And then I've got some export function useTranslation(): TranslationHook {
const { locale, locales } = useRouter();
let translation: Translation;
if (process.browser) {
translation = (window as any).translation ?? {};
} else {
translation = require(`@/utils/i18n/locales/${locale}/__generated/translation.json`);
}
const translate = useCallback(
(a: string, b?: string, c?: string) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const translationAny = translation as any;
if (typeof a === 'undefined') return translationAny;
if (typeof b === 'undefined') return translationAny[a];
if (typeof c === 'undefined') return translationAny[a][b];
return translationAny[a][b][c];
},
[translation],
);
return translate;
} This leads to working i18n ssr & hydration without repeating yourself all the time. |
Beta Was this translation helpful? Give feedback.
-
Hi, i don't understand why I got this error:
|
Beta Was this translation helpful? Give feedback.
-
This will be fixed after this PR will be merged #21930 |
Beta Was this translation helpful? Give feedback.
-
Feature request
Is your feature request related to a problem? Please describe.
It would be interesting that in the same way that in
getStaticProps
andgetServerSideProps
you can access the locale through the context, you can also access it fromgetInitialProps
.Ideally, all data loading methods:
getStaticProps
,getServerSideProps
,getInitialProps
should have the i18n in the context.This would help many projects to adapt the new i18n routing. Although
getInitialProps
is no longer recommended, many projects still use it. In fact for now many libraries that offered HOC the only method they touched is that one.Describe the solution you'd like
❌
To work in the same way that:
✅
or
✅
Beta Was this translation helpful? Give feedback.
All reactions