Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PoEdit and PHP annotations

I'm looking for a way to make PoEdit understand PHP annotations. Here's a sample of code I want PoEdit to pick up and put into catalog:

class MyController extends Controller {

    /**
     * @Title "Home"
     */
    public function index() {
        ...
    }

}

The interesting part is @Title annotation. It is accessed in front controller and assigned to master view, effectively ending up inside <title>...</title> tag.

Now I need that string translated, but PoEdit seems to only understand _() expressions, and adding @Title to keywords does not work. This is probably because annotations in PHP are in comment block.

Is there any way to force PoEdit to understand annotations?

like image 647
Michał Niedźwiedzki Avatar asked Jul 19 '12 14:07

Michał Niedźwiedzki


People also ask

What are annotations in PHP?

Introduction to PHP Annotations. PHP annotations are basically metadata which can be included in the source code and also in between classes, functions, properties and methods. They are to be started with the prefix @ wherever they are declared and they indicate something specific.

What is Poedit and how does it work?

Poedit was built to handle translation using gettext (PO), which is used by many PHP projects (Drupal, WordPress), Python projects (Django), or virtually anything running on Linux.

What is the correct syntax for @PHP-Doc annotations?

PHP-DOC annotations do not have a fixed syntax - that is, everything after @name is interpreted differently for each type of annotation. For example, the syntax for @var is @var {type} {description}, while the syntax for @param is @param {type} {$name} {description}.

Where can I find Poedit source code?

Open Source version and source code Up-to-date source code of the Open Source version of Poedit (sans the Pro features included in the above binaries) is available at GitHub under the terms of the MIT license. Windows and Mac versions can only be built from a git checkout; Unix builds can be done either from the checkout or from the above tarball.


1 Answers

Short answer, you can't.

POEdit uses xgettext to scan your files therefore using specific syntax, ignoring commented lines.

For example, if your keywords are _ the following examples will be parsed like:

_('test'); -> string 'test'

_("test"); -> string 'test'

_('test' -> string 'test'

_ 'test -> no catch

_('test -> no catch

_(test) -> no catch

_($test) -> no catch

//_('test'); -> no catch

/*_('test');*/ -> no catch

You can execute xgettext using other parameters but I'm not sure if you will be able to achieve your goal.


One easy fix (not standard ofc) is to add other keyword like placeholder and make a php function like

function placeholder($string){}

and use it so POEdit can parse it

class MyController extends Controller {

    /**
     * @Title "Home"
     */
    public function index() {
      placeholder('Home');  
      ...
    }

}

At your frontend parser just use the simple _($value) and you will have your title translated.

Dunno how is your code but will assume its something similar to this.

Assuming that $tag = 'title' and $value = 'Home'

echo '<'.$tag.'>'._($value).'</'.$tag.'>';
like image 96
Ciro Avatar answered Nov 08 '22 09:11

Ciro