Can't get gettext (php) on Ubuntu working

前端 未结 2 711
暗喜
暗喜 2020-12-25 08:06

The following example works on Mac OS X with Apache, i.e. I get the translated string echoed back. But on Ubuntu with lighttpd I get the original text \'Inactive account\'.

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-25 08:28

    Though I followed the steps outlined in the accepted answer, I couldn't get it to work on Ubuntu 14.04 LTS. I had to do two important things:

    (1) Before generating the messages.mo file, I had to specify in message.po explicitly UTF-8 for charset:

    msgid   ""
    msgstr  "Project-Id-Version: PACKAGE VERSION\n"
            "Report-Msgid-Bugs-To: \n"
            "POT-Creation-Date: 2016-12-19 16:28+0530\n"
            "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
            "Last-Translator: FULL NAME \n"
            "Language-Team: LANGUAGE \n"
            "Language: \n"
            "MIME-Version: 1.0\n"
            "Content-Type: text/plain; charset=UTF-8\n"    <---------change here
            "Content-Transfer-Encoding: 8bit\n"
    

    (2) I had to maintain same folder name as the locale installed. For instance, I installed Kannada by running sudo locale-gen kn_IN. This generated kn_IN.UTF-8. Note the capital letters and hyphen in UTF-8. I used this same name while setting locale in my php script and for the folder name where I put the messages.mo file:

    PHP Script:

    // I18N support information
    $locale = "kn_IN.UTF-8";       <----------change here
    putenv("LANG=".$locale); 
    putenv("LC_ALL=".$locale);
    setlocale(LC_ALL, $locale);
    
    $domain = 'messages';
    bindtextdomain($domain, "./Locale");
    bind_textdomain_codeset($domain, 'UTF-8');
    textdomain($domain);
    

    Folder Name:

    project_folder
        /Locale
            /kn_IN.UTF-8      <------change here
                /LC_MESSAGES
                    /messages.mo
    

    Ubuntu is case sensitive to folder and file names. So make sure the capital letters are maintained as shown above.

提交回复
热议问题