我正在使用此功能设置网站的语言环境:
function set_locale($locale) { // ie. en, es
$language = $locale;
putenv("LANG=".$language);
setlocale(LC_ALL, $language);
$domain = "phd";
bindtextdomain($domain, "locale");
bind_textdomain_codeset($domain, 'UTF-8');
textdomain($domain);
} // end set_locale
当有人访问该站点时,他们可以更改其语言环境。我想要做的是在网站的其他地方检索当前的语言环境。
我该怎么做?
最佳答案
你可以像这样调用setlocale,它会返回当前本地。
$currentLocale = setlocale(LC_ALL, 0);
echo $currentLocale; //outputs C/en_US.UTF-8/C/C/C/C on my machine
这是@JROB评论的来自php.net的文档
locale
If locale is "0", the locale setting is not affected, only the current setting is returned.
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 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.
关于PHP-获取当前语言环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29932843/