Search code examples
regexunixgreptranslate

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 using 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...


Solution

  • 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" {} \;