Skip to content

Translate annotations

BowlOfSoup edited this page Apr 10, 2026 · 2 revisions

Note: Legacy docblock annotations are still supported. But since version 6 you should use PHP attributes.

Specify your domain (the .xliff file)

By default, the translation must be in a Resources/translations/messages.en.xliff. You can specify a custom domain (xliff file)

  /**
   * @Bos\Normalize(group={"somegroup"}, name="automobile")                                                                                                                                                                                                                                                    
   * @Bos\Translate(group={"translation"}, domain="some_domain")                                                                                                                                                                                                                                               
   */
  #[Bos\Normalize(group: ['somegroup'], name: 'automobile')]                                                                                                                                                                                                                                                   
  #[Bos\Translate(group: ['translation'], domain: 'some_domain')]
  private $propertyToBeNormalized;                                                                                                                                                                                                                                                                             

This tries to find the translation in Resources/translations/some_domain.en.xliff.

Specify your locale (language)

You can specify your locale, if you did not set that globally in Symfony.

  /**                                                                                                                                                                                                                                                                                                          
   * @Bos\Normalize(group={"somegroup"}, name="automobile")
   * @Bos\Translate(group={"translation"}, domain="some_domain", locale="nl")
   */                                                                                                                                                                                                                                                                                                          
  #[Bos\Normalize(group: ['somegroup'], name: 'automobile')]
  #[Bos\Translate(group: ['translation'], domain: 'some_domain', locale: 'nl')]                                                                                                                                                                                                                                
  private $propertyToBeNormalized;                      

This will search for the translation in Resources/translations/some_domain.nl.xliff, notice the nl in the file name (Dutch language).

Clone this wiki locally