我在阅读 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。