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

Add frontend language cookie functionality #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

yvirtp
Copy link

@yvirtp yvirtp commented Sep 3, 2024

In our projects we set a frontend language cookie whenever the user changes the language via the language menu.
Like that the user will always get on the language he visited last and doesn't have to switch the language everytime he revisits your website.

We think that this functionality may also be useful for others hence why we create this merge request.

Best Regards
Yannis

Copy link
Member

@Benjamin-K Benjamin-K left a comment

Choose a reason for hiding this comment

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

Just some little improvements for better readability in the future.

{
$detectedLocale = $this->localeDetector->detectLocaleFromLocaleTag($feLanguageCookie);

if (!$detectedLocale instanceof Locale) {
Copy link
Member

Choose a reason for hiding this comment

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

Could you please move L150-168 to a new function and use it in both findMatchingPresetBy... methods?
Sth. like:

$detectedLocale = 'dependsOnMethod';

$preset = $this->findPresetByLocale($detectedLocale);

// For the presetByLanguageCookie one can directly return $preset.
// This check is only neccessary in the AcceptLanguageHeader while loop.
if ($preset !== null) {
    return $preset;
}

// ...

@@ -2,3 +2,5 @@ Wegmeister:
LanguageRedirect:
# Add mappings for language codes if you use some different codes than the default ones.
languageCodeOverrides: {}
# configure the name of your frontend language cookie name
feLanguageCookieName: _fe_language
Copy link
Member

Choose a reason for hiding this comment

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

Use null as default here, because one has to implement the cookie handling for this to work. This will prevent the backend from trying to read cookies that are missing.
If you want, you can add some more information on this in the readme, but I'm also fine with the current state.

@yvirtp
Copy link
Author

yvirtp commented Oct 7, 2024

Hey I updated the code like you requested.
Best Regards
Yannis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants