PoEdit and PHP annotations

試著忘記壹切 提交于 2019-12-23 09:05:26

问题


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?


回答1:


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.'>';



回答2:


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)



来源:https://stackoverflow.com/questions/11563403/poedit-and-php-annotations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!