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

[TwigBridge] Add current locale to AppVariable service #49870

Closed
aprat84 opened this issue Mar 30, 2023 · 0 comments · Fixed by #49913
Closed

[TwigBridge] Add current locale to AppVariable service #49870

aprat84 opened this issue Mar 30, 2023 · 0 comments · Fixed by #49913

Comments

@aprat84
Copy link
Contributor

aprat84 commented Mar 30, 2023

Description

If we want to render a template using a locale different from the one in the request, then there's no way to access this locale from within the template. Only solution is to inject it as a context variable:

$this->localeSwitcher->runWithLocale($anotherLocale, function () {
    $email = (new TemplatedEmail())
        ->to('...')
        ->subject('Some subject')
        ->htmlTemplate('email/notification.html.twig')
        ->context([
            'locale' => $anotherLocale,
            'var1'    => '...',
            'var2'    => '...',
        ]);

    $this->mailer->send($email);
});

It would be nice to be able to access the current locale with something like this:

<p>Locale: {{ app.locale }}!</p>

I know there's the app.request.locale approach, but this fails when using the LocaleSwitcher::runWithLocale() with a different locale that the request one.

Example

The AppVariable service could have a getLocale() function which retrieves the current locale being used from the LocaleSwitcher::getLocale() or the \Locale::getDefault() functions.

<p>Locale: {{ app.locale }}!</p>
@aprat84 aprat84 changed the title [Twig] Add current locale to AppVariable service [TwigBridge] Add current locale to AppVariable service Mar 30, 2023
@fabpot fabpot closed this as completed Apr 11, 2023
fabpot added a commit that referenced this issue Apr 11, 2023
…iable` (SVillette)

This PR was squashed before being merged into the 6.3 branch.

Discussion
----------

[TwigBridge][TwigBundle] Add current locale to `AppVariable`

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #49870
| License       | MIT
| Doc PR        | symfony/symfony-docs#18190

As stated in #49870, they were no way to get the current locale without passing it through a variable when rendering a template within `LocaleSwitcher::runWithLocale()`.

```php
#[AsController]
final class HomeController
{
    #[Route('/', name: 'app_home')]
    public function __invoke(LocaleSwitcher $localeSwitcher, Environment $twig): Response
    {
        $localeSwitcher->setLocale('en');

        return $localeSwitcher->runWithLocale('fr', function () use ($twig) {
            return new Response($twig->render('index.html.twig'));
        });
    }
}
```

```twig
{{ app.locale }} // fr
```

A doc PR will be submitted if this change is accepted.

Commits
-------

2371216 [TwigBridge][TwigBundle] Add current locale to `AppVariable`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants