网页 综合 更新时间 2026-11-09 08:06:00

New in Symfony 8.2: Translation Improvements (Symfony Blog)

自动蜘蛛 symfony.com 正文约 4184 字
New in Symfony 8.2: Translation Improvements (Symfony Blog)
November 9, 2026 • Published by Javier Eguiluz
Symfony 8.2 is backed by:
Symfony 8.2 brings many improvements to the Translation component and its integration with Twig and forms, including better XLIFF files and a translation:extract command that finds many more messages.
In XLIFF files, the <source> element is meant to contain the original text to translate, which is what translation tools and translators display. However, the XLIFF files generated by translation:extract store the translation key in it:
First, Symfony 8.2 now keeps the <source> element when loading and dumping XLIFF files, instead of replacing it with the translation key. Second, a new command fills the <source> elements of your XLIFF files with the messages of the default locale:
If the English translation of navbar.home is Home (and en is the default locale), the <source> element of the previous example now contains Home. The command only updates files stored in your translations/ directory and in the paths configured in framework.translator.paths; it never changes the translation files of your vendors or bundles.
The trans_default_domain Twig tag applies to the current template scope. That scope doesn't include the body of {% embed %} tags, because Twig compiles them as separate templates. In the following example, product.title is translated in the catalog domain, but product.add_to_cart uses messages:
In Symfony 8.2, add the new for _self modifier to apply the domain to the whole template file, including embed bodies and macros:
The domain must be a literal string and you can only declare it once per template, outside of any block and before any embed tag. It doesn't apply to other templates (e.g. the ones included or extended), and a regular trans_default_domain tag still takes precedence where it applies.
The group_by option of ChoiceType creates <optgroup> elements to group choices. Their labels were already translated, but the callable could only return strings. In Symfony 8.2, it can also return TranslatableInterface objects, like the choice_label option already allows:
If your custom form theme overrides the choice_widget_options block, render the group label from choice.label (falling back to the loop key) because the array key is no longer the label when using translatable objects.
The translation:extract command only found messages passed to trans(), t() or new TranslatableMessage() as literal strings, concatenations of literals or class constants. In Symfony 8.2, the extractor also resolves ternary and null-coalescing expressions, match expressions, interpolated strings, variables assigned earlier and values returned by methods:
Methods are resolved when the extractor knows the class of the object ($this, typed parameters and properties, enum cases, return types, etc.) and when they are defined in the extracted paths:
When rendering forms in Twig, you can override the label and help messages defined in the form type. In Symfony 8.2, translation:extract finds the messages that you pass to the form_label(), form_widget(), form_help() and form_row() functions:
These messages are extracted in the domain defined by the translation_domain variable or in the default domain otherwise. The trans_default_domain tag is ignored on purpose, because form themes don't use it to translate labels and help messages.
❤️ Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
💼 DevOps for a Symfony project at Oliver Bernard View Symfony jobs →
£70,000 – £80,000 / year Full remote
Code consumes server resources. Blackfire tells you how
Peruse our complete Symfony & PHP solutions catalog for your web development needs.
No stress: we've got you covered with our 141 automated quality checks of your code
Thanks Cedric BERTOLINI (@alsciende) for being a Symfony contributor
4 commits • 29 lines changed
View all contributors that help us make Symfony
Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.
Symfony™ is a trademark of Symfony SAS. All rights reserved.
访问原文 返回搜索 https://symfony.com/blog/new-in-symfony-8-2-translation-improvements
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。