Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search for function call in php in order to generate a translation file

I am developing a php website that needs to be multilingual. For this reason, I implemented a translation function which has the following header:

function t($string, $replace_pairs = array(), $language = NULL)

Basically, this function is called like this in multiples files of my project:

echo '<p>' . t('Hello world!') . '</p>';
$hello_String = t("Hello @name!", array('@name'=>$username));

I haven't generated the translation strings yet and I would like to generate multiple translation file automatically (one for each language).

What I am looking for is a bash program (or a single command, using grep for example) that would look for every call to this t() function and generate a php file with the following structure:

<?php
/* Translation file "fr.php" */
$strings['fr']['Hello world!'] = '';
$strings['fr']['Hello @name!'] = '';

Has anyone ever encountered this situation and could help me with this ?

Thank you very much.

Kind regards,

Matthieu

like image 409
Mat Avatar asked Dec 06 '22 12:12

Mat


1 Answers

Yes, you're not exactly the first to come across this. :)

You can use the venerable gettext system for this, you don't need to invent your own functions. Then you'd get to use xgettext, which is a command line utility to extract strings using the _() function.

If you want to roll your own system for whatever reason, your best bet is to write a PHP script which uses token_get_all to tokenize the source, then go through the tokens and look for T_FUNCTIONs with the value t.

like image 50
deceze Avatar answered Feb 22 '23 22:02

deceze