gettext不翻译所有utf-8字符

时间:2012-07-28 22:00:08

标签: php gettext multilingual

test.php文件回应了一些希腊字母进行测试

<head>
<meta charset = "UTF-8" />
</head>
<?php
$language = "en_US";
$lang_path = "language";
putenv("LC_ALL=$language");
setlocale(LC_ALL, $language);
bindtextdomain("lang", $lang_path);
bind_textdomain_codeset("lang","UTF-8");
textdomain("lang");
echo gettext("α");
echo gettext("β");
echo gettext("γ");
echo gettext("δ");
echo gettext("ε");
echo gettext("ζ");
?>

我的文件夹和文件的结构类似于

 language/en_US/LC_MESSAGES/lang.mo and lang.po

mo文件已正确编译,必须转换为abcdez。在poedit我到处都使用过utf-8。代码产生αβcδεζ,所以只翻译字母c!我重新启动了我的服务器,我的系统中安装了en_US编码,我无法理解为什么只翻译了一个字母..

1 个答案:

答案 0 :(得分:0)

我写了一个简单的实现,阅读zend和cakephp代码。它不支持复数,它只是替换单词,因此您不仅可以用一种语言替换它。

test.php文件

<head>
<meta charset = "UTF-8" />
</head>
<?php
    require_once "gettext.php";
    echo __("something");
    echo __("α");
    echo __("β");
    echo __("γ");
    echo __("δ");
    echo __("ε");
    echo __("ζ");
?>

gettext.php文件

<?php
define('APPPATH','/opt/lampp/htdocs/test/');
/* please change APPPATH to an ultimate directory of the language folder */

/**
 * Loads the binary .mo file and returns array of translations
 *
 * @param string $filename Binary .mo file to load
 * @return mixed Array of translations on success or false on failure
 */
    function loadMo($filename) {
        $translations = false;

        // @codingStandardsIgnoreStart
        // Binary files extracted makes non-standard local variables
        if ($data = file_get_contents($filename)) {
            $translations = array();
            $header = substr($data, 0, 20);
            $header = unpack("L1magic/L1version/L1count/L1o_msg/L1o_trn", $header);
            extract($header);

            if ((dechex($magic) == '950412de' || dechex($magic) == 'ffffffff950412de') && $version == 0) {
                for ($n = 0; $n < $count; $n++) {
                    $r = unpack("L1len/L1offs", substr($data, $o_msg + $n * 8, 8));
                    $msgid = substr($data, $r["offs"], $r["len"]);
                    unset($msgid_plural);

                    if (strpos($msgid, "\000")) {
                        list($msgid, $msgid_plural) = explode("\000", $msgid);
                    }
                    $r = unpack("L1len/L1offs", substr($data, $o_trn + $n * 8, 8));
                    $msgstr = substr($data, $r["offs"], $r["len"]);

                    if (strpos($msgstr, "\000")) {
                        $msgstr = explode("\000", $msgstr);
                    }
                    $translations[$msgid] = $msgstr;

                    if (isset($msgid_plural)) {
                        $translations[$msgid_plural] =& $translations[$msgid];
                    }
                }
            }
        }
        // @codingStandardsIgnoreEnd

        return $translations;
    }   

    /*
        Your system folder must be ULTIMATE_PATH/language/$language
        where language is a parametre like en,el,fr or english or whatever
        domain is the name of the mo file without mo extension e.g. default
        You can pass this parameteres with the __ function below
    */
    function translate($singular, $language = null, $domain = null) {
        $directory = APPPATH."language/"; // your path to language folder
        if (strpos($singular, "\r\n") !== false) {
            $singular = str_replace("\r\n", "\n", $singular);
        }

        if (is_null($language) ) { // default el
            $language = "el";
        }

        if (is_null($domain)) {
            $domain = "default";
        }

        $localeDef = $directory . $language;
        $file = $localeDef."/".$domain;
        $translations = null;

                if (is_file($file . '.mo')) {
                    $translations = loadMo($file . '.mo');
                }

        if (!empty($translations[$singular])) {

                $trans = $translations[$singular];

                if (strlen($trans)) {
                    return $trans;
                }
            }

        return $singular;
    }
    /*
    take language (must be in language folder) from $_GET["lang"];
    */
    function __($singular) {
        if (!$singular) {
            return;
        }
        if (isset($_GET["lang"]) ) // you can use cookie, session etc
            $translated = translate($singular,$_GET["lang"]);
        else 
            $translated = translate($singular);
        return $translated;
    }
?>

在我的代码中,我有默认语言el,你可以使用GET ['lang']获得语言。 (您可以将其更改为COOKIE,SESSION等。此外,mo文件必须被称为default.mo。(您也可以在代码中更改它。)所以您可以在您的目录中:

test.php
gettext.php
language/el/deafult.mo
language/en/default.mo

默认为el / default.mo,您可以在网址中使用?lang = en更改为en。