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

[Translation] Give current locale to LocaleSwitcher::runWithLocale()'s callback #51684

Merged

Conversation

alexander-schranz
Copy link
Contributor

Q A
Branch? 6.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #...
License MIT
Doc PR symfony/symfony-docs#...

I think it would not hurt to give the $locale everytime to the callback method.

Example usage:

$notificationContent = $this->localeSwitcher->runWithLocale($contact->getLocale(), function(string $locale) use ($someEntity): string {
    $title = $someEntity->getTitle($locale);

    return $this->twig->render('template.html.twig', ['title' => $title]);
});

Alternative solution currently is:

$locale = $contact->getLocale();
$notificationContent = $this->localeSwitcher->runWithLocale($locale, function() use ($locale, $someEntity): string {
    $title = $someEntity->getTitle($locale);

    return $this->twig->render('template.html.twig', ['title' => $title]);
});

So this avoids creating a $locale variable which maybe only is necessary inside the callback method.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

Please add a line in the changelog :)

@welcoMattic welcoMattic changed the title Give current locale to locale switcher runWithLocale callback [Translation] Give current locale to locale switcher runWithLocale callback Sep 20, 2023
@nicolas-grekas nicolas-grekas changed the title [Translation] Give current locale to locale switcher runWithLocale callback [Translation] Give current locale to LocaleSwitcher::runWithLocale()'s callback Sep 20, 2023
@nicolas-grekas
Copy link
Member

Thank you @alexander-schranz.

@nicolas-grekas nicolas-grekas merged commit c49e2d7 into symfony:6.4 Sep 20, 2023
3 of 7 checks passed
@alexander-schranz alexander-schranz deleted the feature/local-switcher-locale branch September 20, 2023 15:52
@nicolas-grekas nicolas-grekas added this to the 6.4 milestone Sep 25, 2023
This was referenced Oct 21, 2023
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.

4 participants