简体   繁体   English

PHP-函数gettext()不起作用

[英]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永远无法工作。

  1. Tested on both Windows with XAMPP and Linux. 在装有XAMPP和Linux的Windows上进行了测试。
  2. Gettext is active on php.ini and with WordPress work good always. Gettext在php.ini上处于活动状态,并且与WordPress一起始终运行良好。
  3. I confirm that the .mo and .po files are correct due work good with WordPress, created with Poedit. 我确认.mo和.po文件正确无误,可与Poedit创建的WordPress正常工作。
  4. 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是我的域)插入以下文件夹中:

    • C:\\xampp\\htdocs\\sbphp\\sb\\lang C:\\ xampp \\ htdocs \\ sbphp \\ sb \\ lang
    • C:\\xampp\\htdocs\\sbphp\\sb\\lang\\it C:\\ xampp \\ htdocs \\ sbphp \\ sb \\ lang \\ it
    • C:\\xampp\\htdocs\\sbphp\\sb\\lang\\it_IT C:\\ xampp \\ htdocs \\ sbphp \\ sb \\ lang \\ it_IT
    • C:\\xampp\\htdocs\\sbphp\\sb\\lang\\locale\\LC_MESSAGES\\it C:\\ xampp \\ htdocs \\ sbphp \\ sb \\ lang \\ locale \\ LC_MESSAGES \\ it
    • C:\\xampp\\htdocs\\sbphp\\sb\\lang\\locale\\LC_MESSAGES\\it_IT C:\\ xampp \\ htdocs \\ sbphp \\ sb \\ lang \\ locale \\ LC_MESSAGES \\ it_IT
    • C:\\xampp\\htdocs\\sbphp\\sb\\lang\\locale\\it C:\\ xampp \\ htdocs \\ sbphp \\ sb \\ lang \\ locale \\ it
    • C:\\xampp\\htdocs\\sbphp\\sb\\lang\\locale\\it_IT C:\\ xampp \\ htdocs \\ sbphp \\ sb \\ lang \\ locale \\ it_IT
    • C:\\xampp\\htdocs\\sbphp\\sb\\lang\\LC_MESSAGES\\it C:\\ xampp \\ htdocs \\ sbphp \\ sb \\ lang \\ LC_MESSAGES \\ it
    • C:\\xampp\\htdocs\\sbphp\\sb\\lang\\LC_MESSAGES\\it_IT C:\\ xampp \\ htdocs \\ sbphp \\ sb \\ lang \\ LC_MESSAGES \\ it_IT

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. 从不工作。

My Problem: 我的问题:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM