开帖讨论多语言处理,本地化服务的问题

xiezhi 2004-09-15 12:09:44
今天开到这么篇文章,说到PHP有这么好的功能,不知道是怎么实现的。我通常的做法是文章所说的使用用于语言翻译的烦杂文件,不知道他咋就不用了呢?
http://dev.csdn.net/Develop/article/37/article/37/37155.shtm
PHP的好处


我不是JSP或者ASP的老手,在此我也不想贬低这些语言。相反,我会把注意力放在PHP的好处上。

本地化
PHP让你能够为网站的访问者提供本地化的服务。当用户点击进入网站的时候,网站会根据他们浏览器的设置自动地以其母语向其提供页面。要实现这一点不需要使用用于语言翻译的烦杂文件,而是使用和本地化的C程序所具有的相同能力,通过一个叫做gettext的系统实现的。如果被请求的语言文件存在,那么用户所看到的文本就是其母语;如果语言文件不存在,那么文本就是缺省的英语或者其他任何你所指定的语言。许多本地化的UNIX应用程序都将gettext作为标准,它让第三方的翻译变得轻而易举。
...全文
198 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
blueoxygen 2004-09-17
  • 打赏
  • 举报
回复
Quick Primer on Using gettext

PHP's gettext functions provide an interface to working with the GNU gettext utility. gettext is a GNU utility that helps programmers internationalize programs. A brief and somewhat inaccurate description of internationalization (often abbreviated I18N) is that it is the process of making software portable to languages and cultures other than the originating language(s) and culture(s). gettext helps this process by providing a set of tools to help manage the messages output by the software.

Following is a brief overview of how to use gettext with a PHP script:

1. Get and install a copy of gettext. (See http://www.gnu.org/ for more details.)

2. Make sure PHP is built with gettext support (i.e., with --with-gettext on the ./configure command line).

3. Develop your script/application, writing the messages in the language of your choice. Our sample application looks like this:

<pre>
<?
// Write a small application to print "Good Morning!" in a variety of languages
// Rather than hard-code the greetings, use gettext to manage the translations

// Make an array
// Use the ISO two-letter codes as keys
// Use the language names as values
$iso_codes = array (
'en'=>'English',
'fr'=>'French',
'it'=>'Italian',
'pt'=>'Portuguese',
'es'=>'Spanish'
);

foreach ($iso_codes as $iso_code => $language) {
// Set the LANGUAGE enviroment variable to the desired language
putenv ('LANGUAGE='.$iso_code);

// Print out the language name and greeting
// Note that the greeting is wrapped in a call to gettext
printf ("<b>%12s:</b> %s\n", $language, gettext("Good morning!"));
}
?>
</pre>



3. Extract the translatable strings with the xgettext utility. (xgettext is part of the gettext utilities and is not a PHP function.) A sample command line for the utility might look like this:

xgettext \
--extract-all \
--c++ \
--default-domain=greetings \
--indent \
--omit-header \
--sort-output \
--width=76 \
gettext.php



Note

You may need different command line options than those noted above. For more information on xgettext, consult the gettext documentation or run xgettext --help.

xgettext parses the specified file (in the previous example, gettext.php), looking for strings of characters. From the found strings, xgettext generates a file called greetings.po, which looks something like this:

#: gettext.php:28
#, c-format
msgid "<b>%12s:</b> %s\n"
msgstr ""

#: gettext.php:28
msgid "Good morning!"
msgstr ""



Note that xgettext was designed for parsing C and C++ files, and ignores single-quoted strings.

Lines starting with # are comments.

msgid contains the text of the original, untranslated message.

msgstr contains the translated message.

4. Open the .po file in your favorite text editor and remove any messages that you don't want translated. This would probably leave us with something like this:

#: gettext.php:26
msgid "Good morning!"
msgstr ""



5. Create a directory to store the tranlations.

6. In the directory created in step 5, create one subdirectory for every language to which you will be translating. Name the directories for the ISO 639 language code - ar for Arabic, co for Corsican, en for English, and so on. See http://lcweb.loc.gov/standards/iso639-2/englangn.html for the codes.

7. In each subdirectory, create a subdirectory named LC_MESSAGES.

8. Place one copy of your .po file in every LC_MESSAGES directory.

9. Have your translators translate the messages in the .po file in the appropriate directory.

10. When the translations are ready, use the msgfmt utility to convert the .po files into the compact, binary format used by gettext. (msgfmt is another part of the gettext package, and is not a PHP function.) Basic syntax for converting the files is as follows:

msgfmt -o output_file.mo input_file.po



Note

You may need different command line options than those noted above. For more information on msgfmt, consult the gettext documentation or run msgfmt --help.

11. Modify your original script so that the gettext function knows where to find the translated versions:

<pre>
<?
// Bind a domain to directory
// Gettext uses domains to know what directories to
// search for translations to messages passed to gettext
bindtextdomain ('greetings', './translations');

// Set the current domain that gettext will use
textdomain ('greetings');

# Make an array
# Use the ISO two-letter codes as keys
# Use the language names as values
$iso_codes = array (
'en'=>'English',
'fr'=>'French',
'it'=>'Italian',
'pt'=>'Portuguese',
'es'=>'Spanish'
);

foreach ($iso_codes as $iso_code => $language) {
# Set the LANGUAGE environment variable to the desired language
putenv ('LANGUAGE='.$iso_code);

# Print out the language name and greeting
# Filter the greeting through gettext
printf ("<b>%12s:</b> %s\n", $language, _("Good morning!"));
}
?>
</pre>



If you encounter troubles with these examples, read the gettext documentation. While gettext is simple to use, it takes a bit of time to get the hang of it.
blueoxygen 2004-09-17
  • 打赏
  • 举报
回复
What is the best way to write an application for different languages?
What is gettext used for?

May 14th, 2000 20:22

Nathan Wallace
chuck


I'd suggest gettext - there is gettext support in php 3.0.something (6?
9?) and later, and in php4 (compile --with-gettext). There's not much
documentation right now, but it's pretty easy to use - you make the
translations, set the locale, and then put _() around all your strings,
and boom, it works. Fast, and also fairly standard.
xiezhi 2004-09-17
  • 打赏
  • 举报
