Recipe 19.2. Using a Particular Locale


19.2.1. Problem

You want PHP to use the settings of a particular locale.

19.2.2. Solution

Call setlocale( ) with the appropriate category and locale. Here's how to use the es_MX (Mexican Spanish) locale for all categories:

setlocale(LC_ALL,'es_MX');

Here's how to use the de_AT (Austrian German) locale for time and date formatting:

setlocale(LC_TIME,'de_AT');

On Windows, the equivalent locale names are Spanish_Mexico and German_Austria.

19.2.3. Discussion

To find the current locale without changing it, call setlocale( ) with 0 for the locale, as in Example 19-2.

Getting the current locale

<?php print setlocale(LC_ALL,0); ?>

Many systems also support a set of aliases for common locales, listed in a file such as /usr/share/locale/locale.alias. This file is a series of lines including:

russian         ru_RU.ISO-8859-5 slovak          sk_SK.ISO-8859-2 slovene         sl_SI.ISO-8859-2 slovenian       sl_SI.ISO-8859-2 spanish         es_ES.ISO-8859-1 swedish         sv_SE.ISO-8859-1

The first column of each line is an alias; the second column shows the locale and character set the alias points to. You can use the alias in calls to setlocale( ) instead of the corresponding string the alias points to. For example, you can do setlocale(LC_ALL,'swedish'); instead of setlocale(LC_ALL,'sv_SE.ISO-8859-1');.

Call setlocale( ) to change the locale on Windows as well. As described in 19.1, however, the locale names are different. If PHP is running in a multithreaded environment, changing the locale can have unexpected results. A call to setlocale( ) changes the locale for all threads in the current process. This means that when one thread changes the locale, it is immediately changed in other threads' running scripts. If you need to use setlocale( ), consider using a single-threaded server setup.

19.2.4. See Also

19.3 shows how to set a default locale; documentation on setlocale( ) at http://www.php.net/setlocale.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net