2

I'm trying to support traditional chinese in one of my PHP projects, but I can't get it to show any chinese characters. Here's my gettext setup:

putenv("LC_ALL=zh_CN.utf8");
setlocale(LC_ALL, "zh_CN.utf8");
bindtextdomain("wpsmessages", dirname(__FILE__) . "/locale");
bind_textdomain_codeset("wpsmessages", 'UTF-8');
textdomain("wpsmessages");

Earlier I created the locale in SSH by typing:

sudo locale-gen zh_CN.utf8

Which then returned:

Generating locales...
zh_CN.UTF-8... done

After typing locale -a I get the follwing in the list (among others):

zh_CN
zh_CN.utf8

But still, it doesn't show any chinese characters. What am I doing wrong here?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
oschloebe
  • 386
  • 1
  • 2
  • 16
  • Not too sure I understand your examples. You are trying to get PHP get text to have Chinese support, but all of your examples are showing system level configs. Where is your PHP code that fails? Is it pulling data from the database? Is the database & PHP script UTF8 aware? – Giacomo1968 Dec 21 '13 at 15:39
  • possible duplicate of [Can't make (UTF-8) traditional Chinese character to work in PHP gettext extension (.po and .mo files created in poEdit)](http://stackoverflow.com/questions/2264740/cant-make-utf-8-traditional-chinese-character-to-work-in-php-gettext-extensio) – Giacomo1968 Dec 21 '13 at 15:40
  • 1
    Sorry, I wasn't too clear about this. So I basically have a gettext ready PHP project calling `__('This is some text')`, getting the text from .mo and .po files residing in a `/locale/` folder. gettext is working fine for locales de_DE.utf8, nl_NL.utf8, pt_BR.utf8 etc, but I can't get it to print the strings for simplified chinese. Instead, it prints the english default strings. – oschloebe Dec 21 '13 at 15:47
  • Okay, now I seem to get it. But the larger issue issue is it seems you are using WordPress for this because you say you are calling via `__()` correct? Added that as a tag & changed the title. Should help bring more eyes to this issue. Also, look at this additional question: http://stackoverflow.com/questions/5257519/cant-get-gettext-php-on-ubuntu-working?rq=1 – Giacomo1968 Dec 21 '13 at 15:53
  • Hey Jake, I'm not using WordPress, but the PHP gettext functions (http://www.php.net/manual/en/ref.gettext.php) using the config I posted in my initial post. What gets me wondering is that gettext is working fine for several locales in my project, but it refuses to work for simplified chinese. I'm not even sure if `zh_CN.utf8` is the correct locale. Maybe `zh_CN.GB2312` (where GB2312 is the encoding) is the correct one, but I tried both, neither worked. My `wpsmessages-zh_CN.mo` file seems correct to me. – oschloebe Dec 21 '13 at 16:07
  • Hmmm. Okay. My guess is that the translation files you are looking for are the issue here. And in my experience with other multilingual sites, there could be a mismatch between what you believe the translation files are & what they really are. Good luck! – Giacomo1968 Dec 21 '13 at 16:11
  • 2
    Damnit, sorry to have wasted your time with this, but the issue was a file name convention issue. The `wpsmessages-zh_CN.mo` should have been named `wpsmessages.mo`, but you couldn't know that. Please delete this topic, burn it, burn me, whatever you like. :) This is embarrassing. – oschloebe Dec 21 '13 at 16:13
  • You can delete this question if you wish, but I would say that answering your own question & selecting that as the answer is the best route. This might seem like a stupid topic to you, but I can easily see others solving problems by following your logic. – Giacomo1968 Dec 21 '13 at 16:14

1 Answers1

3

A little emberrassing, but the solution was simply renaming the file wpsmessages-zh_CN.mo to wpsmessages.mo following my own file name conventions.

Basically the structure of my PHP project using gettext is like this:

/locale/pt_BR.utf8/LC_MESSAGES/wpsmessages.mo
/locale/zh_CN.utf8/LC_MESSAGES/wpsmessages.mo
...

So using bindtextdomain("wpsmessages", dirname(__FILE__) . "/locale"); gets the correct .mo file for a locale specified by setlocale(), but it couldn't find the file as it's name was wrong.

Hope this helps anyone being as stupid as me. :)

oschloebe
  • 386
  • 1
  • 2
  • 16