转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 数据库 >> SyBase >> 正文
linux下php使用gettext开发多语言站点         ★★★★

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

作者:闵涛 文章来源:闵涛的学习笔记 点击数:730 更新时间:2009/4/22 23:08:10

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 


[MySql]PHP存取 Mysql 数据乱码终极解决方案  [Web开发]PHP提示Notice: Undefined variable的解决办法
[Web开发]PHP 大小写函数  [Web开发]PHP的sleep函数关闭窗口后是否继续运行?
[Web开发]教你如何在PHP开启gzip页面压缩实例介绍  [Web开发]PHP过滤HTML字符串的常用函数使用方法介绍
[Web开发]PHP采集程序常用函数大全  [Web开发]用PHP实现Javascript的escape(),unescape()的方法
[Web开发]常见的PHP截取字符串函数整理  [系统软件]突破Windows 2003 PHP服务器的新思路
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Sql Server  · MySql
    · Access  · ORACLE
    · SyBase  · 其他
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台