Exemplo n.º 1
0
 /**
  * Load translations from a given file.
  *
  * @param string $domain the domain to load the data into
  * @param string $file the file name
  * @return boolean TRUE if data was successfully loaded, FALSE otherwise
  */
 private static function load_file($domain, $file)
 {
     if (!file_exists($file)) {
         Error::raise(sprintf(_t('No translations found for locale %s, domain %s!'), self::$locale, $domain));
         return FALSE;
     }
     if (filesize($file) < 24) {
         Error::raise(sprintf(_t('Invalid .MO file for locale %s, domain %s!'), self::$locale, $domain));
         return FALSE;
     }
     $fp = fopen($file, 'rb');
     $data = fread($fp, filesize($file));
     fclose($fp);
     // determine endianness
     $little_endian = TRUE;
     list(, $magic) = unpack('V1', substr($data, 0, 4));
     switch ($magic & 0xffffffff) {
         case (int) 0x950412de:
             $little_endian = TRUE;
             break;
         case (int) 0xde120495:
             $little_endian = FALSE;
             break;
         default:
             Error::raise(sprintf(_t('Invalid magic number 0x%08x in %s!'), $magic, $file));
             return FALSE;
     }
     $revision = substr($data, 4, 4);
     if ($revision != 0) {
         Error::raise(sprintf(_t('Unknown revision number %d in %s!'), $revision, $file));
         return FALSE;
     }
     $l = $little_endian ? 'V' : 'N';
     if ($data && strlen($data) >= 20) {
         $header = substr($data, 8, 12);
         $header = unpack("{$l}1msgcount/{$l}1msgblock/{$l}1transblock", $header);
         if ($header['msgblock'] + ($header['msgcount'] - 1) * 8 > filesize($file)) {
             Error::raise(sprintf(_t('Message count (%d) out of bounds in %s!'), $header['msgcount'], $file));
             return FALSE;
         }
         $lo = "{$l}1length/{$l}1offset";
         for ($msgindex = 0; $msgindex < $header['msgcount']; $msgindex++) {
             $msginfo = unpack($lo, substr($data, $header['msgblock'] + $msgindex * 8, 8));
             $msgids = explode("", substr($data, $msginfo['offset'], $msginfo['length']));
             $transinfo = unpack($lo, substr($data, $header['transblock'] + $msgindex * 8, 8));
             $transids = explode("", substr($data, $transinfo['offset'], $transinfo['length']));
             self::$messages[$domain][$msgids[0]] = array($msgids, $transids);
         }
     }
     // setup plural functionality
     self::$plural_function = self::get_plural_function(self::$messages[$domain][''][1][0]);
     // only use locale if we actually read something
     return count(self::$messages) > 0;
 }