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?
分享到:
相关推荐
包含测试的php文件和gettext-0.14.4.exe,以及Poedit.exe 使用方法: 访问test.php?lan=zh_CN则显示简体 访问test.php?lan=en_US则显示英文 教程地址:https://www.cnblogs.com/-mrl/p/10949820.html
NULL 博文链接:https://hudeyong926.iteye.com/blog/895103
PHP Smarty通过gettext制作多语言版本
PHP多语言翻译 和php的gettext函数类似 内附demo 有兴趣的可以看下 mo文件生成要用poedit自己百度生成去
gettext-0.18.1.1.tar.zip linux
我们今天用一个简单的实例说明一下在PHP中的getText的用法(getText是一系列的工具和库函数,帮助程序员和翻译人员开发多语言软件的), 从而实现PHP的i18n. 现在, 我们假设要显示一个返回主页的link: 复制代码 代码...
PHP代码获取个text相关代码,可以把po转换成指定PHP代码获取个text相关代码,可以把po转换成指定PHP代码获取个text相关代码,可以把po转换成指定
Laravel开发-gettext 将php gettext库集成到Laravel4中
gettext,PHP,多语言,国际化编程
GetText
有些Linux系统缺少gettext库,需要自己手动编译安装。
gettext-0.17.tar.gz,Linux操作系统
php不用gettext来实现多语言,里面是一个实例实现
Gettext 用于系统的国际化(I18N)和本地化(L10N),可以在编译程序的时候使用本国语言支持(Native Language Support(NLS)),其可以使程序的输出使用用户设置的语言而不是英文. 关于gettext的更多资料请参见: 下面说说...
gnugettext. for delphi 使用说明书
gettext-0.19.3.tar 源码
Laravel开发-laravel-gettext 简单的getText加载程序和分析器