PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Subscribe to PHP Subscribe to Newsletters

Gettext
Pages: 1, 2

Distributing the Pot File

Ok, so now that you have your pot file with the strings that need to be translated, you need to distribute it to your translators. In a successful open source project, you would have several different volunteers that take care of the translation of your user interface messages. In this case, you would send an email to the development mailing list and tell the volunteers that the next release would go out on the X date and that the following list of languages need to be updated.



That's, of course, in a successful project. The normal situation, though, would have you, the project leader, to do the most important translations yourself and wait for contributions from volunteers. In a Web site or a Web application, this is usually not the case. That is, you normally wouldn't have a team of volunteers working on your personal Web site, but the picture is different for a community Web site like themes.org, for instance.

In any case, either you or the volunteers will translate the pot file and then you will need to convert the file into a binary file that Gettext actually "understands." For that you would use the following command:

$ msgfmt messages.po

The line above will create a messages.mo file, which you should save in the appropriate locale/<LANG_CODE>/LC_MESSAGES/ ng strings y.

Managing Evolving Pot Files

Now think about all of this for a moment -- you have a Web site that is constantly evolving, with new features being added or inconsistencies being removed, and your strings to be translated are also changing. New ones are added, old ones are modified, and so on.

So how do you manage multiple versions of the pot files? In a usual situation, you will have a messages.po that is completely translated for a specific language, and a new file with the new strings to be translated. The problem lies here: since Gettext doesn't work in any other way, this new file will look just like the example above -- it will be empty.

The question is how to merge the files in a way that keeps the already- translated strings while adding the new untranslated ones. The answer is provided by the msgmerge Gettext utility. An example of its use would be the following sequence of commands:

$ ls
example.php
$ xgettext -n *.php
$ ls
example.php   messages.po
// ...
// Translates the messages.po file now
// ...
$ msgfmt messages.po
$ ls
example.php   messages.po   messages.mo
// ...
// Changes the example.php file
// ...
$ mv messages.po old.po
$ xgettext -n *.php
$ ls
example.php   messages.po   messages.mo   old.po
$ msgmerge old.po messages.po --output-file=new.po
$ ls
example.php   messages.po   messages.mo   new.po    old.po
// ...
// Translates the new.po file
// ...
$ msgfmt new.po

What to Do Next

The best thing to do now is to experiment. You will need to enable the Gettext PHP extension and start to play around with it. The example provided here is simple but should be enough to build applications for multiple languages.

More information can be found in the Gettext manual.

Have fun.

Joao Prado Maia is a web developer living in Houston with more than four years of experience developing web-based applications and loves learning new technologies and programming languages.


Return to the ONLamp.com.




Tagged Articles

Post to del.icio.us

This article has been tagged:

php

Articles that share the tag php:

Understanding MVC in PHP (477 tags)

The PHP Scalability Myth (123 tags)

The Dynamic Duo of PEAR::DB and Smarty (53 tags)

PHP Form Handling (43 tags)

Very Dynamic Web Interfaces (39 tags)

View All

gettext

Articles that share the tag gettext:

Gettext (4 tags)

View All

programming

Articles that share the tag programming:

Rolling with Ruby on Rails (1374 tags)

Very Dynamic Web Interfaces (279 tags)

Ajax on Rails (231 tags)

Understanding MVC in PHP (202 tags)

A Simpler Ajax Path (186 tags)

View All

i18n

Articles that share the tag i18n:

Unicode Secrets (7 tags)

More Unicode Secrets (6 tags)

XML on the Web Has Failed (6 tags)

Internationalization and Localization with PHP (5 tags)

Internationalization, Part 2 (4 tags)

View All

tutorial

Articles that share the tag tutorial:

Rolling with Ruby on Rails (1417 tags)

A Simpler Ajax Path (135 tags)

Ajax on Rails (88 tags)

Rolling with Ruby on Rails, Part 2 (66 tags)

Very Dynamic Web Interfaces (66 tags)

View All

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com