11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 23 • BUILDING WEB SITES FOR THE WORLDwww.it-ebooks.infoStep 5: Generate Binary FilesThe final required preparatory step involves generating binary versions of the messages.po files, whichwill be used by gettext. This is done with the msgfmt command. Navigate to the appropriate languagedirectory and execute the following command:%>msgfmt messages.poExecuting this command produces a file named messages.mo, which is what gettext will ultimatelyuse for the translations.Like xgettext, msgfmt also offers a number of features through options. Execute msgfmt --help tolearn more about what’s available.Step 6: Set the Desired Language Within Your ScriptsTo begin taking advantage of your localized strings, all you need to do is set the locale using setlocale()and call the bindtextdomain() and textdomain() functions as demonstrated in Listing 23-1. The endresult is the ability to use the same code source to present your web site in multiple languages. Forinstance, Figures 23-1 and 23-2 depict the same form, the first with the locale set to en_US and the secondwith the locale set to it_IT.Figure 23-1. A newsletter subscription form with English promptsFigure 23-2. The same subscription form, this time in ItalianOf course, there’s more to maintaining translations than what is demonstrated here. For instance,you’ll need to know how to merge and update .po files as the web site’s content changes over time.Gettext offers a variety of utilities for doing exactly this; consult the gettext documentation for moredetails.While gettext is great for maintaining applications in multiple languages, it still doesn’t satisfy theneed to localize other data such as numbers and dates. This is the subject of the next section.■ Tip If your web site offers material in a number of languages, perhaps the most efficient way to allow a user toset a language is to store the locale string in a session variable, and then pass that variable into setlocale()when each page is loaded. See Chapter 18 for more information about <strong>PHP</strong>’s session-handling capabilities.454

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

Saved successfully!

Ooh no, something went wrong!