phparraysgettextzend-translatephp-gettext

Use PHP Gettext without having to install locales


I've been looking at options for internationalizing an open source project:

Can I somehow read .mo files without Gettext or Zend_Translate, or must I use Gettext? If so, how can I make all locales work, like in the question I linked to above?

EDIT: I'm now willing to use Zend_Translate. I just need to work out what files I need (it would be great if they could be combined into one file) - I don't want the entire Zend Framework in my project.


Update: I was interested to see how big open source projects handle i18n:

So none of those three random projects use Zend_Translate, nor gettext directly, as far as I can see.

Maybe it's a good idea to use the C locale, store the name of the language in the text domain name, and go from there.


So here's as far as I've got with that:

$lang = 'de'; //debug
setlocale( LC_ALL, 'C' );
bindtextdomain( 'default', PATH . "/locale/$lang" );
bind_textdomain_codeset( 'default', 'UTF-8' );
textdomain( 'default' );

var_dump( file_exists( PATH . "/locale/$lang/C/LC_MESSAGES/default.mo" ) ); //bool(true)

But I still just get the English string, even though I've used poedit, msgfmt etc. to make the appropriate files. I've also tried restarting Apache.


Solution

  • Here is the solution:

    $lang = 'de'; //debug
    setlocale( LC_ALL, 'C.UTF-8' );
    bindtextdomain( 'default', PATH . "/locale/$lang" );
    bind_textdomain_codeset( 'default', 'UTF-8' );
    textdomain( 'default' );
    

    The only difference between that and the example I posted at the bottom of my answer is that it uses C.UTF-8 not just C.

    I'll be doing more testing of this, and if it works cross-platform, and will update this answer if I find out anything else.