PHP-获取当前语言环境

标签 php

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

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/

相关文章:

php - MYSQLi 不返回任何行,即使它具有有效的 SQL 和 PHP 语法

php - 如何在 Elasticsearch 中获取搜索历史记录?

php - 无法使用 AFHTTPRequestOperation post 方法将希伯来文文本插入 mysql 数据库

php - 安装 Composer 时出现语法错误?

php - 如何从闭包中终止 Eloquent 分块

php - 使用 PHP 重新创建类似于 VistaPrint/CafePress/Zazzle 的内容。有任何想法吗?

php - SQL 复杂连接

ubuntu 中的 PHP 扩展路径

php - 如何压缩文件夹并使用php下载它?

php if else 条件不工作