简体   繁体   中英

Using a terminology in Symfony's translation component - especially with validation messages

I know and understood how to translate validation messages in Symfony 2.

Just to repeat it from the official documentation:

// src/AppBundle/Entity/Author.php
use Symfony\Component\Validator\Constraints as Assert;

class Author
{
     /**
      * @Assert\NotBlank(message = "author.name.not_blank")
      */
      public $name;
}

And I would set up a translation file/key for it like:

 <!-- validators.en.xlf -->
 <?xml version="1.0"?>
 <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
     <file source-language="en" datatype="plaintext" original="file.ext">
         <body>
             <trans-unit id="1">
                 <source>author.name.not_blank</source>
                 <target>Please enter an author name.</target>
             </trans-unit>
         </body>
     </file>
</xliff>

This works perfectly fine, but in my case I'd heavily need some terminology to use, so eg in the example case above have a separate translation for 'author', because if my 'author' occurs in many validation messages, I'd need to change every occurrence of 'author' as soon as it's not 'author' anymore but eg 'creator'.

Is there any change to have something like:

 <target>Please enter an %author% name.</target>

With an additional translation like:

 <!-- validators.en.xlf -->
 <?xml version="1.0"?>
 <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
     <file source-language="en" datatype="plaintext" original="file.ext">
         <body>
             <trans-unit id="2">
                 <source>author</source>
                 <target>creator</target>
             </trans-unit>
         </body>
     </file>
</xliff>

this is a bad idea IMO, because it makes it much harder to write translation messages when using placeholders which are themselves translations. And I can give you a simple example where it breaks: just translate your messages above in French.

  • Merci d'entrer un nom d'auteur
  • Merci d'entrer un nom de créateur

As you see, renaming author to creator does not only involve changing this word, but also other words in the message (thinking about the English version only is what leads to such mistake, as English is much simpler in this regard). So if you follow your suggestion, you will be unable to translate your app in most languages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM