简体   繁体   中英

PHP Localization - gettext or array?

Setting up the multi language site I now need to decide which option I need to use for static text on the site. Let me explain my site setup and then if you can help me decide which solution to use.

The site is a CMS system that allows multiple domains to point to the same directory and serves content based on the domain name. So all domains share the same code. On the site I created blocks of code, called modules, that do specific jobs. So I might have a module that all it does is display the latest news from the database. I then can decide what page that module can display on and what site it will display on. So my question is, would gettext work for multiple domains that may have different modules showing up on different pages? Or should I just create a language file for each module that contains an array which has the language conversation for that specific module? So if I had 10 modules, each module would have its own language file and whatever page each module shows up it just refers to the array in the language file for that module to decide what text to show? Hope this makes sense, I read a lot about gettext and using the array version, but cant decide which one is better for this type of site setup.

In my opinion PHP gettext is the way to go. In all my projects I use the wordpress style for translation. http://codex.wordpress.org/Translating_WordPress and using the same functions naming convention:

__('message') // Return the translation
_e('message') // echo's the translation
_n('singular_message', 'plural_message', count ) // return singular or plural

I use poedit http://www.poedit.net/ to extract translatable strings from the PHP source and translate them to other languages. Storing and compiling the files in the required PHP gettext directory structure like this:

en_US/LC_MESSAGES/default.mo
nl_NL/LC_MESSAGES/default.mo
de_DE/LC_MESSAGES/default.mo

Note that .mo files are cached by PHP and changes in your .mo file are non existing until you restart the webserver. Pulling my hair out while developing I came across this very helpfull solution: http://blog.ghost3k.net/articles/php/11/gettext-caching-in-php

The whole gettext thing took me a while to work it out, but is was worth it. Once in place it saved me a great deal of time and allowed my clients to do the translation for their projects themselves.

If you want to activate a community for translating your project have a look at the web based translation tool Pootle .

In fact, I find gettext easier:

  1. You can just echo / print _("your text"); and translate later
  2. It is easy to be helped with getext editors
  3. To generate the boostrap po file (iirc, the /source/ file), you can use etags that will make a kind of grep on your files. So you'll just have to translate tokens later.

So basically, everything works since the beginning of the project, it is then easier to start, and more convenient for upscale.

A lot of CMS uses the array version. I have seen GetText as well for scalable applications. The array version is simpler especially when you want to manage the translation from a web interface.

It is a matter of preference of course.

In my experience, raw gettext isn't terribly well suited for a web site context where content changes over time and usually outside of a formal release cycle.

I'd recommend that you take a look at Zend_Translate (and Zend_Locale , if you want to localize dates, numbers, etc). Zend_Translate is a higher-level library that has adapters for various underlying methods (including gettext and arrays).

It's fairly well documented, and can be used as a standalone component.

I would recommend using gettext. It's a well-established set of tools. There is a tool to automatically extract text strings from your source code (xgettext) and there are other tools that help you with translating your localization files, for example, poedit ( http://www.poedit.net/ ).

If you are running PHP 5.3 and/or have the intl extension installed, there's another option, too: messageformatter:

http://php.net/manual/en/class.messageformatter.php

This is very powerful, but it lacks -- in my opinion -- a little bit of documentation and might be overkill for your purpose. You can find some more information about this at the ICU project's page:

http://userguide.icu-project.org/formatparse/messages

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM