Search code examples
phpmultilingual

multi language website poEDit and getText()


I am trying to create a new multilanguage website. I use poEDit and getText() function. I don't know what I have missed with this code:

<?php
    if (!function_exists("gettext"))
    { 
       echo "gettext is not installed\n";
    } else  { 
       echo "gettext is supported\n"; 
    }

    $language = 'ar_JO';
    putenv("LANG=$language"); 
    setlocale(LC_ALL, $language);

    $domain = 'ar_JO';
    bindtextdomain($domain, "./locale");
    bind_textdomain_codeset($domain, 'UTF-8');
    textdomain($domain);

    echo _("HELLO_WORLD");
    echo _("hi this to be translated ");

Solution

  • The directory is very imported. Create a directory structure like this:

    locale/de_DE/LC_MESSAGES/messages_de_DE.mo
    locale/de_DE/LC_MESSAGES/messages_de_DE.po
    

    Then try this example code:

    <?php
    
    $locale = 'de_DE';
    //$locale = 'fr_CH';
    $domain = 'messages';
    $codeset = 'UTF-8';
    $directory = __DIR__ . '/locale';
    
    // Activate the locale settings
    putenv('LC_ALL=' . $locale);
    setlocale(LC_ALL, $locale);
    
    // Debugging output 
    $file = sprintf('%s/%s/LC_MESSAGES/%s_%s.mo', $directory, $locale, $domain, $locale);
    echo $file . "\n";
    
    // Generate new text domain
    $textDomain = sprintf('%s_%s', $domain, $locale);
    
    // Set base directory for all locales
    bindtextdomain($textDomain, $directory);
    
    // Set domain codeset (optional)
    bind_textdomain_codeset($textDomain, $codeset);
    
    // File: ./locale/de_DE/LC_MESSAGES/messages_de_DE.mo
    textdomain($textDomain);
    
    // test translations
    echo _('Yes');