chapter-three / next-drupal

Next.js for Drupal has everything you need to build a next-generation front-end for your Drupal site: SSG, SSR, and ISR, Multi-site, Authentication, Webforms, Search API, I18n and Preview mode (works with JSON:API and GraphQL).
https://next-drupal.org
MIT License
634 stars 176 forks source link

decoupled_router and locale #433

Open spiderneo opened 1 year ago

spiderneo commented 1 year ago

Hi, i'm currently working to translate my next App

My Drupal is configured to use the URL to set the language image

I found that the function translatePath don't use locale to search for a path. So if i follow the request https://dev.sneo.fr/router/translate-path?path=en will be redirected to FR (which is the default lang) https://dev.sneo.fr/fr/router/translate-path?path=en and no result is found.

if I use the EN prefix instead it work and the EN version of my page is returned https://dev.sneo.fr/en/router/translate-path?path=en

Do i missed something ? or is it the normal behavior ?

https://github.com/chapter-three/next-drupal/blob/7f27653649a8f62b2d33bf1634c1083151d62b2b/packages/next-drupal/src/client.ts#L904-L931

spiderneo commented 1 year ago

For those who are interested, this is my patch.

add_locale_to_translatePath.patch

p0zi commented 1 year ago

I checked you patch but it was not working for me. This is for sure problem with path resolution in decoupled_router.

I had the same issue, i have 2 languages in front, one is EN, it was set as default in front and backend. Drupal does not recommend to switch language after configuration of "Content language and translation". Moreover i am using Paragraphs module in Drupal, which has specific apporach to language handling. Since i use the same default language in both frontend and backend all works good, second language, non default is also working. But when i change in front default language to other one translation is not working i am getting for both EN version. The way i solved an issue i by using approach with "default" locale, which is descibed here: https://github.com/vercel/next.js/discussions/18419

Hardcoded en you can switch to whatever default language (i called it defaultLanguagePseudo) should it be for your page. Do not confuse this defaultLanguagePseudo with defaultLanguage which i left both in frontend adn backend to EN.

MartinDavi commented 1 year ago

@shadcn What're your thoughts on this? Seems like translatePath should maybe take locale into account, like in the patch from @spiderneo

Reason being, if I have a node that is only in en-ca, not fr-ca, and en-ca is the Drupal default language, then all requests to the router hit the /en-ca/router/translate-path:

/en-ca/router/translate-path?path=/fr-ca/english-only&_format=json -> this returns the english node, which for our fr-ca site, I don't want to show. /fr-ca/router/translate-path?path=/fr-ca/english-only&_format=json -> this returns:

{
"message": "Unable to resolve path /fr-ca/english-only.",
"details": "None of the available methods were able to find a match for this path."
}

as I would expect.

Now, I think we've done some work in Drupal not to return default language content if content for the requesting locale doesn't exist - but I don't know enough about decoupled_router to know if it should always be getting queried with the correct locale as prefix.

Anyway I'm gonna try out that patch above, but figured you'd have more insight.