Search function call in php to create a translation file

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

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

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

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

I have not generated translation strings yet, and I would like to automatically generate several translation files (one for each language).

I am looking for a bash program (or one command, for example, using grep) that will 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 come across this situation and could help me with this?

Thank you very much.

Respectfully,

Mathieu

+4
source share
6 answers

No need to reinvent the wheel

Drupal uses the same t () function to localize itself, and potx is your friend.

If you do not already have or want to install a drupal instance, you can look at the potx.inc file and reuse it in the script.

Below is the full API documentation for the translation template extractor.

+2
source

Yes, this is not your first time encountering this. :)

You can use the venerable gettext system for this, you do not need to invent your own functions. Then you can use xgettext , which is a command line utility, to retrieve strings using the _() function.

If you want to rotate your own system for any reason, it is best to write a PHP script that uses token_get_all to token_get_all source, then go through the markers and find T_FUNCTION with a value of t .

+11
source

Try the script http://pastie.org/4568713

Using:

 PHP .php ./proj-directory lang1 lang2 lang3 

This creates the files lang1.php , lang2.php , lang3.php in the ./lang directory

+2
source

You need two functions:

1- directory scanning for php files. for example this

2- correspondence of your function t, grep string and language file generation. as

  function genLang($file) { $content = file_get_contents($file); preg_match(...); foreach(...){ echo(...); } } 
0
source

The Yii framework also uses the same functionality, see MessageCommand class https://github.com/yiisoft/yii/blob/master/framework/cli/commands/MessageCommand.php#L125

0
source

You need a simple (very simple) "template system", but there are two template templates in your problem.

  • Convert " Hello $X! " To " Hello Jonh! " Or " Hello Maria! " By specifying $ X. (PHP does this for you in line declarations).
  • Choose the appropriate template: “ Hello $X! ” For English, “ ¡Hola $X! ” For Spanish.

Element 1 is simpler, but the order of the algorithm is 2.1 (clause 2 of clause 1). For this simple task, you do not need a regular expression (to reuse the "string with place owner" PHP).

Illustrating

For element 1, the easiest way is to declare a specialized function to say "Hello",

 // for any PHP version. function template1($name) { return "<p>Hello $name!</p>";} print template1("Maria"); 

For point 2, you need a generalization of what PHP does for you, by closing,

 header('Content-Type: text/html; charset=utf-8'); // only for remember UTF8. // for PHP 5.3+. Use function generalTemplate1($K) { // $K was a literal constant, now is a customized content. return function($name) use ($K) {return "<p>$K $name!</p>"; }; } // Configuring template1 (T1) for each language: $T1_en = generalTemplate1('Hello'); // english template $T1_es = generalTemplate1('¡Hola'); // spanish template // using the T1 multilingual print $T1_en('Jonh'); // Hello Jonh! print $T1_es('Maria'); // ¡Hola Maria! 

For additional templates, use generalTemplate2 (), generalTemplate3 (), etc .; $T2_en , $T2_es , $T2_fr , $T3_en , $T3_es , etc.

Decision

Now, for rational use, you like to use arrays ... Well, there is a problem with the file structure, and more than 1 level of generalization. Cost is a variable name parser for place owners. I used a simple regular expression with preg_replace_callback ().

 function expandMultilangTemplate($T,$K,$lang,$X) { // string $T is a template, a HTML structure with $K and $X placeholders. // array $K is a specific language constants for the template. // array $lang is the language, a standard 2-letter code. "en", "fr", etc. // array $X is a set of name-value (compatible with $T placeholders). // Parsing steps: $T = str_replace('{#K}',$K[$lang],$T); // STEP-1: expand K into T with lang. // STEP-2: expand X into T global $_expMultTpl_X; // need to be global for old PHP versions $_expMultTpl_X = $X; $T = preg_replace_callback( '/@([az]+)/', create_function( '$m', 'global $_expMultTpl_X; return array_key_exists($m[1],$_expMultTpl_X)? $_expMultTpl_X[$m[1]]: ""; ' ), $T ); return $T; } // CONFIGURING YOUR TEMPLATE AND LANGUAGES: $T = "<p>{#K} @ name@surname !</p>"; $K = array('en'=>'Hello','es'=>'¡Hola'); // take care with things like "!", that is generic, and "¡" that is not. // USING! print expandMultilangTemplate( $T, $K, 'en', array('name'=>'Jonh', 'surname'=>' Smith') ); print expandMultilangTemplate($T, $K, 'es', array('name'=>'Maria')); 

I tested this script with PHP5, but it works with the old ones (PHP 4.0.7 +).

About "multilingual files": if your translations are in files, you can use somthing like

 $K = getTranslation('translationFile.txt'); function getTranslation($file,$sep='|') { $K = array(); foreach (file($file) as $line) { list($lang,$words) = explode($sep,$line); $K[$lang]=$words; } } 

and file as

 en|Hello es|¡Hola 

Simplest with PHP 5.3

If you are using PHP 5.3+, there’s a simple and elegant way to express this “simple multilingual template system”,

 function expandMultilangTemplate($T,$K,$lang,$X) { $T = str_replace('{#K}',$K[$lang],$T); $T = preg_replace_callback( '/@([az]+)/', function($m,$X=NULL) use ($X) { return array_key_exists($m[1],$X)? $X[$m[1]]: ''; }, $T ); return $T; } 
0
source

All Articles