New in Symfony 6.4: Locale Improvements


Render Emails in User Locale

Contributed by Alexander Schranz
in #51690.

When creating emails with the TemplatedEmail class, you can configure things
like the the subject, the templates used to generate text and/or HTML contents,
the variables passed to those templates, etc.
In Symfony 6.4 we're adding a new locale() method so you can also configure
the locale that the email contents will use. This is a common need when creating
emails in the different preferred languages of your customers:

use Symfony\Bridge\Twig\Mime\TemplatedEmail;

$email = (new TemplatedEmail())
// ...
->locale($customer->getLocale())
;

Passing the Current Locale to runWithLocale()

Contributed by Alexander Schranz
in #51684.

The locale switcher was introduced in Symfony 6.1 as a way to change at once
the locale used in all services (tagged with kernel.locale_aware) in your
application. It also includes a runWithLocale() utility to run some code with
the given locale without changing the locale of the application.
In Symfony 6.4 we're making a small improvement to it to pass the locale to the
callback function that runs the code:

$notificationContent = $this->localeSwitcher->runWithLocale(
$contact->getLocale(),
// the $locale argument is now passed by Symfony automatically
function (string $locale) use ($someEntity): string {
$title = $someEntity->getTitle($locale);

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

Getting the Enabled Locales in Templates

Contributed by Jonathan Scheiber
in #50321.

In Symfony 5.1 we introduced the enabled_locales configuration option so
you can configure which locale(s) are available in your application. This improves
performance slightly because Symfony doesn't generate translation files for locales
not supported in your application.
In Symfony 6.4 we're making this value available in all Twig templates via the
app global variable. This can help you for example when building the dropdown
that allows visitors to switch the application language:

<ul class="locales">
{% for locale in app.enabledLocales %}
{% set is_active = app.request.locale == locale.code %}
<li class="{{ is_active ? 'active' }} translate="no">
<a lang="{{ locale.code }}" hreflang="{{ locale.code }}"
href="{{ path(app.current_route, app.current_route_parameters|merge({_locale: locale.code})) }}">
{{ locale.name|capitalize }}
a>
li>
{% endfor %}
ul>

Sponsor the Symfony project.