0

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 编码,但我不明白为什么只翻译一个字母..

4

1 回答 1

0

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

测试.php 文件

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

获取文本.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,您可以在 url 中使用 ?lang=en 更改为 en。

于 2012-08-09T09:11:54.233 回答