Render Emails in User Locale
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:
1 2 3 4 5 6
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
$email = (new TemplatedEmail())
// ...
->locale($customer->getLocale())
;
Passing the Current Locale to runWithLocale()
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:
1 2 3 4 5 6 7 8 9
$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
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:
1 2 3 4 5 6 7 8 9 10 11
<ul class="locales">
{% for locale in app.enabledLocales %}
{% set is_active = app.locale == locale %}
<li class="{{ is_active ? 'active' }} translate="no">
<a lang="{{ locale }}" hreflang="{{ locale }}"
href="{{ path(app.current_route, app.current_route_parameters|merge({_locale: locale})) }}">
{{ locale|locale_name(locale) }}
</a>
</li>
{% endfor %}
</ul>
{% for locale in app.enabledLocales %} should be {% for locale in app.enabled_locales %}
enabledLocales is a private array.
Curiously, the getter is declared as