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

feat: cms.views - set request language according to request.LANGUAGE_CODE #7610

Open
wants to merge 30 commits into
base: develop-4
Choose a base branch
from

Conversation

vasekch
Copy link

@vasekch vasekch commented Jul 13, 2023

Description

I'm facing difficulties when using custom middleware for switching languages based on domain name. Inspired by django.middleware.locale.LocaleMiddleware I use request.LANGUAGE_CODE to set language (along with translations etc.)

When a Page is rendered, the language is wrong, first value from settings.LANGUAGES is used. Current code only works with language prefix patterns or it serves default language (being the first one from LANGUAGES, not LANGUAGE_CODE as one would expect).

So I propose to include in main cms.views extra code for language recognition. This is designed to work with built-in LocaleMiddleware and any custom middleware.

If LocaleMiddleware is used, our code for handling language prefix pattern is arbitrary as it does the very same thing.

Checklist

  • I have opened this pull request against develop-4
  • I have added or modified the tests when changing logic
  • I have followed the conventional commits guidelines to add meaningful information into the changelog
  • I have read the contribution guidelines and I have joined #workgroup-pr-review on Slack to find a “pr review buddy” who is going to review my pull request.

@vasekch
Copy link
Author

vasekch commented Jul 24, 2023

I've removed the code that is already included in the LocalMiddleware (detecting language from url prefix).

LocaleMiddleware is part of official instruction for using i18n in Django and we have it by default in the django-cms-quickstart.

@vasekch
Copy link
Author

vasekch commented Jul 24, 2023

Please can someone help we on how to write tests for this.

I think this is vastly covered by current tests:

New approach in this PR adds possibility to handle languages using custom middleware through Django standard way - using request.LANGUAGE_CODE. It seems bit out of scope to implement custom middleware in the test to proof this.

Any guidance on how this should be tested is appreciated.

@fsbraun
Copy link
Sponsor Member

fsbraun commented Jul 25, 2023

@vasekch I agree that the conventional behaviour is already tested. A way of testing your generalization would be a one liner middleware that always sets the language to, say, Swedish. The test could check what languages a page would be served in:

  • /
  • /en/ or /it/

@fsbraun
Copy link
Sponsor Member

fsbraun commented Jul 25, 2023

I've been looking into why this, so far, has been hard-coded into the CMS but came to your conclusion: It currently is not the "right" way of processing languages. I am not aware of any side effects as of now. If we get this tested, I'd be happy to back the PR 👍 .

@vasekch
Copy link
Author

vasekch commented Jul 26, 2023

I'll work on this, not immediately, but the time will come.

@fsbraun
Copy link
Sponsor Member

fsbraun commented Aug 22, 2023

@vasekch I'd love to put this in the next release candidate. Do you think you'll come around to doing the tests?

fsbraun and others added 4 commits August 23, 2023 16:12
* [4.1.0rc4 release process] Building locales

* [4.1.0rc4 release process] Bumped version to 4.1.0rc4

* [4.1.0rc4 release process] compilemessages

* [4.1.0rc4 release process] compiling new static files

* [4.1.0rc4 release process] updating latest docs

* Update CHANGELOG.rst

* Update 4.1.0.rst

---------

Co-authored-by: Github Release Action <[email protected]>
* Fix css glitch

* Update translations source fill which was missing strings

* Fix: en locale
fsbraun and others added 4 commits August 26, 2023 00:10
* Fix css glitch

* Update translations source fill which was missing strings

* Fix: en locale

* Fix typo

* Fix: typo: occured -> occurred

* undo mo change
@fsbraun fsbraun self-assigned this Sep 19, 2023
Include language in page cache key, the same as is in v3.11
@fsbraun
Copy link
Sponsor Member

fsbraun commented Oct 19, 2023

@vasekch can you quickly comment on the last commit?

@fsbraun fsbraun changed the title cms.views - set request language according to request.LANGUAGE_CODE feat: cms.views - set request language according to request.LANGUAGE_CODE Nov 10, 2023
@fsbraun
Copy link
Sponsor Member

fsbraun commented Nov 10, 2023

Hi @vasekch ! We'll probably be doing another RC. I'd be happy if we could include this PR. Any hopes to get it over the line soon?

Added option to extend page_cache_key by referencing a function in settings.CMS_PAGE_CACHE_KEY_EXTRA as string formatted for import_module.

This is needed when language is moved away from url path, it needs to be provided separately, otherwise cache is mixed.

This is also useful if request contain any other cache differentiators, for example currency in a cookie.
@vasekch
Copy link
Author

vasekch commented Nov 14, 2023

@fsbraun sorry, no ETA. I'm in the middle of production release preparation agony. I need to make some compromises, like the last commit here. I'll later gladly explain intentions and split this into more sensible PRs, currently it's quite ugly.

Added option to extend page_cache_key by referencing a function in settings.CMS_PAGE_CACHE_KEY_EXTRA as string formatted for import_module.

This is needed when language is moved away from url path, it needs to be provided separately, otherwise cache is mixed.

This is also useful if request contain any other cache differentiators, for example currency in a cookie.

vasekch and others added 3 commits December 5, 2023 16:43
Added option to extend get_placeholder_cache_key by referencing a function in settings.CMS_PLACEHOLDER_CACHE_KEY_EXTRA as string formatted for import_module.

This is needed when some session setting change page content (e.g. currency), otherwise cache is mixed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants