views:

29

answers:

1

Hi all,

I have a load of php templates which uses a custom translate function "__", i.e.

<?php echo __("Hello"); ?>

I need to write a script which will look for all these function calls (there are about 200 templates).

I.e. it will find __("Hello") and add it to a sentences to be translated array. For example it will find:

$sentences[] = "Hello";
$sentences[] = "Goodbye";
$sentences[] = "Random sentence to be translated";

Basically i need to find the strings which need to be translated.

Which do you think is the best language for doing the script in? and do you think it will be best to use a regular expression?

Any help to point me in the right direction would be superb!

Thanks

+1  A: 

I always jump to Perl for string manipulation problems.

However, awk or sed could easily solve your problem.

For example, in Perl:

 while(<>) {
   if( $_ =~ /echo __\((".*?")\)/ ) {
     print '$sentences[] = ' + $1;
   }
 }

Note, this will only capture one string per line. You can do more, but I'll leave that as an exercise to the reader.

Also, the 'while(<>)' will loop through each line in each file you pass on the command line. There's also a way read all of the files in a directory if that's what you need.

CWF