setlocale

(PHP 3, PHP 4, PHP 5)

setlocale -- Set locale information

Description

string setlocale ( int category, string locale [, string ...] )

string setlocale ( int category, array locale )

category is a named constant (or string) specifying the category of the functions affected by the locale setting:

  • LC_ALL for all of the below

  • LC_COLLATE for string comparison, see strcoll()

  • LC_CTYPE for character classification and conversion, for example strtoupper()

  • LC_MONETARY for localeconv()

  • LC_NUMERIC for decimal separator (See also localeconv())

  • LC_TIME for date and time formatting with strftime()

注: As of PHP 4.2.0, passing category as a string is deprecated, use the above constants instead. Passing them as a string (within quotes) will result in a warning message.

If locale is NULL or the empty string "", the locale names will be set from the values of environment variables with the same names as the above categories, or from "LANG".

If locale is "0", the locale setting is not affected, only the current setting is returned.

If locale is an array or followed by additional parameters then each array element or parameter is tried to be set as new locale until success. This is useful if a locale is known under different names on different systems or for providing a fallback for a possibly not available locale.

注: Passing multiple locales is not available before PHP 4.3.0

Setlocale returns the new current locale, or FALSE if the locale functionality is not implemented on your platform, the specified locale does not exist or the category name is invalid. An invalid category name also causes a warning message. Category/locale names can be found in RFC 1766 and ISO 639. Different systems have different naming schemes for locales.

注: The return value of setlocale() depends on the system that PHP is running. It returns exactly what the system setlocale function returns.

警告

The locale information is maintained per process, not per thread. If you are running PHP on a multithreaded server api like IIS or Apache on Windows you may experience sudden changes of locale settings while a script is running although the script itself never called setlocale() itself. This happens due to other scripts running in different threads of the same process at the same time changing the processwide locale using setlocale().

提示: Windows users will find useful information about locale strings at Microsoft's MSDNwebsite. Supported language strings can be found at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_language_strings.asp and supported country/region strings at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_country_strings.asp. Windows systems support the three letter codes for country/region specified by ISO 3166-Alpha-3, which can be found at this Unicode website .

例子 1. setlocale() Examples

<?php
/* Set locale to Dutch */
setlocale(LC_ALL, 'nl_NL');

/* Output: vrijdag 22 december 1978 */
echo strftime("%A %e %B %Y", mktime(0, 0, 0, 12, 22, 1978));

/* try different possible locale names for german as of PHP 4.3.0 */
$loc_de = setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'de', 'ge');
echo
"Preferred locale for german on this system is '$loc_de'";
?>

例子 2. setlocale() Examples for Windows

<?php
/* Set locale to Dutch */
setlocale(LC_ALL, 'nld_nld');

/* Output: vrijdag 22 december 1978 */
echo strftime("%A %d %B %Y", mktime(0, 0, 0, 12, 22, 1978));

/* try different possible locale names for german as of PHP 4.3.0 */
$loc_de = setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'deu_deu');
echo
"Preferred locale for german on this system is '$loc_de'";
?>


add a note add a note User Contributed Notes
Clayton Smith
27-Sep-2006 02:15
If you already have all the locales installed and "locale -a" is only showing a few languages, then edit /etc/locale.gen and add a line, e.g., es_MX ISO-8859-1.  After you add the line, run the command locale-gen for it to generate the locales based on those settings.
grmela at removethis ent cz
07-Sep-2006 05:58
Just in case you freshly installed some new locales and they doesn't seem to work with setlocale(), don't forget to restart your webserver. It may save you some time browsing your code trying to fix it there :)
Sven K
08-Jun-2006 06:08
If your system doesn't show any installed locales by "locale -a", try installing them by "dpkg-reconfigure locales" (on debian).
beastie
06-Jun-2006 06:12
Note: "Different systems have different naming schemes for locales. The return value of setlocale() depends on the system that PHP is running. It returns exactly what the system setlocale function returns."

