Skip to content

feat: Allow to render Twig templates with a specific locale#265

Open
Jean-Beru wants to merge 2 commits into
sensiolabs:1.xfrom
Jean-Beru:feat/locale-aware-rendering
Open

feat: Allow to render Twig templates with a specific locale#265
Jean-Beru wants to merge 2 commits into
sensiolabs:1.xfrom
Jean-Beru:feat/locale-aware-rendering

Conversation

@Jean-Beru

@Jean-Beru Jean-Beru commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add a locale(string) method on builders using Twig templates (HtmlPdfBuilder, MarkdownPdfBuilder, UrlPdfBuilder and their Screenshot counterparts) to render content, header and footer templates with the given locale via Symfony's LocaleSwitcher.
  • Behavior is unchanged when locale() is not called: symfony/translation remains an optional dependency. A clear LogicException is thrown if locale() is called without it installed.
  • Exposed as a YAML configuration node via WithConfigurationNode so it can be set globally per builder.

Credits

Original idea by @StevenRenaux.

Add a `locale(string)` method on builders using Twig (HtmlPdfBuilder,
MarkdownPdfBuilder and their Screenshot counterparts) to render content,
header and footer templates with the given locale via Symfony's
LocaleSwitcher. The current application locale is restored after each
rendered template.

The `symfony/translation` dependency is optional: when no locale is
set, behavior is unchanged. When `locale()` is called without
`symfony/translation` installed, a clear LogicException is thrown.

The locale is also exposed as a YAML configuration node via
WithConfigurationNode so it can be set globally per builder.

Original idea by @StevenRenaux.

Co-authored-by: Steven Renaux <59167761+StevenRenaux@users.noreply.github.com>
Assisted-by: Claude:claude-opus-4-7
@Jean-Beru Jean-Beru force-pushed the feat/locale-aware-rendering branch from 801afaf to edd2f4b Compare April 30, 2026 13:01
protected function withRenderedPart(Part $part, string $template, array $context = []): static
{
if (null !== $this->locale && $this->getLocaleSwitcher()->getLocale() !== $this->locale) {
return $this->getLocaleSwitcher()->runWithLocale(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about using locale aware instead ?

@Neirda24 Neirda24 modified the milestones: v1.2, v1.3, v1.4 May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants