PHP- 获取当前语言环境

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29932843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:40:13  来源:igfitidea点击:

PHP- Get current locale

php

提问by JROB

I am setting the locale of the website using this function:

我正在使用此功能设置网站的语言环境:

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

When someone visits the site, they have the ability to change their locale. What I am trying to do is somewhere else in the site retrieve what the current locale is.

当有人访问该站点时,他们可以更改其语言环境。我想要做的是在站点的其他地方检索当前的语言环境。

How would I do this?

我该怎么做?

回答by Jerry Saravia

You can call setlocale like so, and it'll return the current local.

你可以像这样调用 setlocale,它会返回当前的本地。

$currentLocale = setlocale(LC_ALL, 0);
echo $currentLocale; //outputs C/en_US.UTF-8/C/C/C/C on my machine

Here is documentation from php.net as commented by @JROB

这是@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.

语言环境

如果语言环境为“0”,则语言环境设置不受影响,仅返回当前设置。

如果 locale 为 NULL 或空字符串 "",则将根据与上述类别名称相同的环境变量的值或 "LANG" 来设置区域设置名称。

如果 locale 是一个数组或后跟附加参数,则尝试将每个数组元素或参数设置为新的 locale,直到成功。如果区域设置在不同系统上以不同的名称已知,或者为可能不可用的区域设置提供后备,则这很有用。

回答by PHPst

Another answer after two years!

两年后的另一个答案!

You can simply user Locale::getDefault()or locale_get_default()to get the current locale.

您可以简单地使用Locale::getDefault()locale_get_default()获取当前语言环境。

http://php.net/manual/en/locale.getdefault.php

http://php.net/manual/en/locale.getdefault.php

Intl Localeclass is the modern alternative for old functions. Once you started using it, you need to update the local by php.net/manual/en/locale.setdefault.php function

IntlLocale类是旧功能的现代替代品。一旦你开始使用它,你需要通过 php.net/manual/en/locale.setdefault.php 函数更新本地