11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 23 • BUILDING WEB SITES FOR THE WORLDwww.it-ebooks.infoTranslating Web Sites with GettextGettext (www.gnu.org/software/gettext), one of the many great projects created and maintained by theFree Software Foundation, consists of a number of utilities useful for internationalizing and localizingsoftware. Over the years it’s become a de facto standard solution for maintaining translations forcountless applications and web sites. <strong>PHP</strong> interacts with gettext through a namesake extension, meaningyou need to download the gettext utility and install it on your system. If you’re running Windows,download it from http://gnuwin32.sourceforge.<strong>net</strong> and make sure you update the PATH environmentvariable to point to the installation directory.Because <strong>PHP</strong>’s gettext extension isn’t enabled by default, you probably need to reconfigure <strong>PHP</strong>. Ifyou’re on Linux, you can enable it by rebuilding <strong>PHP</strong> with the --with-gettext option. On Windows, justuncomment the php_gettext.dll line found in the php.ini file. See Chapter 2 for more informationabout configuring <strong>PHP</strong>.The remainder of this section guides you through the steps necessary to create a multilingual website using <strong>PHP</strong> and gettext.Step 1: Update the Web Site ScriptsGettext must be able to recognize which strings you’d like to translate. This is done by passing alltranslatable output through the gettext() function. Each time gettext() is encountered, <strong>PHP</strong> will lookto the language-specific localization repository (more about this in Step 2) and match the stringencompassed within the function to the corresponding translation. The script knows which translationto retrieve due to earlier calls to setlocale(), which tells <strong>PHP</strong> and gettext which language and countryyou want to conform to, and then to bindtextdomain() and textdomain(), which tell <strong>PHP</strong> where to lookfor the translation files.Pay special attention to the mention of both language and country because you shouldn’t simplypass a language name (e.g., Italian) to setlocale(). Rather, you need to choose from a predefinedcombination of language and country codes as defined by the International Standards Organization. Forexample, you might want to localize to English but use the United States number and time/date format.In this case, you would pass en_US to setlocale() as opposed to passing en_GB. Because the differencesbetween British and United States English are minimal, largely confined to a few spelling variants, you’donly be required to maintain the few differing strings and allow gettext() to default to the stringspassed to the function for those it cannot find in the repository.■ Note You can find both the language and country codes as defined by ISO on many web sites; just search forthe keywords ISO, country codes, and language codes. Table 23-1 offers a list of common code combinations.450

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!