Skip to content

[TwigBridge] Add current locale to AppVariable service #49870

Closed
@aprat84

Description

@aprat84

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>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions