PHPのお勉強!

PHP TOP

setlocale

(PHP 4, PHP 5, PHP 7, PHP 8)

setlocaleロケール情報を設定する

説明

setlocale(int $category, string $locales, string ...$rest): string|false

代替のシグネチャ (名前付き引数をサポートしていません):

setlocale(int $category, array $locale_array): string|false

ロケール情報を設定します。

警告

ロケール情報は、スレッド毎ではなくプロセス毎に維持されます。 もし PHP を マルチスレッドサーバーAPI 上で動作させている場合、 スクリプトを実行している間にロケールの設定が突然変わるのを 経験するかも知れませんが、スクリプト自身は決して setlocale() 自身をコールしていません。 これは同時に同一プロセスの異なるスレッドで実行されている他のスクリプトが setlocale() を使用してプロセスワイドなロケールを変更する事により発生します。 Windows では、PHP 7.0.5 以降、 ロケール情報はスレッド単位で維持されるようになっています。

パラメータ

category

categoryは、名前付きの定数(または文字列)であり、 ロケール設定により影響を受ける関数のカテゴリを指定します。

locales

localesが空の文字列 "" の場合、ロケール名は上記のカテゴリと同じ名前の環境変数の値、 または環境変数 "LANG" からセットされます。

locales"0" の場合、 ロケール設定は適用されず、単に現在の設定が返されます。

locales に追加のパラメータが続く場合、 それぞれのパラメータは成功するまで新規ロケールとしてセットされます。 これは、ロケールが異なるシステムで異なる名前を持っている、 もしくはロケールが利用できない場合のフォールバックを提供するといった場合に有用です。

rest

オプションの文字列。ロケール設定が成功するまで設定を試みます。

locale_array

それぞれの配列の要素が、 新しいロケールとして設定が成功するまで設定を試みます。 これは、異なるシステムでロケールが違う名前として認識されたり、 ロケールが利用できない可能性がある場合にフォールバックさせる目的で使うと便利です。

注意:

Windows では、setlocale(LC_ALL, '') を使用するとシステムの 地域と言語の設定の値を使用します (コントロールパネルで確認できます)。

戻り値

現在の新しいロケールを返します。ロケール機能が未実装、 指定されたロケールが存在しない、カテゴリ名が無効などの場合は false を返します。

また、カテゴリ名が無効の場合は警告メッセージが発生します。 カテゴリやロケール名は、 » RFC 1766» ISO 639 にあります。 ロケールの命名方式は、システムによって異なります。

注意:

setlocale() の戻り値は、 PHP が実行されているシステムに依存します。 システムの setlocale 関数が返す値を返すためです。

例1 setlocale() の例

<?php
/* ロケールをオランダ語に設定 */
setlocale(LC_ALL, 'nl_NL');

/* 出力: vrijdag 22 december 1978 */
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'";
?>

例2 Windows での setlocale() の例

<?php
/* ロケールをオランダ語に設定 */
setlocale(LC_ALL, 'nld_nld');

/* 出力: vrijdag 22 december 1978 */
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'";
?>

注意

ヒント

Windows ユーザーは Microsoft の MSDN の Web サイトに locales 文字列に関する有用な情報を見つけることができるでしょう。 サポートしている言語文字列は » 言語文字列のドキュメント、 そしてサポートしている国/地域文字列は » 国/地域文字列のドキュメント にあります。

add a note

User Contributed Notes 34 notes

up
175
r dot nospam dot velseboer at quicknet dot nospam dot nl
22 years ago
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
up
53
russ at eatmymonkeydust dot com
13 years ago
If you are looking for a getlocale() function simply pass 0 (zero) as the second parameter to setlocale().

Beware though if you use the category LC_ALL and some of the locales differ as a string containing all the locales is returned:

<?php
echo setlocale(LC_ALL, 0);

// LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;LC_MESSAGES=C;LC_PAPER=C;LC_NAME=C;
// LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=C;LC_IDENTIFICATION=C

echo setlocale(LC_CTYPE, 0);

// en_US.UTF-8

setlocale(LC_ALL, "en_US.UTF-8");
echo
setlocale(LC_ALL, 0);

// en_US.UTF-8

?>

If you are looking to store and reset the locales you could do something like this:

<?php

$originalLocales
= explode(";", setlocale(LC_ALL, 0));
setlocale(LC_ALL, "nb_NO.utf8");

// Do something

foreach ($originalLocales as $localeSetting) {
if (
strpos($localeSetting, "=") !== false) {
list (
$category, $locale) = explode("=", $localeSetting);
}
else {
$category = LC_ALL;
$locale = $localeSetting;
}
setlocale($category, $locale);
}

?>

