SSG - How to localize/i18n the Next.js 404 page? #12477
Replies: 14 comments 20 replies
-
👍 to this question |
Beta Was this translation helpful? Give feedback.
-
Localization is a big pain atm. |
Beta Was this translation helpful? Give feedback.
-
I'm dealing with this issue either. Thanks for informing this as an issue. |
Beta Was this translation helpful? Give feedback.
-
I believe this will be solved when this RFC comes to life. #17078 |
Beta Was this translation helpful? Give feedback.
-
I think that you could send the user to another page when he visits You can use this to do that. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
The solution to this is a bit convoluted but it can be done by passing the translations hash to the 404 page... the static 404 will be build as the fallback language (during build time)... and then pass the desired language to override the 404 page translations when availalbe... this will look something roughly like the following // src/pages/404.tsx
const ErrorPage = ({ translations }: { translations?: Dictionary }) => {
return (
<>translations["pageNotFound"]</>
);
};
// NOTE: nextjs only allow static 404 pages, and they are compiled during build time
export const getStaticProps: GetStaticProps = async () => {
const translations = await getTranslations(Languages.EN);
return {
props: {
translations,
},
};
};
export default ErrorPage; then provide a dynamic catch all route // src/pages/[...lng].tsx
import Error from "@src/pages/404";
const CatchAllPage = ({ translations }: { translations?: Dictionary }) => {
return <Error translations={translations} />;
};
export const getServerSideProps: GetServerSideProps = async ({ params }) => {
const { lng } = params!;
// Parse the URL and fall back to a default language, this to handle the case of /test or /ch/test to fallback to English
// and /fr/test to fallback to `fr`
const language = getErrorPageLanguage(lng![0]);
const translations = await getTranslations(langauge);
return {
props: {
translations,
},
};
};
export default CatchAllPage; and from any route you can basically call the translations dictionary and pass it to the page components to handle routes like the enum Languages {
EN = "en",
DE = "de",
}
export const SUPPORTED_LANGUAGES: string[] = [Languages.EN, Languages.DE];
export const getErrorPageLanguage = (lng: string | string[] | undefined) => {
let language = lng;
if (!SUPPORTED_LANGUAGES.includes(lng as string)) {
language = Languages.EN;
}
return language;
}; TL;DR:
|
Beta Was this translation helpful? Give feedback.
-
Was this ever solved with the new i18 support? |
Beta Was this translation helpful? Give feedback.
-
As per suggested by the documentation for Custom Error Page, it looks like that we should pass a title prop to the Error component. And it seems to be working like a charm by taking this example from next-translate import Error from 'next/error';
import useTranslation from 'next-translate/useTranslation';
export default function Custom404() {
const { t } = useTranslation();
const errorMessage = t`error:404`
return <Error statusCode={404} title={errorMessage} />
} |
Beta Was this translation helpful? Give feedback.
-
Error pages are extremely low value but they should at least be functional. The fact that we have to put in extra effort to setup a different translation system for error pages for this is really bad. |
Beta Was this translation helpful? Give feedback.
-
Is it my impression or this is still a pain with the new app router too ? if i'm not mistaken you can create the file, but you have no way to access the parameters related to the route, like |
Beta Was this translation helpful? Give feedback.
-
For anyone that needs a workaround for app router, there is one possible approach. export default function NotFound() {
// if you dont have this check, it cannot build. because document is not defined in server side.
if (typeof document !== "undefined") {
const currentLang = document.location.pathname.substring(1, 3);
if (currentLang === "en") {
return <div> this is for english</div>;
}
if (currentLang === "ko") {
return <div> this is for korean</div>;
}
if (currentLang === "ja") {
return <div> this is for japanese</div>;
}
} @stormsson Here is a workaround. |
Beta Was this translation helpful? Give feedback.
-
I hope that we could put // layout.tsx
interface Props {
children: React.ReactNode
}
export default function RootLayout({ children }: Props) {
return <>{children}</>
} // not-found.tsx
import { redirect } from 'next/navigation'
export default function NotFound() {
redirect('/not-found')
} or // not-found.tsx
'use client'
import { redirect, usePathname } from 'next/navigation'
const notFoundPath = '/not-found'
export default function NotFound() {
const pathname = usePathname()
// Prevent infinite redirect in case not-found page does not exist
if (pathname === notFoundPath) {
console.error('not-found page does not exist')
redirect('/')
}
redirect(notFoundPath)
} @stormsson hope it helps :) |
Beta Was this translation helpful? Give feedback.
-
I'm using the 404 and 500 pages which are easy to translate because they both can use getStaticProps. But it looks as though lots of people are having an issue setting this up and I'm wondering if I've missed something. In my case:
|
Beta Was this translation helpful? Give feedback.
-
The
pages/404.tsx
page allows to customise the 404 error displayed to the end user.See https://nextjs.org/docs/advanced-features/custom-error-page#404-page
But, since it lives in
/pages
and not/pages/[locale]
as do my other Pages, it doesn't benefit from i18n support.I have successfully displayed the 404 page, but it's the same for all languages (no i18n support).
I tried to use
getStaticPaths
, but gotgetStaticPaths can only be used with dynamic pages, not '/404'.
I'm looking for recommandations about how to build a 404 i18n-friendly page using SSG, it shouldn't be that hard.
See:
https://nrn-v1-hyb-mst-aptd-gcms-lcz-sty-c1-1w6t2bwrm.now.sh/en/test
https://nrn-v1-hyb-mst-aptd-gcms-lcz-sty-c1-1w6t2bwrm.now.sh/test
https://nrn-v1-hyb-mst-aptd-gcms-lcz-sty-c1-1w6t2bwrm.now.sh/fr/test
Beta Was this translation helpful? Give feedback.
All reactions