Skip to content
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

Unify frontend and backend language preference using shared cookie #333

Open
virgile-dev opened this issue Oct 14, 2024 · 2 comments
Open
Assignees
Labels
backend bug Something isn't working frontend good first issue Good for newcomers i18n python Pull requests that update Python code refacto

Comments

@virgile-dev
Copy link
Collaborator

virgile-dev commented Oct 14, 2024

Bug Report

Problematic behavior
Here you can see "Doc private" appears in English but the user interface is in French.
https://github.com/user-attachments/assets/0ae0e457-15f7-423c-b4bc-c1c5fc9d48bc

Same when I'm loading a doc I don't have access to. The message only is available in French even if I sent the interface in English and reload.
Capture d’écran du 2024-10-14 17-26-11

@virgile-dev virgile-dev changed the title "Doc private" isn't available as a translation string in crowdin Missing strings in crowdin Oct 14, 2024
@rvveber rvveber self-assigned this Oct 15, 2024
@rvveber
Copy link
Contributor

rvveber commented Oct 16, 2024

This seems to be an issue with the backend not beeing communicated the current language to.

Here is how Django determines the current language:
https://docs.djangoproject.com/en/5.1/topics/i18n/translation/#how-django-discovers-language-preference

First, it looks for the language prefix in the requested URL. This is only performed when you are using the i18n_patterns function in your root URLconf. See Internationalization: in URL patterns for more information about the language prefix and how to internationalize URL patterns.

Failing that, it looks for a cookie.
The name of the cookie used is set by the LANGUAGE_COOKIE_NAME setting. (The default name is django_language.)

Failing that, it looks at the Accept-Language HTTP header. This header is sent by your browser and tells the server which language(s) you prefer, in order by priority. Django tries each language in the header until it finds one with available translations.

Failing that, it uses the global LANGUAGE_CODE setting.

For me the message is always english, which means that Django determines it by
Accept-Language HTTP header.

We currently dont use language patterns e.g./fr in the url

So the best option, would be to set a cookie with the language code when switching the language in the frontend. The default name of the cookie is django_language but the name is configurable via the django configuration option LANGUAGE_COOKIE_NAME

Next.JS accepts a special cookie which needs to be called NEXT_LOCALE
https://nextjs.org/docs/pages/building-your-application/routing/internationalization#leveraging-the-next_locale-cookie
We could set the cookie NEXT_LOCALE on language change and configure django to use that same cookie name to determine its preferred language.

We would have only one cookie for frontend and backend language.
Both respecting the value.
This is the solution i'd like to implement.

(Currently, we have no cookie for this, and we save the language in local storage.)
@AntoLC can i refactor to use this cookie instead?

@rvveber rvveber changed the title Missing strings in crowdin Unify frontend and backend language preference using shared cookie Oct 17, 2024
@rvveber rvveber added backend refacto python Pull requests that update Python code labels Oct 17, 2024
@virgile-dev virgile-dev removed the next label Oct 17, 2024
@rvveber
Copy link
Contributor

rvveber commented Oct 17, 2024

The NEXT_LOCALE cookie seems to be ignored currently and only relevant for url based routing.
It is not necessary and i have decided to name it impress_language instead.

@virgile-dev virgile-dev added the bug Something isn't working label Oct 17, 2024
rvveber added a commit that referenced this issue Oct 17, 2024
… shared cookies

frontend: switch to cookie-based language selection; backend: use newly created cookie to determine user preferred backend language; fix: error messages that are received from the backend are correctly translated

Closes #333
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend bug Something isn't working frontend good first issue Good for newcomers i18n python Pull requests that update Python code refacto
Projects
Status: To do
Development

No branches or pull requests

2 participants