百科狗-知识改变命运!
--

setlocale() - 设置地区信息 - php 字符串函数

乐乐1年前 (2023-11-21)阅读数 23#技术干货
文章标签字符串

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编译)
$locale

If$localeisNULLor 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,orFALSEif the locale functionality is not implemented on your platform, the specified locale does not exist or the category name is invalid.

setlocale() - 设置地区信息 - php 字符串函数

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.0This function now throws anE_DEPRECATEDnotice 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

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)