There are many other systems than Linux. It is nothing wrong with FreeBSD locale settings. All you have to know is the / your operating system ... ;-)
dr dot tarr at drtarr dot it
27-Mar-2006 11:55
FYI: setlocale() function works on linux only if  you use --enable-libgcc parameter in configure script.

I'm experience this in more than one Debian distro (sarge and woody).
jobcello at gmail dot com
11-Mar-2006 04:37
Apparently on FreeBSD locale support is not so great.

There can be problems setting the locale. For example,
<?
setlocale
(LC_TIME,"pt_BR");
?>
at first didn't work for me.

Command to list all locales on the system...

locale -a

...which showed (in my case) that my needed locale was present, but with the charset appended to the locale name. ( ie: pt_BR.ISO8859-1, pt_BR.UTF-8 )

These locales are usually in folders at /usr/share/locale/

What I did to resolve the problem was create a symbolic link:

ln -s /usr/share/locale/pt_BR.ISO8859-1 /usr/share/locale/pt_BR

And it worked. There will probably be problems with pages with different encodings, so perhaps there's a better way, but if not, hope this is a help to someone.
09-Mar-2006 04:17
The example from bruno dot cenou at revues dot org below shows the possibility, but I want to spell it out: you can add charset info to setlocale.

Example:

Into my utf-8-encoded page I want to insert the name of the current month, which happens to be March, in German "Mrz" - with umlaut. If you use

   setlocale(LC_TIME, 'de_DE');
   echo strftime("%B");

this will return "M&auml;rz", but that html-entity will look like this on a utf-8 page: "M?rz". Not what I want.

But if you use

   setlocale(LC_TIME, 'de_DE.UTF8');  // note the charset info !
   echo strftime("%B");

this returns "Mrz", which, on utf-8, looks like it should: "Mrz".
sadike at gmail dot com
03-Mar-2006 07:00
For people who setlocale don't work on thier server, you can use the language name instead of its code.

<?php
setlocale
('LC_TIME', 'french');
?>
bruno dot cenou at revues dot org
20-Feb-2006 10:31
A little function to test available locales on a sytem :

<?php
function list_system_locales(){
  
ob_start();
  
system('locale -a');
  
$str = ob_get_contents();
  
ob_end_clean();
   return
split("\\n", trim($str));
}

$locale = "fr_FR.UTF8";
$locales = list_system_locales();

if(
in_array($locale, $locales)){
       echo
"yes yes yes....";
}else{
       echo
"no no no.......";
}

?>
Edwin Martin
20-Feb-2006 08:29
Debian users: Addition to Gabor Deri's note: if setlocale doesn't work in your locale and you're on Debian, and Gabor Deri's note doesn't work, you have to install the locales package.

As root, type: "apt-get install locales" and it will be installed.
glenn at europlan dot no
30-Nov-2005 03:55
In most Unix/Linux system, you could use:

locale -a

This will list all available locales on the server.
birkholz at web dot de
15-Aug-2005 11:42
When i tried to get the current locale (e.g. after i set the lang to german with setlocale(LC_ALL, 'de_DE'); ), the following did not work on my suse linux 9.0-box:
$currentLocale = setlocale(LC_ALL, NULL);
This code did a reset to the server-setting.

$currentLocale = setlocale(LC_ALL, 0); works perfectly for me, but the manual says NULL and 0 are equal in this case, but NULL seems to act like "".
cdblog at gmail dot com
26-May-2005 01:39
When you use Smarty's html_select_date,
you can use setlocale(LC_TIME, string local) in your script before display the template file
then get the local month name,
also, you can copy the "plugins/function.html_select_date.php" to "plugins/function.html_select_date_local.php",
and modify the function name to smarty_function_html_select_date_local(former is smarty_function_html_select_date)