The above works here (Ubuntu Linux) but as the setlocale() function is just wrapping the equivalent system calls, your mileage may vary on the result.
up
12
epistomai at gmail dot com
5 years ago
setlocale(LC_MONETARY, 'en_US') doesn't work anymore (at least in PHP Version 7.3.8).

I've used 'en_US.UTF-8' instead
up
16
Shashakhmetov Talgat
9 years ago
//Fix encoding for russian locale on windows
$locale = setlocale(LC_ALL, 'ru_RU.CP1251', 'rus_RUS.CP1251', 'Russian_Russia.1251');

function strftime_fix($format, $locale, $timestamp = time()){
// Fix %e for windows
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
$format = preg_replace('#(?<!%)((?:%%)*)%e#', '\1%#d', $format);
}
// convert
$date_str = strftime($format, $timestamp);
if (stripos($locale, "1251") !== false) {
return iconv("windows-1251","utf-8", $date_str);
} elseif (stripos($locale, "1252") !== false) {
return iconv("windows-1252","utf-8", $date_str);
} else {
return $date_str;
}
}
up
18
Kari Sderholm aka Haprog
15 years ago
It took me a while to figure out how to get a Finnish locale correctly set on Ubuntu Server with Apache2 and PHP5.

At first the output for "locale -a" was this:
C
en_US.utf8
POSIX

I had to install a finnish language pack with
"sudo apt-get install language-pack-fi-base"

Now the output for "locale -a" is:
C
en_US.utf8
fi_FI.utf8
POSIX

The last thing you need to do after installing the correct language pack is restart Apache with "sudo apache2ctl restart". The locale "fi_FI.utf8" can then be used in PHP5 after restarting Apache.

For setting Finnish timezone and locale in PHP use:
<?php
date_default_timezone_set
('Europe/Helsinki');
setlocale(LC_ALL, array('fi_FI.UTF-8','fi_FI@euro','fi_FI','finnish'));
?>
up
14
brice/axice/be
15 years ago
Pay attention to the syntax.
- UTF8 without dash ('-')
- locale.codeset and not locale-codeset.

Stupid newbie error but worth knowing them when starting with gettext.

<?php
$codeset
= "UTF8"; // warning ! not UTF-8 with dash '-'

// for windows compatibility (e.g. xampp) : theses 3 lines are useless for linux systems

putenv('LANG='.$lang.'.'.$codeset);
putenv('LANGUAGE='.$lang.'.'.$codeset);
bind_textdomain_codeset('mydomain', $codeset);

// set locale
bindtextdomain('mydomain', ABSPATH.'/locale/');
setlocale(LC_ALL, $lang.'.'.$codeset);
textdomain('mydomain');
?>

where directory structure of locale is (for example) :
locale/fr_FR/LC_MESSAGES/mydomain.mo
locale/en_US/LC_MESSAGES/mydomain.mo

and ABSPATH is the absolute path to the locale dir

further note, under linux systems, it seems to be necessary to create the locale at os level using 'locale-gen'.
up
1
lingureanumanuel at yahoo dot com
2 years ago
for windows

setlocale(LC_ALL, 'Greenlandic_Greenland.1252');
will return false

to make it work use
setlocale(LC_ALL, 'Kalaallisut_Greenland.1252');
up
9
pigmeu at pigmeu dot net
20 years ago
!!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
up
2
stepdate at gmail dot com
6 years ago
If you have Locales installed and things won't work check the spelling: for German all the comments suggested "setlocale(LC_TIME, "de_DE.utf8")", but it has to be "setlocale(LC_TIME, "de_DE.UTF-8")"-> UTF-8 instead of utf8.
up
8
jose dot nobile at gmail dot com
9 years ago
For Windows users complaining about setlocale.

The locale argument to the setlocale function takes the following form:
setlocale( LC_ALL, "<language>_<country>.<code_page>" );

in short, if you want use for example: es_CO.UTF-8 it must be in Windows: Spanish_Colombia.1252

