分享
 
 
 

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

王朝php·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

from:http://www.aota.net

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:

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:

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有