Skip to content

[TwigBridge] Add current locale to AppVariable service #49870

@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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy