Manuel PHP-GTK

Translation of PHP-Gtk applications

What it's all about

You want to distribute your PHP-Gtk application on your webpage and make it reach a large number of people. The only problem between your application and all the people is not the functionality; your app is perfect in this way: It is the language.

Even if some people don't or don't want to realize: There are lots of people out there which would use your program but don't speak your language or the language in which your program is. It would be nonsense to ignore them. The solution is easy: Translate the program. Just find a person who speaks the target language and who is willing to translate all the things your program contains.

But the next step: How do you - technically - translate your program?. There are many ideas how to do this:

  • Take the program sources (and glade files), make a copy and translate them. This is the worst method: If you want to extend your program you will have to change every single file of the 20 translated versions. Even worse: The translator does not have to be a programmer, and requiring him to change the sources can cause serious problems with the code: Does the translator know how to deal with escape sequences? What if the translator accidently removes a end-quote? The program just won't run, and you will have a nice time searching bugs in all the translated versions.
  • Using a big array which contains all the texts. Put it in a translation file for each language, and include the right one when the program starts. This has the advantage that translation and code are segregated strictly, and the translator will have an easy job. Just what if you are using glade files for your GUI definitions? You would have to make a copy of each glade file and translate it; causing the same problems as the first option. Or you load every single widget and change the text of it. This would require a function in your application which knew every single widget and the string in the big translation array it should apply.
  • Using the text as normal in the sources, but having it and the glade files translated automatically. Impossible? No, the gettext extension of php makes this scenario real.

This tutorial shows how to translate your apps the most easy way using the gettext extension of php. It covers normal string translation as well as the translation of glade files and shows a solution if the gettext extension is not available on the user's system.

Exemple 3.1. The first and worst translation method

  1. <?php
  2. //file: cart.en.php
  3. //...
  4. echo '<title>Shopping cart</title>';  
  5. //...
  6. echo 'Copying file ' . $strFile1 . ' to ' . $strFile2;  
  7. //...
  8.  
  9. //file: cart.de.php
  10. //...
  11. echo '<title>Einkaufswagen</title>';  
  12. //...
  13. echo 'Kopiere Datei ' . $strFile1 . ' nach ' . $strFile2;  
  14. //.
  15. ?> 

Exemple 3.2. The second method

  1. <?php
  2. //file: translationl.en.inc
  3. $arTranslation = array(  
  4. 'title' => 'Shopping cart',  
  5. 'copy_1' => 'Copying file ',  
  6. 'copy_2' => ' to '  
  7. );  
  8.  
  9. //file: translationl.de.inc
  10. $arTranslation = array(  
  11. 'title' => 'Einkaufswagen',  
  12. 'copy_1' => 'Kopiere Datei ',  
  13. 'copy_2' => ' nach '  
  14. );  
  15.  
  16. //file: cart.php
  17. include_once( 'translation.en.inc');  
  18. //...
  19. echo '<title>' . $arTranslation['title'] . '</title>';  
  20. //...
  21. echo $arTranslation['copy_1'] . $strFile1 . $arTranslation['copy_2'] . $strFile2;  
  22. //.
  23. ?> 

Exemple 3.3. Using gettext

  1. <?php
  2. //file: cart.php
  3. //...
  4. echo '<title>' . _('Shopping cart') . '</title>';  
  5. //...
  6. echo sprintf( _('Copying file %s to %s'), $strFile1, $strFile2);  
  7. //.
  8. ?> 

Remonter Remonter
L'éditeur javascript - CSS - Gentoo - Tutoriaux PHP - Tutoriels PHP - Bretagne - php - Moto - Kit graphique