`
isiqi
  • 浏览: 16029557 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

linux下php使用gettext开发多语言站点

阅读更多

from:http://www.aota.net

<!--StartFragment -->

With the upgrade of PHP to version 4.1.1 a new feature was introduced; gettext. Here's a small write-up about how to use gettext on your site.

So, what is gettext and how do I use it?
PHP's gettext functions provide an interface to the GNU gettext utility, which is a utility for programmers to internationalize their programs. Gettext helps manage the messages output by the software. You can also use it for internationalization (often abbreviated to I18N) of your website using PHP.

The basic usage in your PHP script is fairly simple;
1. set the language to be used
2. echo/print your text as you normally would, prepending 'gettext' or an underscore '_'. So instead of 'echo "Hello World"', you would write 'echo _("Hello World")' or 'echo gettext("Hello World")'

The translations are stored in a compressed binary file (.mo files), which you create from the plain ASCII files created by gettext from your script (.po files).

So, step by step, how to create your script.
Let's assume the script you want to internationalize looks like this:

PHP:
<?php <BR>echo?_("Hello World");
?>


The first step would be to create the .po script by extracting all the translatable string. For that you use the 'xgettext' program (xgettext is part of the gettext utilities and is not a PHP function). So, you invoke 'xgettext';
xgettext --default-domain=greetings -k_ hello.php

This results in a file called 'greetings.po' being created. The contents of the .po file looks like this:
#: hello.php:2
msgid "Hello World"
msgstr ""

The first line is a comment, the second line the string that is to be translated, the third line will hold the translation of the string.

Let's translate it into Dutch. Open the 'greetings.po' file in a text editor and put the translation after the 'msgstr' directive. Your 'greetings.po' file should now look something like this:
#: hello.php:2
msgid "Hello World"
msgstr "Hallo Wereld"

Now, let's make the binary .mo from this. For that you use the utility 'msgfmt' (msgfmt again is part of the gettext utilities);
msgfmt -o greetings.mo greetings.po

So, now you have your translation, but how do you change your script to show the translation?
First of all create a subdirectory called 'locale' and in that subdirectory make a subdirectory for the language, in this case 'nl_NL' and in that directory create a directory 'LC_MESSAGES'. Put the 'greetings.mo' file in the subdir 'LC_MESSAGES'. You should now have the file 'locale/nl_NL/LC_MESSAGES/greetings.mo'.
Secondly the original PHP script will have to be adapted. The locale needs to be set to the right language and the so-called textdomain will have to be set to the right file ('greetings.mo').
Next, what the script could look like, although in a real application you'd of course want to set the language based on $HTTP_ACCEPT_LANGUAGE or a setting chosen by the user.

PHP:
<?php <BR>putenv("LANG=nl_NL");
setlocale('LC_ALL',?"nl_NL");
bindtextdomain("greetings",?"./locale/");?
textdomain("greetings");

echo?_("Hello World");
?>



Now you should have a working script that outputs "Hallo Wereld". Basically that's all there's to it.

All the gettext utilites you need are installed on the FutureQuest servers, but what if you want to develop the script on your Windows PC?
First of all, enable 'gettext' on your Windows' PHP installation. Open your php.ini file, which should be in \winnt or \windows, if it's not take php.ini-dist from the PHP directory and copy it to your main Windows directory. Next, uncomment the line ";extension=php_gettext.dll", by removing the semi-colon. Then set the extension_dir directive to wherever PHP is located (e.g. "f:\php\extensions\"). Next restart Apache, when there's no error look at the info outputted by phpinfo() to see if 'gettext' is now indeed enabled.

Secondly, you'll have to get a copy of the gettext utilites compiled for Win32;
http://sourceforge.net/projects/mingwrep/
http://home.a-city.de/franco.bez/ge...t_win32_en.html

That's it, you should now be ready to develop your PHP scripts with gettext under Windows.

Although the .po files can be edited in any plain text editor, some people have developed special editors for the job;
http://www.gtranslator.org/
http://poedit.sourceforge.net/
http://i18n.kde.org/tools/kbabel/
http://www.geocities.com/bilibao/
http://muli.sourceforge.net/

Most aren't for Windows, but poEdit is available as a Windows application. Vi/Vim (also available for Windows) isn't specifically intended for translating .po files, but does include syntax coloring for it, which can be handy.

So, that was a -hopefully useful- primer on the basics of using gettext with PHP, here are some more URLs where people can get more information;
http://www.php.net/manual/en/ref.gettext.php
http://www.gnu.org/manual/gettext/h...ettext_toc.html
http://www.php-er.com/chapters/Gettext_Functions.html

Arthur

原文请见: http://www.aota.net/forums/showthread.php?threadid=10615?

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics