Can't get gettext (php) on Ubuntu working

强颜欢笑 提交于 2019-11-30 01:59:29

I was facing the same problem. I'll describe the things I did to fix it on Ubuntu 10.10.

1) make sure you have 'gettext' installed,

sudo apt-get install gettext

Alternatively, you can install 'php-gettext' if 'gettext' cannot be installed. The package 'php-gettext' is not required if you already have 'gettext' installed.

2) Then generate the locale for your language. In this example I'll use 'sv_SE'. Look up the supported locales in '/usr/share/i18n/SUPPORTED',

less /usr/share/i18n/SUPPORTED

You'll find multiple lines that start with 'sv_SE',

sv_SE.UTF-8 UTF-8
sv_SE ISO-8859-1
sv_SE.ISO-8859-15 ISO-8859-15

This means you have multiple options for generating the locale for sv_SE. One of the options does not have a period (.) in its name (i.e. sv_SE ISO-8859-1); this is the default character set for that locale. To generate the locale for the default character set, run the following command,

sudo locale-gen sv_SE

If you want to generate that locale for the UTF-8 character set, run this command,

sudo locale-gen sv_SE.UTF-8

Restart Apache after generating locales (it won't find the newly generated locales otherwise),

sudo service apache2 restart

3) Finally, update your PHP script to match the locale you generated. If you generated the locale for 'sv_SE',

setlocale(LC_ALL, "sv_SE");

But if you generated the UTF-8 equivalent of that locale, use,

setlocale(LC_ALL, "sv_SE.UTF-8");

All should work now.

Sriram Ranganathan

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 <EMAIL@ADDRESS>\n"
        "Language-Team: LANGUAGE <LL@li.org>\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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!