[英]PHP - Function gettext() not work
I have setted everything correctly and with all the possible variants but gettext never work, below my settings, and here the details: 我已经正确设置了所有内容,并设置了所有可能的变体,但在设置下以及此处的详细信息中,gettext永远无法工作。
I inserted the files it_IT.mo, it_IT.po, it.mo, it.po, domain.mo, domain.po
(where domain is my domain) into the following folders: 我将文件
it_IT.mo, it_IT.po, it.mo, it.po, domain.mo, domain.po
(其中domain是我的域)插入以下文件夹中:
My code is this: 我的代码是这样的:
putenv('LANGUAGE=it'); //Variants: it_IT - it_IT.UTF-8- it.UTF-8
putenv('LC_ALL=it'); //Variants: it_IT - it_IT.UTF-8- it.UTF-8
putenv('LANG=it'); //Variants: it_IT - it_IT.UTF-8- it.UTF-8
define('PROJECT_DIR', realpath('./')); //Variants: without this constant
define('LOCALE_DIR', "../lang"); //Variants: without this constant
define('DEFAULT_LOCALE', 'it_IT'); //Variants: without this constant
$results = setlocale(LC_ALL, array('it_IT.UTF-8', 'it.UTF-8', 'it')); //Return 'it'
$results = bindtextdomain($domain, "../lang"); //It return the correct path C:\xampp\htdocs\sbphp\sb\lang - Variants: Locale - locale
$results = bind_textdomain_codeset($domain, 'UTF-8');
$results = textdomain($domain);
return gettext($string);
And never work. 从不工作。
setlocale();
returns "it", If I set wrong values returns false
. 返回“ it”,如果设置错误,则返回
false
。
bindtextdomain();
Returns the correct path C:\\xampp\\htdocs\\sbphp\\sb\\lang , If I set wrong values it returns nothing. 返回正确的路径C:\\ xampp \\ htdocs \\ sbphp \\ sb \\ lang ,如果我设置了错误的值, 则不返回任何内容。
bind_textdomain_codeset
Returns UTF-8 . bind_textdomain_codeset
返回UTF-8 。
bind_textdomain_codeset()
returns my correct domain. bind_textdomain_codeset()
返回我正确的域。 So I think there is not any error here. 因此,我认为这里没有任何错误。 The output is the original (not translated) text.
输出为原始(未翻译)文本。
I found the solution, use this library https://github.com/oscarotero/Gettext solved the problem immediately. 我找到了解决方案,使用此库https://github.com/oscarotero/Gettext立即解决了问题。
Installation 安装
require("gettext/autoloader.php");
use Gettext\Translations;
$translations_file;
Usage 用法
function translate($string,$lang = "en_EN") {
global $translations_file;
if (!isset($translations_file)) {
$translations_file = Translations::fromPoFile('../your-path/' . $lang . ".po");
}
$translation = $translations_file->find(null, $string);
$result = $translation->getTranslation();
if ($result != "") {
return $result;
} else {
return $string;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.