PHP and Gettext do not work on my server

I have a website. I am trying to get gettext to work so that my sites in English, Swedish and Norwegian can appear. I can not make it work. What did I do wrong?

This is my configuration code:

// define constants ( defualt - danish ) $lang = 'da_DA'; $lang_short = ''; $lang_prefix = 'da'; if ( isset( $_GET['lang'] ) ) { switch( $_GET['lang'] ) { case 'en': $lang = 'en_EN'; $lang_short = 'en/'; $lang_prefix = 'en'; break; case 'se': $lang = 'se_SE'; $lang_short = 'se/'; $lang_prefix = 'se'; break; case 'no': $lang = 'no_NO'; $lang_short = 'no/'; $lang_prefix = 'no'; break; } } define( 'LANG', $lang_short ); define( 'LANG_PREFIX', $lang_prefix ); putenv("LC_ALL=". $lang ); bindtextdomain('messages', ROOT .'lang/'); 

And my path is /var/www/rssbot.dk/lang/ . Should I do chmod to the right or ...?

+6
php apache gettext
source share
2 answers

I found that some gettext installations should have a locale-gen for each locale that you want to use. I found this to be true for Ubuntu in particular. You may need to restart PHP (apache) after running locale-gen .

 sudo locale-gen se_SE sudo locale-gen no_NO 

I have a test setup (with working language files) that can determine if the gettext function works.

 <?php //Depending on your OS, putenv/setlocale/both will set your language. putenv('LC_ALL=es_MX'); setlocale(LC_ALL, 'es_MX'); bindtextdomain( "su", "./locale" ); //set the locale folder for a textdomain bind_textdomain_codeset( "su", "UTF-8" ); //set the codeset for a textdomain textdomain( "su" ); //choose a textdomain if( gettext("Hello World!") === "Hola a todos!" ) { print "We translated it correctly"; } else { print "Gettext setup isn't working"; } ?> 
+3
source share

There are a few things that may go wrong.

1- To host most systems, you will need the following lines:

 <?php $newlocale = setlocale(LC_MESSAGES, "sv_SE"); putenv("LANG=$newlocale"); ?> 

2- On Linux, when using setlocale with LC_ALL instead of LC_MESSAGES, you need to set the locale on the server!

It can be installed with a command like this (for Ubuntu)

 aptitude install language-pack-sv 

Or just reconfigured with a command like this

 sudo locale-gen sv_SE 

3- Specify the name of the .mo files and locale directory

 <?php // Use default.mo located at ./locale/LC_MESSAGES/default.mo bindtextdomain( "domain", "./locale" ); ?> 

4- When mixing single and double quotes when using gettext () or _ () you will need to use two bindtextdomain!

 <?php // Double quote _("Hello world") is matched bindtextdomain( "domain", "./locale" ); // Single quote _('Hello world') is matched bindtextdomain( 'domain', "./locale" ); ?> 

5- Coding can be a problem in many places. If your .mo file is not in the same encoding (e.g. utf-8) than your PHP script, this may not match!

0
source share

All Articles