PoEdit and PHP annotations
Asked Answered
N

2

6

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?

Nielsen answered 19/7, 2012 at 14:48 Comment(4)
I dislike the use of comments to actually do stuff on the document. Comments should be reserved for human-readable text to understand the code, and perhaps to assist IDEs in selecting variable types. It should not server any function in the application.Entozoon
This is the only way of doing annotations in PHP and it's actually endorsed by the reflection subystem in the language. See method getDocComment() on various Reflection classes. Plus, they really are readable for humans - alternative to the example above would be something like PageView::getInstance()->setTitle("Home") in the action code.Sandwich
I agree with @Truth there, using annotations to do stuff is like saving variables as filenames of blank files; you can do it, but it's just awkward. And besides, accessing the file as text just to read the value seems a waste when one could, you know, set an ordinary variable, eg a public class property, with whatever value is needed.Ledford
@Ledford it may seem awkward to you because you probably never used annotations. In more robust languages annotations are nothing unusual and they help to hide large chunks of logic that is loosely related to the context within which they operate. To learn more look up term Aspect Oriented Programming. There is more annotations in my code and I dare to say that they reduce controllers code roughly by half. Also, I don't do text parsing - PHP does it automatically at run time, so I don't have to. I only access to what has been already parsed by the engine.Sandwich
B
3

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.'>';
Bo answered 24/7, 2012 at 6:59 Comment(0)
P
0

If you really want to do this that way then you can just extract the strings you need from php files to an external file, while replacing the annotation part with _(string); for each match and run Poedit on that file.

You can match it with .*\*\s\@(\w+)\s\"(\w+)\".* - $1 in the match will be annotation (Title), $2 will be the value: (Home)

Pompous answered 21/7, 2012 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.