phpgettextpophp-gettextmo

gettext stops working randomly


I am stuck in a rut here. When browsing through a translated version of my localized site, the translation randomly stops, and I have no idea why; I'm not sure if it has to do with the way I'm setting the Locale or what, but it randomly stops translating and reverts back to english. Once it does that, the only way to get it back is to go to the home page, highlight the URL in the address bar and press enter.

I'm hoping someone could have a look at the way I'm setting the locale in PHP and seeing if there's something I'm missing or screwing up.

<?php
explode(";", setlocale(LC_ALL, 0)); 
$pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
if (strpos($pageURL,'es.') !== false) {
    $language = 'es_ES.utf8';
}
elseif (strpos($pageURL,'it.') == true) {
    explode(";", setlocale(LC_ALL, 0)); 
    $language = 'it_IT.utf8';
}
elseif (strpos($pageURL,'de.') == true) {
    explode(";", setlocale(LC_ALL, 0)); 
    $language = 'de_DE.utf8';
}
elseif (strpos($pageURL,'pt.') == true) {
    explode(";", setlocale(LC_ALL, 0)); 
    $language = 'pt_PT.utf8';
}
elseif (strpos($pageURL,'fr.') == true) {
    explode(";", setlocale(LC_ALL, 0)); 
    $language = 'fr_FR.utf8';
}
else {
    explode(";", setlocale(LC_ALL, 0)); 
    $language = 'en_US.utf8';
}
putenv("LANG=" . $language);
explode(";", setlocale(LC_ALL, 0)); 
setlocale(LC_ALL, $language);

// Set the text domain as "messages"
$domain = "messages";
bindtextdomain($domain, "Locale"); 
bind_textdomain_codeset($domain, 'UTF-8');

textdomain($domain);

?>

If any of you have any ideas as to why gettext randomly stops working, please let me know. Also please let me know if there's a snippet of code elsewhere that you'd need to see in order to address the issue. I really appreciate it!


Solution

  • $pageURL = 'es.url.com/itdept.php';
    
    if (strpos($pageURL , 'es.') !== FALSE) 
        echo  'it is spanish' . PHP_EOL;
    
    // If this line was before the one above then using the example
    // shown you'd be showing the portugese version.
    
    if (strpos($pageURL , 'pt.') !== FALSE) 
        echo  'Oh, yeah, and its Portugese as well ...' . PHP_EOL;
    

    If your intention is to assess the first 3 chars then that if/else block is, well, not nice.

    Rewrite it so you isolate the language only once, then match it to an easier to read/write array.

    // $incoming = 'eeks';  // uncomment to test default
    $incoming = 'es';  // grab this using substr() function 
    $langs = array(
        'en'=>'English',
        'pt'=>'Portugese',
        'es'=>'Espanol',
    );
    
    if(array_key_exists($incoming, $langs)){
        echo 'Setting lang to ' . $langs[$incoming] ;
    }else{
        echo 'Setting default lang to ' . array_shift($langs);
    }
    

    As a bonus you get to check incoming vs a whitelist.

    If it was NOT your intention to assess the first 3 chars then this might be an incorrect answer.