PoEdit and PHP Annotations

I am looking for a way for PoEdit to understand PHP annotations. Here's a sample code that I want PoEdit to pick up and put into a directory:

class MyController extends Controller { /** * @Title "Home" */ public function index() { ... } } 

The interesting part is the @Title annotation. It is accessed in the front controller and assigned to the master view, which actually ends inside the <title>...</title> .

Now I need this line, but PoEdit seems to only understand _() expressions, and adding @Title to keywords doesn't work. This is probably due to the fact that annotations in PHP are in the comment block.

Is there a way to get PoEdit to understand annotations?

+6
source share
2 answers

The short answer is, you cannot.

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

For example, if your keywords are _ , the following examples will be analyzed as follows:

_('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 with other parameters, but I'm not sure if you can achieve your goal.


One simple fix (not standard) is to add another keyword, such as placeholder , and make a php function like

 function placeholder($string){} 

and use it so that POEdit can analyze it

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

In your frontend syntax just use simple _($value) and you translate your header.

I donโ€™t know how your code is, but suppose something similar to this.

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

 echo '<'.$tag.'>'._($value).'</'.$tag.'>'; 
+3
source

If you really want to do this, you can simply extract the lines you need from php files into an external file, replacing the part of the annotation with _ (string); for each match and run Poedit in this file.

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

0
source

Source: https://habr.com/ru/post/920896/


All Articles