📄 function.setlocale.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Set locale information</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.rtrim.html">rtrim</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.sha1-file.html">sha1_file</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.setlocale" class="refentry"> <div class="refnamediv"> <h1 class="refname">setlocale</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">setlocale</span> — <span class="dc-title">Set locale information</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><b><b>setlocale</b></b></span> ( <span class="methodparam"><span class="type">int</span> <tt class="parameter">$category</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$locale</tt></span> [, <span class="methodparam"><span class="type">string</span> <tt class="parameter">$...</tt></span> ] )</div> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><b><b>setlocale</b></b></span> ( <span class="methodparam"><span class="type">int</span> <tt class="parameter">$category</tt></span> , <span class="methodparam"><span class="type">array</span> <tt class="parameter">$locale</tt></span> )</div> <p class="para rdfs-comment"> Sets locale information. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">category</tt></i></span> <dd> <p class="para"> <i><tt class="parameter">category</tt></i> is a named constant specifying the category of the functions affected by the locale setting: <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> LC_ALL for all of the below </span> </li> <li class="listitem"> <span class="simpara"> LC_COLLATE for string comparison, see <a href="function.strcoll.html" class="function">strcoll()</a> </span> </li> <li class="listitem"> <span class="simpara"> LC_CTYPE for character classification and conversion, for example <a href="function.strtoupper.html" class="function">strtoupper()</a> </span> </li> <li class="listitem"> <span class="simpara"> LC_MONETARY for <a href="function.localeconv.html" class="function">localeconv()</a> </span> </li> <li class="listitem"> <span class="simpara"> LC_NUMERIC for decimal separator (See also <a href="function.localeconv.html" class="function">localeconv()</a>) </span> </li> <li class="listitem"> <span class="simpara"> LC_TIME for date and time formatting with <a href="function.strftime.html" class="function">strftime()</a> </span> </li> <li class="listitem"> <span class="simpara"> LC_MESSAGES for system responses (available if PHP was compiled with <i>libintl</i>) </span> </li> </ul> </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">locale</tt></i></span> <dd> <p class="para"> If <i><tt class="parameter">locale</tt></i> is <b><tt>NULL</tt></b> or the empty string <i>""</i>, the locale names will be set from the values of environment variables with the same names as the above categories, or from "LANG". </p> <p class="para"> If <i><tt class="parameter">locale</tt></i> is <i>"0"</i>, the locale setting is not affected, only the current setting is returned. </p> <p class="para"> If <i><tt class="parameter">locale</tt></i> 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. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">...</tt></i></span> <dd> <p class="para"> </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the new current locale, or <b><tt>FALSE</tt></b> if the locale functionality is not implemented on your platform, the specified locale does not exist or the category name is invalid. </p> <p class="para"> An invalid category name also causes a warning message. Category/locale names can be found in <a href="http://www.faqs.org/rfcs/rfc1766" class="link external">» RFC 1766</a> and <a href="http://www.w3.org/WAI/ER/IG/ert/iso639.htm" class="link external">» ISO 639</a>. Different systems have different naming schemes for locales. </p> <blockquote><p><b class="note">Note</b>: The return value of <b>setlocale()</b> depends on the system that PHP is running. It returns exactly what the system setlocale function returns. <br /> </p></blockquote> </div> <div class="refsect1 changelog"> <h3 class="title">ChangeLog</h3> <p class="para"> <table class="informaltable"> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Version</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">5.3.0</td> <td colspan="1" rowspan="1" align="left"> This function now throws an E_DEPRECATED notice if a string is passed to the <i><tt class="parameter">category</tt></i> parameter instead of one of the <i>LC_*</i> constants. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.3.0</td> <td colspan="1" rowspan="1" align="left"> Passing multiple locales became possible. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.2.0</td> <td colspan="1" rowspan="1" align="left"> Passing <i><tt class="parameter">category</tt></i> as a string is now deprecated, use the above constants instead. Passing them as a string (within quotes) will result in a warning message. </td> </tr> </tbody> </colgroup> </table> </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>setlocale()</b> Examples</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/* Set locale to Dutch */<br /></span><span style="color: #0000BB">setlocale</span><span style="color: #007700">(</span><span style="color: #0000BB">LC_ALL</span><span style="color: #007700">, </span><span style="color: #DD0000">'nl_NL'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Output: vrijdag 22 december 1978 */<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">strftime</span><span style="color: #007700">(</span><span style="color: #DD0000">"%A %e %B %Y"</span><span style="color: #007700">, </span><span style="color: #0000BB">mktime</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">12</span><span style="color: #007700">, </span><span style="color: #0000BB">22</span><span style="color: #007700">, </span><span style="color: #0000BB">1978</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">/* try different possible locale names for german as of PHP 4.3.0 */<br /></span><span style="color: #0000BB">$loc_de </span><span style="color: #007700">= </span><span style="color: #0000BB">setlocale</span><span style="color: #007700">(</span><span style="color: #0000BB">LC_ALL</span><span style="color: #007700">, </span><span style="color: #DD0000">'de_DE@euro'</span><span style="color: #007700">, </span><span style="color: #DD0000">'de_DE'</span><span style="color: #007700">, </span><span style="color: #DD0000">'de'</span><span style="color: #007700">, </span><span style="color: #DD0000">'ge'</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"Preferred locale for german on this system is '$loc_de'"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #2 <b>setlocale()</b> Examples for Windows</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/* Set locale to Dutch */<br /></span><span style="color: #0000BB">setlocale</span><span style="color: #007700">(</span><span style="color: #0000BB">LC_ALL</span><span style="color: #007700">, </span><span style="color: #DD0000">'nld_nld'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Output: vrijdag 22 december 1978 */<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">strftime</span><span style="color: #007700">(</span><span style="color: #DD0000">"%A %d %B %Y"</span><span style="color: #007700">, </span><span style="color: #0000BB">mktime</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">12</span><span style="color: #007700">, </span><span style="color: #0000BB">22</span><span style="color: #007700">, </span><span style="color: #0000BB">1978</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">/* try different possible locale names for german as of PHP 4.3.0 */<br /></span><span style="color: #0000BB">$loc_de </span><span style="color: #007700">= </span><span style="color: #0000BB">setlocale</span><span style="color: #007700">(</span><span style="color: #0000BB">LC_ALL</span><span style="color: #007700">, </span><span style="color: #DD0000">'de_DE@euro'</span><span style="color: #007700">, </span><span style="color: #DD0000">'de_DE'</span><span style="color: #007700">, </span><span style="color: #DD0000">'deu_deu'</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"Preferred locale for german on this system is '$loc_de'"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div> <div class="refsect1 notes"> <h3 class="title">Notes</h3> <div class="warning"><b class="warning">Warning</b> <p class="para"> 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 <b>setlocale()</b> itself. This happens due to other scripts running in different threads of the same process at the same time changing the processwide locale using <b>setlocale()</b>. </p> </div> <div class="tip"><b class="tip">Tip</b> <p class="para"> Windows users will find useful information about <i><tt class="parameter">locale</tt></i> strings at Microsoft's <acronym title="Microsoft Developer Network">MSDN</acronym> website. Supported language strings can be found at <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_language_strings.asp" class="link external">» http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_language_strings.asp</a> and supported country/region strings at <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_country_strings.asp" class="link external">» http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_country_strings.asp</a>. Windows systems support the three letter codes for country/region specified by <i>ISO 3166-Alpha-3</i>, which can be found at this <a href="http://www.unicode.org/onlinedat/countries.html" class="link external">» Unicode website</a>. </p> </div> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.rtrim.html">rtrim</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.sha1-file.html">sha1_file</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -