Search code examples
phpzend-framework2zfdoctrine

ZF2: how to translate Form annotations?


Is it a way to translate annotations in docscommment ?

Here is an example of my actual code:

  /**
     *
     * The Item Name
     * @var string
     * @Annotation\Options({"label":"Name"})
     * @Annotation\Attributes({"type":"text"})
     * @ORM\Column(type="string", length=128, nullable=false, unique=true)
     */
    private $name;

To translate the generated form label in my template file (I use twig):

 <label for="{{field.getName()}}">
    {{translate(field.getLabel())}}:
 </label>

It works well, except a parser like POEdit cannot fint the term "Name" with this way. For now I use a private method into my model to inform the parser that terms exists... But it's dirty..

/**
 * Unused in the software, used by external parser.
 */
private final function parseTranslations()
{
    $this->translate('Name');
    return null;
}

Is there a better solution to fix this and translate terms in docscomments ?


Solution

  • There is no way to enforce poedit to recognise text to translate from annotation. Poedit uses xgettext to parse files and this one simply ignore comment lines.

    Consider writing your own parser could fix this behavior. Creating .po files is not that difficult as it pretendt to be. Next you have to compile it to .mo binary format with msgfmt tool.