CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.7 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.7
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • MoFileParser
  • PoFileParser
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * For full copyright and license information, please see the LICENSE.txt
  8:  * Redistributions of files must retain the above copyright notice.
  9:  *
 10:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 11:  * @link          https://cakephp.org CakePHP(tm) Project
 12:  * @since         3.0.0
 13:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 14:  */
 15: namespace Cake\I18n\Parser;
 16: 
 17: use RuntimeException;
 18: 
 19: /**
 20:  * Parses file in PO format
 21:  *
 22:  * @copyright Copyright (c) 2010, Union of RAD http://union-of-rad.org (http://lithify.me/)
 23:  * @copyright Copyright (c) 2014, Fabien Potencier https://github.com/symfony/Translation/blob/master/LICENSE
 24:  */
 25: class MoFileParser
 26: {
 27: 
 28:     /**
 29:      * Magic used for validating the format of a MO file as well as
 30:      * detecting if the machine used to create that file was little endian.
 31:      *
 32:      * @var float
 33:      */
 34:     const MO_LITTLE_ENDIAN_MAGIC = 0x950412de;
 35: 
 36:     /**
 37:      * Magic used for validating the format of a MO file as well as
 38:      * detecting if the machine used to create that file was big endian.
 39:      *
 40:      * @var float
 41:      */
 42:     const MO_BIG_ENDIAN_MAGIC = 0xde120495;
 43: 
 44:     /**
 45:      * The size of the header of a MO file in bytes.
 46:      *
 47:      * @var int
 48:      */
 49:     const MO_HEADER_SIZE = 28;
 50: 
 51:     /**
 52:      * Parses machine object (MO) format, independent of the machine's endian it
 53:      * was created on. Both 32bit and 64bit systems are supported.
 54:      *
 55:      * @param resource $resource The file to be parsed.
 56:      *
 57:      * @return array List of messages extracted from the file
 58:      * @throws \RuntimeException If stream content has an invalid format.
 59:      */
 60:     public function parse($resource)
 61:     {
 62:         $stream = fopen($resource, 'rb');
 63: 
 64:         $stat = fstat($stream);
 65: 
 66:         if ($stat['size'] < self::MO_HEADER_SIZE) {
 67:             throw new RuntimeException('Invalid format for MO translations file');
 68:         }
 69:         $magic = unpack('V1', fread($stream, 4));
 70:         $magic = hexdec(substr(dechex(current($magic)), -8));
 71: 
 72:         if ($magic == self::MO_LITTLE_ENDIAN_MAGIC) {
 73:             $isBigEndian = false;
 74:         } elseif ($magic == self::MO_BIG_ENDIAN_MAGIC) {
 75:             $isBigEndian = true;
 76:         } else {
 77:             throw new RuntimeException('Invalid format for MO translations file');
 78:         }
 79: 
 80:         // offset formatRevision
 81:         fread($stream, 4);
 82: 
 83:         $count = $this->_readLong($stream, $isBigEndian);
 84:         $offsetId = $this->_readLong($stream, $isBigEndian);
 85:         $offsetTranslated = $this->_readLong($stream, $isBigEndian);
 86: 
 87:         // Offset to start of translations
 88:         fread($stream, 8);
 89:         $messages = [];
 90: 
 91:         for ($i = 0; $i < $count; $i++) {
 92:             $pluralId = null;
 93:             $context = null;
 94:             $plurals = null;
 95: 
 96:             fseek($stream, $offsetId + $i * 8);
 97: 
 98:             $length = $this->_readLong($stream, $isBigEndian);
 99:             $offset = $this->_readLong($stream, $isBigEndian);
100: 
101:             if ($length < 1) {
102:                 continue;
103:             }
104: 
105:             fseek($stream, $offset);
106:             $singularId = fread($stream, $length);
107: 
108:             if (strpos($singularId, "\x04") !== false) {
109:                 list($context, $singularId) = explode("\x04", $singularId);
110:             }
111: 
112:             if (strpos($singularId, "\000") !== false) {
113:                 list($singularId, $pluralId) = explode("\000", $singularId);
114:             }
115: 
116:             fseek($stream, $offsetTranslated + $i * 8);
117:             $length = $this->_readLong($stream, $isBigEndian);
118:             $offset = $this->_readLong($stream, $isBigEndian);
119:             fseek($stream, $offset);
120:             $translated = fread($stream, $length);
121: 
122:             if ($pluralId !== null || strpos($translated, "\000") !== false) {
123:                 $translated = explode("\000", $translated);
124:                 $plurals = $pluralId !== null ? array_map('stripcslashes', $translated) : null;
125:                 $translated = $translated[0];
126:             }
127: 
128:             $singular = stripcslashes($translated);
129:             if ($context !== null) {
130:                 $messages[$singularId]['_context'][$context] = $singular;
131:                 if ($pluralId !== null) {
132:                     $messages[$pluralId]['_context'][$context] = $plurals;
133:                 }
134:                 continue;
135:             }
136: 
137:             $messages[$singularId]['_context'][''] = $singular;
138:             if ($pluralId !== null) {
139:                 $messages[$pluralId]['_context'][''] = $plurals;
140:             }
141:         }
142: 
143:         fclose($stream);
144: 
145:         return $messages;
146:     }
147: 
148:     /**
149:      * Reads an unsigned long from stream respecting endianess.
150:      *
151:      * @param resource $stream The File being read.
152:      * @param bool $isBigEndian Whether or not the current platform is Big Endian
153:      * @return int
154:      */
155:     protected function _readLong($stream, $isBigEndian)
156:     {
157:         $result = unpack($isBigEndian ? 'N1' : 'V1', fread($stream, 4));
158:         $result = current($result);
159: 
160:         return (int)substr($result, -8);
161:     }
162: }
163: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs