next-translate icon indicating copy to clipboard operation
next-translate copied to clipboard

PoC: Translations for Next.js fallback pages

Open berndartmueller opened this issue 3 years ago • 5 comments

Fixes https://github.com/vinissimus/next-translate/issues/575

See https://nextjs.org/docs/basic-features/data-fetching#fallback-pages

Page props on Next.js fallback pages are empty ({}). Therefore translations are also missing.

My idea to circumvent this is providing a new HOC withFallbackTranslation() to wrap any Next.js page which should have fallback translations. This HOC adds a static function __PAGE_NEXT_NAMESPACES to the component. This function is called within the appWithI18n component.

As fallback pages do not provide any way to use async operations, I had to add a way to load translations sync. That's why I also added a new config property loadLocaleFromSync.

To demonstrate that it works, I added a new route to examples/complext demo -> http://localhost:3000/more-examples/fallback/example-not-exists

Here are two Gifs to better show the effect:

Default behaviour for translations on fallback pages.

Notice the flicker while it defaults to the translation key.

Route: http://localhost:3000/more-examples/dynamicroute/example11?another=another+param

default

Fallback pages wrapped with HOC withFallbackTranslation

No flicker at all. The translations are loaded from the beginning on.

Route: http://localhost:3000/more-examples/fallback/example-not-exists?another=another+param

fallback-translations

This PR should be seen as a proof of concept. There are certainly a few things that can and should be refactored/improved.

I'm looking forward to your feedback.

berndartmueller avatar Apr 24 '21 09:04 berndartmueller

Did anyone of the maintainers already checkout this PR? I would be very interested in your feedback :)

berndartmueller avatar May 11 '21 16:05 berndartmueller

@aralroca Was there any particular reason why you closed this PR?

berndartmueller avatar May 22 '21 11:05 berndartmueller

@berndartmueller Sorry, I accidentally deleted the canary branch in the last release and the PR was closed automatically. I reopen it

aralroca avatar May 22 '21 22:05 aralroca

@aralroca Is there any interest in this PR/Feature?

berndartmueller avatar Nov 07 '21 15:11 berndartmueller

@berndartmueller Sorry, I removed the canary branch when I made the release by accident and the PRs that were open have been closed. I am committed to review the PR during this week. Sorry for the delay. And thank you very much for the contribution. 👏

aralroca avatar Nov 07 '21 15:11 aralroca