16.10. Using gettext
Problem
You want a comprehensive system to create, manage, and deploy message catalogs.
Solution
Use
PHP’s gettext
extension, which
allows you to use GNU’s
gettext
utilities:
bindtextdomain('gnumeric','/usr/share/locale'); textdomain('gnumeric'); $languages = array('en_CA','da_DK','de_AT','fr_FR'); foreach ($languages as $language) { setlocale(LC_ALL, $language); print gettext(" Unknown formula")."\n"; }
Discussion
gettext
is a set of tools that makes it easier
for your application to produce multilingual messages. Compiling PHP
with the --with-gettext
option enables functions
to retrieve the appropriate text from
gettext
-format message catalogs, and there are a
number of external tools to edit the message catalogs.
With gettext, messages are divided into
domains, and all messages
for a particular domain are stored in the same file.
bindtextdomain( )
tells gettext
where to
find the message catalog for a particular domain. A call to:
bindtextdomain('gnumeric','/usr/share/locale')
indicates that the message catalog for the
gnumeric
domain in the en_CA
locale is in the file
/usr/share/locale/en_CA/LC_MESSAGES/gnumeric.mo.
The
textdomain('gnumeric')
function sets the default domain to
gnumeric
. Calling gettext( )
retrieves a message from the default domain. There are other
functions, such as dgettext( )
,
that let you retrieve a message from a different domain. When
gettext( )
(or dgettext( )
) is called, it returns the appropriate message for the current locale. ...
Get PHP Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.