for more examples about this function, you can visit my php blog( http://php.clickz.cn/ ) to find more.

this resource's link is http://php.clickz.cn/articles/date/setlocale.html

example test:
<?php
setlocale
(LC_TIME,"chs");

$smarty->display("setlocale.tpl");

?>

the setlocale.tpl's content
-------------------------
<html>
<head>My PHP Blog</head>
<body>
{ html_select_date_local prefix="" time=$smarty.get.date month_format="%B" all_extra="onChange='theFormSubmit();'" end_year=+1 }
</body>
</html>
pigmeu at pigmeu dot net
19-Oct-2004 04:42
!!WARNING!!

The "locale" always depend on the server configuration.

i.e.:
When trying to use "pt_BR" on some servers you will ALWAYS get false. Even with other languages.

The locale string need to be supported by the server. Sometimes there are diferents charsets for a language, like "pt_BR.utf-8" and "pt_BR.iso-8859-1", but there is no support for a _standard_ "pt_BR".

This problem occours in Windows platform too. Here you need to call "portuguese" or "spanish" or "german" or...

Maybe the only way to try to get success calling the function setlocale() is:
setlocale(LC_ALL, "pt_BR", "pt_BR.iso-8859-1", "pt_BR.utf-8", "portuguese", ...);

But NEVER trust on that when making functions like date conversions or number formating. The best way to make sure you are doing the right thing, is using the default "en_US" or "en_UK", by not calling the setlocale() function. Or, make sure that your server support the lang you want to use, with some tests.

Remember that: Using the default locale setings is the best way to "talk" with other applications, like dbs or rpc servers, too.

[]s

Pigmeu
dv at josheli dot com
13-Aug-2004 07:04
On Novell Netware, the language codes require hyphens, not underscores, and using anything other than LC_ALL doesn't work directly.

So... (from their support list)....

You have to set TIME, NUMERIC etc. info in two steps as given below rather than one. This is due to the limitation of setlocale function of LibC.
<?php
   setlocale
(LC_ALL, 'es-ES');
  
$loc = setlocale(LC_TIME, NULL);
   echo
strftime("%A %e %B %Y", mktime(0, 0, 0, 12, 22, 1978));
 
// jeuves 22 diciembre 1978
?>
This should work.

or of course, reset LC_ALL...
<?php
setlocale
(LC_ALL, 'es-ES');
echo
strftime("%A %e %B %Y", mktime(0, 0, 0, 12, 22, 1978));
setlocale(LC_ALL, '');
// jeuves 22 diciembre 1978
?>
bogdan at iHost dot ro
03-Mar-2004 06:53
On some systems (at least FreeBSD 4.x) the format for a `locale' is, for example, ro_RO.ISO8859-2. If you use ro_RO instead setlocale will return FALSE. Just browse in /usr/share/locale and see what is the name of the directory holding your `locale' and use that name in your scripts:

<?php
  clearstatcache
();
 
$pos = strrpos ($_SERVER["PHP_SELF"], "/");
 
$fisier = substr ($_SERVER["PHP_SELF"], $pos + 1);
 
$result = filemtime ($fisier);
 
$local = setlocale (LC_TIME, 'ro_RO.ISO8859-2');
  if (
$local == "ro_RO.ISO8859-2") {
  
$modtime = strftime '%e&nbsp;%B&nbsp;%Y&nbsp;%H:%M', $result);
  } else {
  
$modtime = strftime ('%d.%m.%Y&nbsp;%H:%M', $result);
  }
 
printf ("Ultima&nbsp;actualizare: %s\\n", $modtime);
?>
mk at totu dot com
26-Jan-2004 08:59
Be carefull - setting a locale which uses commas instead of dots in numbers may cause a mysql db not to understand the query:
<?php
setlocale
(LC_ALL,"pl");
$price = 1234 / 100; // now the price looks like 12,34
$query = mysql_query("SELECT Id FROM table WHERE price='".$price."'");
?>
Even if there is a price 12.34 - nothing will be found
r dot nospam dot velseboer at quicknet dot nospam dot nl
09-Sep-2002 10:02
be careful with the LC_ALL setting, as it may introduce some unwanted conversions. For example, I used

setlocale (LC_ALL, "Dutch");

