开发者

Regex / awk / etc for scanning my source code for translations

I'd like to scan my source for certain lines such as:

$obj->setLabel('output this text')->someOtherMethod(etc);

or:

$this->view->title = "I want this text";

Obviously the code is PHP. I'm using Zend Framework. That doesn't really matter.

I'm running linux and understand pipes. I'm guessing I could pipe:

grep --include=*.php -R 'setLabel(' .

into awk or whatever. I just want the each of the "one or more" characters output on it's own line surrounded by quotes, terminated by a comma. CSV translation files are not far off then.

I only expect to search for one pattern at a time. So first I'd get all the "Labels" etc.

note: I know of POedit, etc. I'm usin开发者_如何学Gog CSV files for static UI translations. I'm not going to change that. They need to be editable by a 3rd party who just wants to use "Excel" (shudder...)

This is what I ended up using:

grep -oh --include=*.php -R -E "setLabel\('[^']*'\)" . > labels.txt

And then removing the unwanted "setLabel(" and ")" in a text editor. However, I'm very keen for a cleaner one-liner. Oh... There is also code-golf. I should ask those guys...


How about using find and sed:

find . -type f -name '*.php' -exec sed -ne "s/.*setLabel('\([^']\+\)').*/\1/p" {} \;

and

find . -type f -name '*.php' -exec sed -ne "s/.*view->title = \"\([^\"]\+\)\".*/\1/p" {} \;


Ruby(1.9+)

say you want to search for setLabel

$ ruby -ne 'puts $_.scan(/.*setLabel\(\047(.[^)]*)\047/)' file
output this text

say you want to search for view-title

$ ruby -ne 'puts $_.scan(/.*view->title\s+=\s+\042(.[^"]*)\042/)' file
I want this text
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