0

I'm working on a multilanguage PHP website that uses Poedit for translations and I'm having problems with a specific language, that is Spanish.

I will start saying that in the server (after website is published) both languages work fine but in my DEV environment only english is translated.

FYI, spanish locales were not installed on my Ubunto system, so I have installed them via: sudo apt-get install language-pack-es and generated relevant stuff via: sudo locale-gen es.

I have the next folder structure under my web:

languages => en_US => LC_MESSAGES => .po + .mo (compiled) files

languages => es_ES => LC_MESSAGES => .po + .mo (compiled) files

and the set_locale.php file is the next:

<?php
    // Include the Composer autoloader
    require_once 'vendor/autoload.php';
    // Update include path
    require_once 'Audero/SharedGettext/SharedGettext.php';

    $translationsPath = 'languages';
    $language = 'es_ES'; //en_ZM
    if (isset($_GET['lng'])) {
        $getLocale = $_GET['lng'];
        if($getLocale=="en") {
            $language="en_US";
        }
    }
    $domain = 'audero';

    putenv('LC_ALL=' . $language);
    setlocale(LC_ALL, $language);

    try {
        $sharedGettext = new Audero\SharedGettext\SharedGettext($translationsPath, $language, $domain);

        // Create the mirror copy of the translation and return the new domain
        $newDomain = $sharedGettext->updateTranslation();

        $sharedGettext->deleteOldTranslations();

        // Sets the path for the current domain
        bindtextdomain($newDomain, $translationsPath);

        // Specifies the character encoding
        bind_textdomain_codeset($newDomain, 'UTF-8');

        // Choose domain
        textdomain($newDomain);

        //die(print_r("", true ));
    } catch(\Exception $ex) {
        echo $ex->getMessage();
    }
?>

And I translate texts in the following way:

...
<title><?php echo _("home_title"); ?></title>
...

But for spanish I get the key (home_title following the the example) but not the translation.

Any help?

Thanks.

Diego Perez
  • 2,188
  • 2
  • 30
  • 58
  • Did you try other locale (such like fr_fr)? Then you would be able to know that the problem is around your Spanish locale setting, or gettext is not properly called. – akky Jan 29 '19 at 23:02
  • Thanks for your comments @akky, the fact is that not only english (en_US) works fine (so it cannot be the way gettext is called) but I've also tried "en_ZM", a random locale that was installed on my Ubuntu machine and it also works, and the key (what differentiates) is that spanish was not installed on my system, and I've done it the way I describe above, so it would surely be related with that, but I'm stuck. – Diego Perez Jan 30 '19 at 10:37
  • Try with `es_ES.utf8`. This has worked for me in some situations – Tim Jan 30 '19 at 10:54
  • Hello @Tim, and thank you very much for your response and help. Good point yours, I've thought of that, but could you explain a little bit more? You mean I have to rename the folder "es_ES" to "es_ES.utf8" or what exactly do I have to do? – Diego Perez Jan 30 '19 at 15:54
  • I mean, if I do locale -a | grep es_ the result is: es_AR.utf8 es_BO.utf8 es_CL.utf8 es_CO.utf8 es_CR.utf8 es_CU es_CU.utf8 es_DO.utf8 es_EC.utf8 es_ES.utf8 es_GT.utf8 es_HN.utf8 es_MX.utf8 es_NI.utf8 es_PA.utf8 es_PE.utf8 es_PR.utf8 es_PY.utf8 es_SV.utf8 es_US.utf8 es_UY.utf8 es_VE.utf8 so almost every locale ends with .utf8 so, in practical terms, what should I do different from what I've already done? You mean something in php code? something in poedit? something in system? – Diego Perez Jan 30 '19 at 16:16
  • I've never had to solve this in practical terms because I simply avoid the gettext extension. MO files can be parsed very quickly into memory with some readily available code libraries. (See WordPress. Used by ??million sites and doesn't suffer this problem) – Tim Jan 31 '19 at 13:26
  • I should clarify that I originally meant to try: `setlocale(LC_ALL, 'es_ES.utf8' );` in your code. This has worked for me in the past, but I've also found that on clean servers I've had to run the `localedef` command to build the folders. As I say - a problem I prefer to eliminate. – Tim Jan 31 '19 at 13:31
  • Thank you very much @Tim, I will consider and try your suggestion. – Diego Perez Feb 01 '19 at 11:40

0 Answers0