`
isiqi
  • 浏览: 16362358 次
  • 性别: 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?

分享到:
评论

相关推荐

    gettext库 多语言国际化2

    在IT行业中,多语言国际化(Internationalization)是一个重要的议题,特别是在软件开发中。"gettext"库是实现这一功能的常用工具,尤其在开源社区中广泛使用。本篇将深入探讨gettext库及其在实现多语言国际化中的...

    gettext源码

    `gettext`是一个在开源软件开发中广泛使用的工具集,主要用于多语言环境下的文本翻译。它提供了从源代码中提取可翻译字符串,管理翻译文件,以及最终将这些翻译整合回二进制可执行文件的功能。在Linux系统中,`...

    PHP多语言翻译 和php的gettext函数类似

    `gettext`是一个强大的国际化(i18n)和本地化(l10n)工具,它允许开发者将应用的文本字符串与特定的语言版本关联起来,从而实现多语言环境下的内容展示。 标题中提到的“PHP多语言翻译”是指使用PHP的`gettext`...

    PHP Smarty_gettext下载

    而`gettext`则是Linux/Unix环境中广泛使用的国际化(i18n)和本地化(l10n)工具,用于处理多语言支持。将Smarty与gettext结合,可以方便地在PHP应用中实现多语言功能。 Smarty_gettext是Smarty模板引擎的一个扩展...

    php window系统 gettext方式实现UTF-8国际化多语言(i18n)

    包含测试的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

    Laravel开发-gettext

    总结起来,`Laravel开发-gettext`是Laravel框架中用于增强多语言支持的一个强大工具。通过使用`gettext`,开发者可以更加高效地管理翻译文件,提升项目的国际化体验。无论是小型项目还是大型企业级应用,`gettext`都...

    Laravel开发-laravel-gettext

    - Gettext是GNU项目开发的一个翻译工具,它允许开发者通过一种标准的方式来处理应用程序中的文本,以便于进行多语言翻译。 - 在PHP中,Gettext通常用于提取源代码中的字符串并生成翻译文件(.po和.mo),这些文件...

    PHP中使用gettext来支持多语言的方法

    我们今天用一个简单的实例说明一下在PHP中的getText的用法(getText是一系列的工具和库函数,帮助程序员和翻译人员开发多语言软件的), 从而实现PHP的i18n. 现在, 我们假设要显示一个返回主页的link: 复制代码 代码...

    DELPHI GETTEXT源代码

    在使用GETTEXT进行多语言开发时,需要注意以下关键步骤: 1. **字符串提取**:使用dxgettext工具从Delphi源代码中提取需要翻译的字符串,生成`.pot`文件。 2. **翻译**:将`.pot`文件分发给翻译团队,他们将完成...

    PHP获取gettext工具

    PHP代码获取个text相关代码,可以把po转换成指定PHP代码获取个text相关代码,可以把po转换成指定PHP代码获取个text相关代码,可以把po转换成指定

    gettext-0.18.1.1.tar.zip

    `gettext-0.18.1.1.tar.zip` 是一个包含了 `gettext` 工具的源代码包,主要用于在Linux系统中进行多语言支持。`gettext` 是一个广泛使用的软件开发工具集,它提供了强大的国际化(i18n)和本地化(l10n)功能,使得...

    GNU Gettext Delphi Demo

    GNU Gettext Delphi Demo 是一个基于GNU Gettext技术的演示项目,主要用于展示如何在Delphi开发环境中使用Gettext工具链进行多语言支持。这个小型应用的目的是为了验证Gettext是否能够在Delphi环境下正常工作,帮助...

    源码gettext-0.19.3.tar

    6. **多语言支持**:一个应用可以通过 `gettext` 支持多种语言,只需为每种语言提供对应的 `.mo` 文件,系统会自动根据用户设置选择正确的翻译。 7. **模板和更新**:开发过程中,如果源代码中的可翻译字符串发生...

    php多语言版

    使用PHP多语言版时,开发者可以方便地在多种语言环境下工作,这有助于扩大项目的国际影响力,同时也可以吸引不同地区和背景的开发者参与项目。为了充分利用这个功能,开发者需要了解如何在代码中设置和切换语言,...

    gettext-0.18.1.1.tar.gz

    《gettext-0.18.1.1:Linux系统中的多语言支持库》 在Linux操作系统的世界里,软件本地化(Localization,简称L10n)是一个至关重要的议题,它使得全球用户能够根据自己的语言环境使用软件。"gettext"项目正是为了...

Global site tag (gtag.js) - Google Analytics