ParisNakitaKejser
ParisNakitaKejser

Reputation: 14859

PHP and Gettext don't work on my server

I have a website. I'm trying to get gettext to work so that my English, Sweden and Norway sites can come up. I can't get it to work. What have I done wrong?

This is my config 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 make chmod right, or...?

Upvotes: 6

Views: 3256

Answers (2)

Kendall Hopkins
Kendall Hopkins

Reputation: 44104

I've found that some gettext installs need to have locale-gen run for each locale you want to use. I found this to be true for Ubuntu in particular. You might need to restart PHP (apache) after running locale-gen.

sudo locale-gen se_SE
sudo locale-gen no_NO

I've have a test setup (with working locale files) that can determine if you gettext setup is working.

<?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";
}

?>

Upvotes: 4

Nicolas Grasset
Nicolas Grasset

Reputation: 473

There are a few things that might go wrong.

1- To accomodate most systems, you 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 will need to have the locale installed on the server!

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

aptitude install language-pack-sv

Or just re-configured with a command like this one

sudo locale-gen sv_SE

3- Specify the name of .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 while using gettext() or _(), your 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- Encoding might be an issue in many places. If your .mo file is not in the same encoding (utf-8 for example) than your PHP script, it might not match!

Upvotes: 0

Related Questions