回复
不知道也可以帮顶一下嘛!
xiezhi 2004-09-17
  • 打赏
  • 举报
回复
继续啊~~~
loveconan 2004-09-16
  • 打赏
  • 举报
回复
gettext有时候会出问题
不知道为啥~~~
xiezhi 2004-09-16
  • 打赏
  • 举报
回复
blazingSnow(月光飞闪刀剑吻) 说的方法正是我现在在用的,或我跟你的方法类似。不过我看那篇文章真是说的神乎其神,真有那么强么。知道有那么强就应该知道怎么做到那么强。不至于知道它是神兵利器,缺不了解怎么运用,还一直拿它砍柴。岂不是笑话!
pswdf 2004-09-16
  • 打赏
  • 举报
回复
PHP真的不错。
caojinrong 2004-09-16
  • 打赏
  • 举报
回复
up
朝阳 2004-09-16
  • 打赏
  • 举报
回复
观注,学习!
xuzuning 2004-09-16
  • 打赏
  • 举报
回复
看来都对 gettext 不甚了了啊
blazingSnow 2004-09-16
  • 打赏
  • 举报
回复
多语言处理还是用模板之类的东西解决,也可以自己写一个函数,在你开发完之后,运行一下来查找替换,对于数据库中的字符,那是需要同时存储几个语种了。

比如你的程序里面有朋友一词,那么开发的时候不要用朋友,你自己定义你的标签
例如:
<MYWEB:FRIEND>之类,之后,你用你的函数来查找这些标签,帮你翻译一下,替换成相应的字符....

呵呵,上面是自己用的简陋的方法....
mrshelly 2004-09-15
  • 打赏
  • 举报
回复
PHP+模板,我觉得是最好的办法.

当然,客户端的输入,不可以多语言的.除非有智能化的翻译代码.
blueoxygen 2004-09-15
  • 打赏
  • 举报
回复
这个这个...缺乏重量级商业支持...感觉比较重要 没有ibm bea java也不会发展到今天
另外,企业级应用不是想象得那么简单...看看j2ee再看看php...我想没必要非要跻身什么行列,ms得asp就是霸占了中低端市场阿 php的明天是美好的 但是,不能迷信php
哦对了,其实asp也是无所不能 只是现在的asp程序员没几个会com得,实力大打折扣
pwtitle 2004-09-15
  • 打赏
  • 举报
回复
phplet是不错,可惜在具体应用上还不成熟。
xiezhi 2004-09-15
  • 打赏
  • 举报
回复
google的强大是人所共知的,不知道php是如何提供的这中功能,看到那篇文章,真是觉得眼界大开,还有其他种种的功能,让我唏嘘不已!希望作者或者知情者能告诉下我如何让第三方的翻译变得轻而易举,当然其他的那些功能的实现方法说说也行啊!
ashchen 2004-09-15
  • 打赏
  • 举报
回复
php还能做web服务器呢,呵呵
cuipi2003 2004-09-15
  • 打赏
  • 举报
回复
我觉得,如果单单是用模版来处理应该不是PHP的独有的,而且看不出来什么优势。可能是我PHP学的比较浅,我一直都作ASP,用模版处理是一种编程思想和语言没有什么关系。
我倒是最近研究PHP的msn机器人的时候发现,PHP的底层功能十分诱人,在ASP中要借助COM来实现,而PHP可以直接用内在函数进行网络通信,支持打开网络的 Socket 链接。看来PHP在开源以后变得很强大了
lzkd 2004-09-15
  • 打赏
  • 举报
回复
好象....... google就是这样吧,你是什么语言过去,它就显示什么语言的版本给你

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