setlocale() - 设置地区信息 - php 字符串函数
setlocale()
(PHP 4, PHP 5, PHP 7)
设置地区信息
说明
setlocale(int $category, string $locale[,string $...] ): stringsetlocale(int $category, array $locale) : string设置地区信息。
参数
$category
$category命名常量指定的受区域设置的功能类别:
LC_ALL
所有的设置LC_COLLATE
字符串比较,详见strcoll()LC_CTYPE
字符串的分类与转换,参见例子strtoupper()LC_MONETARY
等同localeconv()LC_NUMERIC
对于小数点的分隔(另请参见localeconv())LC_TIME
时间与格式strftime()LC_MESSAGES
系统响应(如果PHP使用libintl编译)
If$localeisNULL
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$localeis"0", the locale setting is not affected, only the current setting is returned.
If$localeis 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:在Windows中,setlocale(LC_ALL,'')要从系统中的区域/语言设置(通过控制面板访问)。
返回值
Returns the new current locale,orFALSE
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 1766and» ISO 639. Different systems have different naming schemes for locales.
Note:The return value ofsetlocale()depends on the system that PHP is running. It returns exactly what the systemsetlocalefunction returns.
更新日志
版本 | 说明 |
---|---|
5.3.0 | This function now throws anE_DEPRECATED notice if a string is passed to the$categoryparameter instead of one of theLC_*constants. |
范例
Example #1setlocale()Examples
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'.
Setting locale that is not supported by your system will result in some string operations returning a question mark "?" in your strings where it needs to perform transliteration. 1) Always check the return of setlocale() to ensure it has set to something supported 2) on Linux you can use the "locale -a" command to find a list of supported locales
In *some* Windows systems, setting LC_TIME only will not work, you must either set LC_ALL or both LC_CTYPE and LC_TIME. BUT if you have already set LC_TIME using setlocale earlier in the script, dates will not be affected! For example: will not work, while will do the job.
Instead, using php with IIS, I had to use this line for Italian language...
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: The above certainly applies to Windows systems, but should also apply to Unix if the required support is installed. Cheers, Bryn.
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ä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".
In addition to russ, about getting / backing up the locale: I'm using this in unit-tests. I wanted to test something based on locale, and reset the locale after the tests were done. Yet there were some errors; * setlocale doesn't like strings anymore. You need to use constants. * Some contants don't exist anymore. Here's an updated piece of code:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!