The code page 1252 is ISO-8859-1 (windows-1252 ANSI Latin 1; Western European (Windows)

Windows use different languages code from Unix, for example, es_CO becomes es-CO or Spanish_Colombia, also it doesn't support UTF-8 charset as is shown in their website: https://msdn.microsoft.com/en-us/library/x99tb11d(v=vs.140).aspx

"The set of available locale names, languages, country/region codes, and code pages includes all those supported by the Windows NLS API except code pages that require more than two bytes per character, such as UTF-7 and UTF-8. If you provide a code page value of UTF-7 or UTF-8, setlocale will fail, returning NULL."

Please check the updated website of language and code pages:

https://msdn.microsoft.com/en-us/library/39cwe7zf(v=vs.140).aspx
and
https://msdn.microsoft.com/en-us//goglobal/bb895996

Here a copy paste in case the link is removed:

Afrikaans
Albanian
Arabic_Saudi_Arabia
Arabic_Iraq
Arabic_Egypt
Arabic_Libya
Arabic_Algeria
Arabic_Morocco
Arabic_Tunisia
Arabic_Oman
Arabic_Yemen
Arabic_Syria
Arabic_Jordan
Arabic_Lebanon
Arabic_Kuwait
Arabic_UAE
Arabic_Bahrain
Arabic_Qatar
Armenian
Azeri_Latin
Azeri_Cyrillic
Basque
Belarusian
Bengali_India
Bosnian_Latin
Bulgarian
Catalan
Chinese_Taiwan
Chinese_PRC
Chinese_Hong_Kong
Chinese_Singapore
Chinese_Macau
Croatian
Croatian_Bosnia_Herzegovina
Czech
Danish
Divehi
Dutch_Standard
Dutch_Belgian
English_United_States
English_United_Kingdom
English_Australian
English_Canadian
English_New_Zealand
English_Ireland
English_South_Africa
English_Jamaica
English_Caribbean
English_Belize
English_Trinidad
English_Zimbabwe
English_Philippines
Estonian
Faeroese
Farsi
Finnish
French_Standard
French_Belgian
French_Canadian
French_Swiss
French_Luxembourg
French_Monaco
Georgian
Galician
German_Standard
German_Swiss
German_Austrian
German_Luxembourg
German_Liechtenstein
Greek
Gujarati
Hebrew
Hindi
Hungarian
Icelandic
Indonesian
Italian_Standard
Italian_Swiss
Japanese
Kannada
Kazakh
Konkani
Korean
Kyrgyz
Latvian
Lithuanian
Macedonian
Malay_Malaysia
Malay_Brunei_Darussalam
Malayalam
Maltese
Maori
Marathi
Mongolian
Norwegian_Bokmal
Norwegian_Nynorsk
Polish
Portuguese_Brazilian
Portuguese_Standard
Punjabi
Quechua_Bolivia
Quechua_Ecuador
Quechua_Peru
Romanian
Russian
Sami_Inari
Sami_Lule_Norway
Sami_Lule_Sweden
Sami_Northern_Finland
Sami_Northern_Norway
Sami_Northern_Sweden
Sami_Skolt
Sami_Southern_Norway
Sami_Southern_Sweden
Sanskrit
Serbian_Latin
Serbian_Latin_Bosnia_Herzegovina
Serbian_Cyrillic
Serbian_Cyrillic_Bosnia_Herzegovina
Slovak
Slovenian
Spanish_Traditional_Sort
Spanish_Mexican
Spanish_Modern_Sort
Spanish_Guatemala
Spanish_Costa_Rica
Spanish_Panama
Spanish_Dominican_Republic
Spanish_Venezuela
Spanish_Colombia
Spanish_Peru
Spanish_Argentina
Spanish_Ecuador
Spanish_Chile
Spanish_Uruguay
Spanish_Paraguay
Spanish_Bolivia
Spanish_El_Salvador
Spanish_Honduras
Spanish_Nicaragua
Spanish_Puerto_Rico
Swahili
Swedish
Swedish_Finland
Syriac
Tamil
Tatar
Telugu
Thai
Tswana
Ukrainian
Turkish
Ukrainian
Urdu
Uzbek_Latin
Uzbek_Cyrillic
Vietnamese
Welsh
Xhosa
Zulu

The code pages identifiers:

https://msdn.microsoft.com/en-us/library/windows/desktop/dd317756(v=vs.85).aspx
up
3
tomas dot hampl at gmail dot com
13 years ago
On Linux, setlocale() depends on the installed locales. To see which locales are available to PHP, run this from the terminal:

"locale -a"

Provided list are all locales that are available on your server for PHP to use. To add a new one, run

locale-gen <locale name> (this may need sudo / root permissions), for example to add a Czech locale, run something like this:

"sudo locale-gen cs_CZ.utf8"

Then you can use this locale declaration:

setlocale(LC_ALL, 'cs_CZ.utf8');
up
5
Anonymous
18 years ago
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 "März" - 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 "M√§rz", which, on utf-8, looks like it should: "März".
up
2
bryn AT lunarvis DOT com
16 years ago
Posting this in the hope it might be useful to others, as I could find very little info anywhere. If you want to use a Welsh locale and have the suitable language support installed, you pass 'cym' (abbreviated form of Cymraeg) to setlocale:

<?php
setlocale
(LC_TIME, 'cym');
$welsh= gmstrftime("%A, %B %Y - %H:%M",time());
echo
$welsh;
?>

The above certainly applies to Windows systems, but should also apply to Unix if the required support is installed.

Cheers,

Bryn.