Как можно разобрать gettext .mo-файлы в PHP4, не полагаясь на setlocale / locales?

Я сделал пару связанных потоков, но это единственный прямой вопрос, на который я ищу ответ. Мои рамки будут использовать Zend_Translate если версия php равна 5, в противном случае я должен подражать функциональности для 4.

Похоже, что почти всякая реализация gettext опирается на setlocale или локали, я знаю, что в системе существует много несоответствий, поэтому я не хочу полагаться на нее.

Я попытался пару раз получить функции textdomain , bindtextdomain и gettext но мне всегда нужно было вызвать setlocale .

Кстати, все файлы .mo будут UTF-8.

Вот некоторый многоразовый код для анализа файлов MO в PHP на основе Zend_Translate_Adapter_Gettext :

 <?php class MoParser { private $_bigEndian = false; private $_file = false; private $_data = array(); private function _readMOData($bytes) { if ($this->_bigEndian === false) { return unpack('V' . $bytes, fread($this->_file, 4 * $bytes)); } else { return unpack('N' . $bytes, fread($this->_file, 4 * $bytes)); } } public function loadTranslationData($filename, $locale) { $this->_data = array(); $this->_bigEndian = false; $this->_file = @fopen($filename, 'rb'); if (!$this->_file) throw new Exception('Error opening translation file \'' . $filename . '\'.'); if (@filesize($filename) < 10) throw new Exception('\'' . $filename . '\' is not a gettext file'); // get Endian $input = $this->_readMOData(1); if (strtolower(substr(dechex($input[1]), -8)) == "950412de") { $this->_bigEndian = false; } else if (strtolower(substr(dechex($input[1]), -8)) == "de120495") { $this->_bigEndian = true; } else { throw new Exception('\'' . $filename . '\' is not a gettext file'); } // read revision - not supported for now $input = $this->_readMOData(1); // number of bytes $input = $this->_readMOData(1); $total = $input[1]; // number of original strings $input = $this->_readMOData(1); $OOffset = $input[1]; // number of translation strings $input = $this->_readMOData(1); $TOffset = $input[1]; // fill the original table fseek($this->_file, $OOffset); $origtemp = $this->_readMOData(2 * $total); fseek($this->_file, $TOffset); $transtemp = $this->_readMOData(2 * $total); for($count = 0; $count < $total; ++$count) { if ($origtemp[$count * 2 + 1] != 0) { fseek($this->_file, $origtemp[$count * 2 + 2]); $original = @fread($this->_file, $origtemp[$count * 2 + 1]); $original = explode("\0", $original); } else { $original[0] = ''; } if ($transtemp[$count * 2 + 1] != 0) { fseek($this->_file, $transtemp[$count * 2 + 2]); $translate = fread($this->_file, $transtemp[$count * 2 + 1]); $translate = explode("\0", $translate); if ((count($original) > 1) && (count($translate) > 1)) { $this->_data[$locale][$original[0]] = $translate; array_shift($original); foreach ($original as $orig) { $this->_data[$locale][$orig] = ''; } } else { $this->_data[$locale][$original[0]] = $translate[0]; } } } $this->_data[$locale][''] = trim($this->_data[$locale]['']); unset($this->_data[$locale]['']); return $this->_data; } } с <?php class MoParser { private $_bigEndian = false; private $_file = false; private $_data = array(); private function _readMOData($bytes) { if ($this->_bigEndian === false) { return unpack('V' . $bytes, fread($this->_file, 4 * $bytes)); } else { return unpack('N' . $bytes, fread($this->_file, 4 * $bytes)); } } public function loadTranslationData($filename, $locale) { $this->_data = array(); $this->_bigEndian = false; $this->_file = @fopen($filename, 'rb'); if (!$this->_file) throw new Exception('Error opening translation file \'' . $filename . '\'.'); if (@filesize($filename) < 10) throw new Exception('\'' . $filename . '\' is not a gettext file'); // get Endian $input = $this->_readMOData(1); if (strtolower(substr(dechex($input[1]), -8)) == "950412de") { $this->_bigEndian = false; } else if (strtolower(substr(dechex($input[1]), -8)) == "de120495") { $this->_bigEndian = true; } else { throw new Exception('\'' . $filename . '\' is not a gettext file'); } // read revision - not supported for now $input = $this->_readMOData(1); // number of bytes $input = $this->_readMOData(1); $total = $input[1]; // number of original strings $input = $this->_readMOData(1); $OOffset = $input[1]; // number of translation strings $input = $this->_readMOData(1); $TOffset = $input[1]; // fill the original table fseek($this->_file, $OOffset); $origtemp = $this->_readMOData(2 * $total); fseek($this->_file, $TOffset); $transtemp = $this->_readMOData(2 * $total); for($count = 0; $count < $total; ++$count) { if ($origtemp[$count * 2 + 1] != 0) { fseek($this->_file, $origtemp[$count * 2 + 2]); $original = @fread($this->_file, $origtemp[$count * 2 + 1]); $original = explode("\0", $original); } else { $original[0] = ''; } if ($transtemp[$count * 2 + 1] != 0) { fseek($this->_file, $transtemp[$count * 2 + 2]); $translate = fread($this->_file, $transtemp[$count * 2 + 1]); $translate = explode("\0", $translate); if ((count($original) > 1) && (count($translate) > 1)) { $this->_data[$locale][$original[0]] = $translate; array_shift($original); foreach ($original as $orig) { $this->_data[$locale][$orig] = ''; } } else { $this->_data[$locale][$original[0]] = $translate[0]; } } } $this->_data[$locale][''] = trim($this->_data[$locale]['']); unset($this->_data[$locale]['']); return $this->_data; } } 

Хорошо, я в основном закончил тем, что написал парсер для mo-файлов, основанный на Gettext Adapter от Zend, насколько я знаю, gettext в значительной степени зависит от локали, поэтому ручное разборка файла .mo избавит вас от неудобства запутаться в странных обстоятельствах с проблемами локали с setlocale . Я также планирую разбор данных Zend Locale, представленных в виде xml-файлов.