Skip to content

i18n next-intl #14906

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

Merged
merged 23 commits into from
Feb 21, 2025
Merged

i18n next-intl #14906

merged 23 commits into from
Feb 21, 2025

Conversation

pettinarip
Copy link
Member

@pettinarip pettinarip commented Feb 14, 2025

Description

This PR implements next-intl replacing the previous i18n library next-i18next. This is the initial step towards the use of the app router.

Changes for this transition

  • Removed the built-in i18n mechanism from nextjs. This is all handled by next-intl and by the page components using getStaticPaths. The load of namespaces logic is now in our control, created a loadNamespaces function.
  • All pages are located in the folder [locale]
  • All translation related hooks were migrated to the exported versions from next-intl. These are useTranslation, useRouter, usePathname, useLocale
  • Current limitation: 404 page can't be translated in this transition.
  • Changed the env var BUILD_LOCALES to NEXT_PUBLIC_BUILD_LOCALES since we need to use it to get all locales used in client components.
  • Updated the Link component to use the i18n navigation components

Things to test

  • Netlify support (bundles sizes, redirects, compression, default locale redirect)
  • Static files urls (pdfs, images in md files)
  • Edit paths (docs & md pages)
  • RTL support
  • Missing translations defaults to English or the searched key
  • Storybook support

Preview link

https://deploy-preview-14906--ethereumorg.netlify.app/

@github-actions github-actions bot added config ⚙️ Changes to configuration files content 🖋️ This involves copy additions or edits dependencies 📦 Changes related to project dependencies documentation 📖 Change or add documentation tooling 🔧 Changes related to tooling of the project translation 🌍 This is related to our Translation Program labels Feb 14, 2025
Copy link

netlify bot commented Feb 14, 2025

Deploy Preview for ethereumorg ready!

Name Link
🔨 Latest commit c16b424
🔍 Latest deploy log https://app.netlify.com/sites/ethereumorg/deploys/67b5c9b0c9c811000833cef0
😎 Deploy Preview https://deploy-preview-14906--ethereumorg.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
7 paths audited
Performance: 50 (🔴 down 3 from production)
Accessibility: 92 (🔴 down 3 from production)
Best Practices: 87 (🔴 down 9 from production)
SEO: 98 (no change from production)
PWA: 59 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

@pettinarip pettinarip marked this pull request as ready for review February 18, 2025 10:44
@pettinarip pettinarip changed the title i18n next-intl i18n next-intl Feb 18, 2025
@@ -59,7 +61,6 @@ module.exports = (phase, { defaultConfig }) => {

return config
},
i18n,
Copy link
Member Author

Choose a reason for hiding this comment

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

Important change. This is not supported in the app router, removing now and building the pages using getStaticPaths

href={imgSrc}
onClick={matomoHandler}
target="_blank"
locale={false}
Copy link
Member Author

Choose a reason for hiding this comment

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

Modified the Link component to handle this case.

Copy link
Member Author

Choose a reason for hiding this comment

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

Im leaving this for this transition. Should be fixed once we moved to the app router.

For context, the main purpose of this was to remove the query params from the url once the page loads.

Copy link
Member Author

Choose a reason for hiding this comment

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

Important change. Adaptation of the Translation component. It changes a bit the API.

Copy link
Member Author

Choose a reason for hiding this comment

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

Important change.

if (isInternalPdf) {
href = getRelativePath(asPath, href)
if (isInternalFile && !href.startsWith("/")) {
href = "/" + getRelativePath(pathname, href)
Copy link
Member Author

Choose a reason for hiding this comment

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

Convert relative paths into absolute paths

@@ -112,15 +113,11 @@ export const BaseLink = forwardRef<HTMLAnchorElement, LinkProps>(function Link(
)
}

if (isInternalPdf) {
if (isInternalFile) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Uses this strat for all files, not just pdfs. This adds support for opening md images and assets in /assets.

Copy link
Member Author

Choose a reason for hiding this comment

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

Created this abstraction to add backwards compatibility with the args that we could handle, making the transition easier.

Copy link
Member Author

Choose a reason for hiding this comment

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

Loading the namespace is not handled by the i18n library.

This logic tries to mimic what we were doing before:

  • tries to load the namespace for the locale + the default locale, and merges both to default to english in missing translations
  • if the namespace doesn't exist, it loads the default one

Copy link
Member Author

Choose a reason for hiding this comment

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

Can be ignored. Not important at this point since this is used for app router pages.

Copy link
Member Author

Choose a reason for hiding this comment

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

We don't need to handle the default locale scenario.

@@ -96,7 +98,16 @@ const H3 = (props: ChildOnlyProp) => (
<h3 className="mb-0 mt-10 leading-xs" {...props} />
)

export const getStaticProps = (async ({ locale }) => {
export async function getStaticPaths() {
Copy link
Member Author

Choose a reason for hiding this comment

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

All pages now needs to export the expected paths

}

export const getStaticProps = (async ({ params }) => {
const { locale = DEFAULT_LOCALE } = params || {}
Copy link
Member Author

Choose a reason for hiding this comment

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

The locale is now passed as a param as its not part of the context anymore.

Comment on lines +38 to +39
// Suppress errors by default, enable if needed to debug
// console.error(error)
Copy link
Member Author

Choose a reason for hiding this comment

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

Once we fix all our missing translations, we could enable this and let the build failed. For now, we are suppressing them as we are doing currently.

}}
getMessageFallback={({ key }) => {
const keyOnly = key.split(".").pop()
return keyOnly || key
Copy link
Member Author

Choose a reason for hiding this comment

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

If a missing translation, output just the key without the namespace.

@pettinarip pettinarip merged commit 2626732 into dev Feb 21, 2025
8 of 10 checks passed
@pettinarip pettinarip deleted the i18n-next-intl branch February 21, 2025 15:34
This was referenced Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
config ⚙️ Changes to configuration files content 🖋️ This involves copy additions or edits dependencies 📦 Changes related to project dependencies documentation 📖 Change or add documentation tooling 🔧 Changes related to tooling of the project translation 🌍 This is related to our Translation Program
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants