|
|
 |
setlocale (PHP 3, PHP 4, PHP 5) setlocale -- Set locale information Descriptionstring setlocale ( mixed category, string locale [, string ...] ) string setlocale ( mixed 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()
Note:
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.
Note:
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.
Note:
The return value of setlocale() depends
on the system that PHP is running. It returns exactly
what the system setlocale function returns.
Example 1. setlocale() Examples |
<?php
setlocale(LC_ALL, 'nl_NL');
echo strftime("%A %e %B %Y", mktime(0, 0, 0, 12, 22, 1978));
$loc_de = setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'de', 'ge');
echo "Preferred locale for german on this system is '$loc_de'";
?>
|
|
Example 2. setlocale() Examples for Windows |
<?php
setlocale(LC_ALL, 'nld_nld');
echo strftime("%A %d %B %Y", mktime(0, 0, 0, 12, 22, 1978));
$loc_de = setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'deu_deu');
echo "Preferred locale for german on this system is '$loc_de'";
?>
|
|
User Contributed Notes
setlocale
anonIMouS :)
18-Jan-2005 03:03
For checking available locales possible to use this scripts
<?php
system('locale -a'); ?>
For russian locale in most cases universal will
<?php
setlocale (LC_ALL, array ('ru_RU.CP1251', 'rus_RUS.1251'));
?>
03-Jan-2005 11:29
after run 'dpkg-reconfigure locales' restart apache!
wais_bg at yahoo dot com
20-Dec-2004 02:08
NetPanther at gmx dot net
05-Dec-2004 05:12
string setlocale ( mixed category, string locale [, string ...])
In many cases I experienced that setlocale() does not work with date(), no matter which value you use as 'string locale':
<?php
setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'de-DE', 'de', 'ge', 'de_DE.ISO_8859-1', 'German');
echo date(F);
?>
Instead you might want to use an array in order to store locale month-names, but in most cases using strftime() works fine, as well:
<?php
setlocale(LC_ALL, "de_DE");
echo strftime("%B");
?>
k at ailis dot de
14-Nov-2004 08:23
Note that there is also a constant named LC_MESSAGES on platforms supporting it. Useful if you want to build i18n support based on this locale category.
pigmeu at pigmeu dot net
18-Oct-2004 03: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
pixeline
03-Oct-2004 06:20
To set the time in French, i tried:
'fr_FR',
'fr'
'FR'
'french'
'fra'
'france'
'French'
'fr_FR.ISO8859-1'
on FreeBSD, adding .8859-1 after 'fr_FR' finally solved it
the end is the beginning is the end... ;-)
pix.l|ne
Alexandre Andrade
24-Sep-2004 07:53
For Portuguese (Portugal) i use this code
if (!setlocale (LC_ALL, 'pt_PT.ISO8859-1')) {
setlocale(LC_ALL,'portuguese');
}
It works fine in FreeBSD and Windows
dv at josheli dot com
12-Aug-2004 06: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));
?>
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, '');
?>
j_arbues at terra dot es
23-May-2004 08:39
Well, for spanish locales, I use es_ES.ISO_8859-1 for FreeBSD and just es_ES for Red Hat.
MagicalTux at FF.st
06-Apr-2004 11:37
Be careful when using LC_ALL or LC_NUMERIC ! For example if you want to pass a numeric value (example : 18.4) in a GET/POST form, you'll probably use :
<input type="text" name="numeric" value="<?=floatval($num);?>" />
If you use locale fr_FR at this time, $_GET['numeric'] will have value string(18,4) ... if you convert it back to float without re-doing a setlocale, PHP will truncate the value and you'll get only 18 ...
And the main problem can be if you do cross-sites data transfert. Example : I have a website that uses locale from the user settings. If the user have a French locale, I'll get something like 18,4 . If I pass that to paypal (pratical example) it will not work. So the best is to always set LC_NUMERIC to "C" (posix).
Little example code :
<?php
$val=18.4; echo 'Initial: '.$val."\n";
setlocale(LC_NUMERIC,'fr_FR');
$val=strval($val); echo 'French: '.$val."\n";
setlocale(LC_NUMERIC,'C'); $val=floatval($val); echo 'Bad convertion: '.$val."\n"; ?>
Output :
Initial: 18.4
French: 18,4
Bad convertion: 18
zyxwvu at users dot sourceforge dot net
02-Apr-2004 02:55
If you're a Windows PHP programmer and you want to use ISO encoding, not default windows-125x, you have to use one of these codepages:
28591 gives iso-8859-1
28592 gives iso-8859-2
28593 gives iso-8859-3
etc.
28605 gives iso-8895-15
Example:
// Set to polish ISO-8859-2
setlocale(LC_ALL, 'Polish_Poland.28592');
Of course you have to activate required codepages in the Start -> Control Panel -> Internationalization -> Advanced, and be sure that your editor generates documents in the iso encoding :).
bogdan at iHost dot ro
02-Mar-2004 04: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 %B %Y %H:%M', $result);
} else {
$modtime = strftime ('%d.%m.%Y %H:%M', $result);
}
printf ("Ultima actualizare: %s\\n", $modtime);
?>
Melvin D. Nava
20-Feb-2004 07:24
Get the Current Date in Spanish Format
setlocale (LC_TIME,"spanish");
$long_date = str_replace("De","de",ucwords(strftime("%A, %d de %B de %Y")));
Is that easy!!
mk at totu dot com
26-Jan-2004 06: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; $query = mysql_query("SELECT Id FROM table WHERE price='".$price."'");
?>
Even if there is a price 12.34 - nothing will be found
mbirth at webwriters dot de
23-Jan-2004 10:00
I noticed that PHP sometimes doesn't switch the locale as it should. Although I used <?php setlocale(LC_TIME, 'German'); ?>, I sometimes got e.g. "Monday" instead of "Montag" for the day of week. So I now set the locale using the following do ... while:
<?php
$locale = 'de_DE.ISO8859-1';
do {
$locale_result = setlocale(LC_TIME, $locale);
} while ($locale_result != $locale);
?>
kommissar at zonapowers dot cl
13-Jan-2004 03:37
Just for taking property about the language; write the name of your language where is es_CL (Chile). But I am now fighting with the PHP script trying to get the chilean time, and not the server time.
<?php
setlocale (LC_TIME,"es_CL");
$date = strftime("%A %d de %B de %Y");
echo $date;
?>
Adam Rice
01-Dec-2003 11:13
OS X 10.2 does not have complete locale support: navigating to /usr/share/locale shows all the locale directories one might expect, but these don't contain all the same files as other flavors of Unix.
I haven't figured out where to get the necessary resources, or how to install them, but if I do, I'll post that here.
Not sure if this has been fixed in 10.3.
thomas at hannibalsoftware dot dk
22-Nov-2003 07:34
In hope to help other danes (who else would need our lang.code?!), here is the language-code for FreeBSD:
setlocale (LC_ALL, 'da_DK.ISO8859-1');
I got the info from a user of RedHat so I guess that it works with that too!
/Hannibal
Duke Ionescu <duke at mastre dot com>
12-Aug-2003 04:16
BTW, the standard US locale is:
en_US.iso885915
Just in case you need to set it on a system that has it set to POSIX (in which case PHP returns "C") and either you don't or can't change it system-wide.
stefan DO NOT SPAM @ netautor NOPE com
09-Feb-2003 06:33
On a Win98 / Apache 2 / PHP 4.3.0 / Zend Optimizer system works the following : setlocale(LC_ALL,"ge") instead of de_DE as on any other system. why i'm not surprised ;-)
bartolli at plovdiv-news dot com
10-Jan-2003 07:06
For Bulgaria Time Zone:
<?
setlocale (LC_TIME, 'bg_BG');
echo strftime ("%A, %e %B %Y")
?>
output = "Ден Дата Месец Година"
example --> Петък, 10 Януари 2003
ekekec79 at yahoo dot com
02-Jan-2003 01:45
For Turkey time zone, the
setlocale(LC_TIME, 'turkish');
works well.
info at zeltru-design dot de
29-Dec-2002 03:42
On Windows XP (maybe W2K too) set it to "German", and on an Unixserver it work with "de".
Primary I was confused, cause everyone say it should be "de_DE".
No matter, the settings above works perfektly.
r dot nospam dot velseboer at quicknet dot nospam dot nl
08-Sep-2002 09: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>
06-Aug-2002 11: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.
nospam at please dot com
18-Jul-2002 10:22
My server runs freebsd 4.6-stable, apache2 + php4.2.1 module
The only way to get locale settings working was to define the 'package' directly:
setlocale (LC_ALL, 'nl_NL.ISO8859-1');
I saw that no alias file existed in /usr/share/locale maybe it could be defined
I hope this helps some people :)
jorg-spamm at omnimedia dot no
03-Jul-2002 01: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
12-May-2002 04: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 05: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 08: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
14-Dec-2000 09:20
BTW, not all locales are always supported on every server. Ask you ISP/network admin to see if they are.
Fabian dot Rodriguez at toxik dot com
14-Dec-2000 09:19
noog at libero dot it
23-Nov-2000 05:13
On windows:
Control Panel->International Settings
You can set your locale and customize it
And locale-related PHP functions work perfectly
Morgan Christiansson <mog at linux dot nu>
29-Mar-2000 02: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 04: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.
Michal Fita <manveru at witrynka dot pl>
11-Jan-1999 01:41
It is written nowhere, that setlocale() can be used for simple mathematical operations like adding, substracting, dividing or multiplying. The code below gives correct results:
setlocale(LC_ALL, "pl");
$net = "1234,56";
$gross = "1,22" * $net;
printf("gross: %s, net: %s", $gross, $net");
/* Polish VAT tax is 22% */
Result of this code is:
gross: 1234,56, net: 1506,1632
So, every one can use his own decimal point for mathematical operations without any error or warning (PHP3.0.3).
| |