16.2. Using a Particular Locale
Problem
You want to tell PHP to use the settings of a particular locale.
Solution
Call setlocale( )
with the appropriate category and locale.
Here’s how to use the es_US
(U.S.
Spanish) locale for all categories:
setlocale(LC_ALL,'es_US');
Here’s how to use the de_AT
(Austrian German) locale for time and date formatting:
setlocale(LC_TIME,'de_AT');
Discussion
To find the current locale without changing it, call
setlocale( )
with a NULL
locale:
print setlocale(LC_ALL,NULL);
en_US
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');
On Windows, to change the locale, visit the Control Panel. In the Regional Options section, you can pick a new locale and customize its settings.
See Also
Section 16.4 shows how to set a default
locale; documentation on setlocale( )
at
http://www.php.net/setlocale.
Get PHP Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.