关于PHP的i18n(国际化)的一点总结(1)----gettext部分

原创 2005年05月21日 17:18:00

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.
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.

i18n和i10n:国际化&本地化--gettext

[正文] froward 软件编写和文档通常使用英文,这便于世界各地的程序员交流,但是并非所有用户都懂英文,所以程序能显示为用户的母语,能使软件用户群更大,且方便了普通...

php 通过include方式实现国际化多语言(i18n)

php 通过include方式实现国际化多语言(i18n) 标签: include语言phpmenuapache 2009-11-05 17:09 4797人阅读 评论(0) 收藏 举报 ...
  • csnewdn
  • csnewdn
  • 2016年12月27日 08:57
  • 335

Java for Web学习笔记(七三):国际化i18n(1)使用Spring框架MessageSource

一般性了解 我们在JSTL fmt[1]中已经接触过国际化i18n,本地化L10n。使用JSTL fmt(Internationalization and Formatting tag library...

c/c++ ,gettext,软件的多语言支持(i18n)

一个小例程,用于演示: cat >hello.cpp

javaweb学习总结(三十一)——国际化(i18n)

 一、国际化开发概述   软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的、符合来访者阅读习惯的页面或数据。   国际化(inter...

javaweb学习总结(三十一)——国际化(i18n)

只为成功找方法,不为失败找借口! javaweb学习总结(三十一)——国际化(i18n) 一、国际化开发概述   软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访...

javaweb学习总结(三十一)——国际化(i18n)

一、国际化开发概述   软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的、符合来访者阅读习惯的页面或数据。   国际化(interna...

javaweb学习总结(三十一)——国际化(i18n)

一、国际化开发概述   软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的、符合来访者阅读习惯的页面或数据。  国际化(internation...

javaweb学习总结(三十一)——国际化(i18n)

javaweb学习总结(三十一)——国际化(i18n) 一、国际化开发概述   软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的、...

javaweb学习总结(三十一)——国际化(i18n)

一、国际化开发概述   软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的、符合来访者阅读习惯的页面或数据。   国际化(interna...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:关于PHP的i18n(国际化)的一点总结(1)----gettext部分
举报原因:
原因补充:

(最多只允许输入30个字)