to get my weekdays in dutch on the page. From that moment on (as I found out many hours later) my floating point values from MYSQL where interpreted as integers because the Dutch locale wants a comma (,) instead of a point (.) before the decimals. I tried printf, number_format, floatval.... all to no avail. 1.50 was always printed as 1.00 :(

When I set my locale to :

 setlocale (LC_TIME, "Dutch");

my weekdays are good now and my floating point values too.

I hope I can save some people the trouble of figuring this out by themselves.

Rob
Gabor Deri <gaborDOTderi at fuggetlen dot hu>
07-Aug-2002 12:31
IMPORTANT notice for DEBIAN linux users:

after the upgrade to the new stable (woody), you may have encountered that setlocale is not working at all, even though you have the files in the /usr/share/locale directory and everything was fine before the upgrade. in this case look at the /etc/locale.gen file, which contains the generated locales. if it is empty, you do not have any useful locales. to generate the needed locales run 'dpkg-reconfigure locales' as root and select the locales you need.
jorg-spamm at omnimedia dot no
04-Jul-2002 02:23
I needed to compile and install some extra locales to get this to work on RH7.3. Probably just me not doing a proper installation, but this is what it took to fix it:

localedef -ci no_NO -f ISO_8859-1 no_NO
elindset at hoved dot net
13-May-2002 05:59
In FreeBSD I had to use no_NO.ISO8859-1 instead of just no_NO..

<?PHP
   setlocale
(LC_ALL, 'no_NO.ISO8859-1');
   echo
strftime ("%A %e %B %Y", time());
?>
27-Jul-2001 06:34
Warning ! As specified in the man page, "The locale state is common to all threads within a process". This may be a limitation for PHP, because setlocale() stores this state within global variables shared by all threads instead of allocating a thread-level safe variable. If PHP runs in SAPI mode with enabled support for mulyithreading, this will break some PHP paradygmes... On some platforms only will the locale be set in thread-safe mode. Test your PHP installation in SAPI mode (integrated with a multithreaded web-server) by running concurrent scripts on the same web server, each of them setting a different locale and sleeping for a while before retreiving the current locale. This limitation does not applied to PHP running with a CGI interface (i.e. autonomous process).
misc dot anders at feder dot dk
19-Jun-2001 09:13
Under FreeBSD, locale definitions are stored in the /usr/share/locale/ directory. Danish time formats and weekdays, for instance, are stored in /usr/share/locale/da_DK.ISO_8859-1/LC_TIME.
Fabian dot Rodriguez at toxik dot com
15-Dec-2000 11:19
Language information is defined by this RFC:
RFC1766 - Tags for the Identification of Languages
 - http://www.ietf.org/rfc/rfc1766.txt

And the 2 letter codes come from:
ISO639: "Code for the representation of names of languages"
 - http://www.w3.org/WAI/ER/IG/ert/iso639.htm
ISO3166-1: "English country names and code elements"
 - http://www.din.de/gremien/nas/nabd/iso3166ma/codlstp1/index.html

Also note that the language name can be used in full (ex: setlocale('LC_TIME','swedish'); ) . This full name comes from the latest mentioned file.
noog at libero dot it
24-Nov-2000 07:13
On windows:
Control Panel->International Settings
You can set your locale and customize it
And locale-related PHP functions work perfectly
Morgan Christiansson &lt;mog at linux dot nu&gt;
29-Mar-2000 04:56
check /usr/share/locale/ if you want more info about the locale available with your *NIX box

there is also a file called /usr/share/locale/locale.alias with a list of aliases
such as swedish for sv_SE

so on all boxes i have accounts on (rh 6.0 and slack 3.4) you can just use setlocale("LC_ALL","swedish"); or other prefered language in plain english.

However, the weekdays were in all lowercase :(

Note: export LC_ALL=swedish made a lot of programs swedish for me, it's also possible to make them russian or japanese :)
Lucas Thompson <lucas at slf dot cx>
31-Jan-2000 06:57
The Open Group has an excellent document available on the setlocale() library function, most of which applies to the PHP function of the same name.

http://www.opengroup.org/onlinepubs/7908799/xbd/locale.html

WARNING: This document might be a little too complex for people who came from HTML to PHP.

If you migrated from the world of C programming you'll be a locale master after reading this document.