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.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 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:      * Magic used for validating the format of a MO file as well as
 29:      * detecting if the machine used to create that file was little endian.
 30:      *
 31:      * @var float
 32:      */
 33:     const MO_LITTLE_ENDIAN_MAGIC = 0x950412de;
 34: 
 35:     /**
 36:      * Magic used for validating the format of a MO file as well as
 37:      * detecting if the machine used to create that file was big endian.
 38:      *
 39:      * @var float
 40:      */
 41:     const MO_BIG_ENDIAN_MAGIC = 0xde120495;
 42: 
 43:     /**
 44:      * The size of the header of a MO file in bytes.
 45:      *
 46:      * @var int
 47:      */
 48:     const MO_HEADER_SIZE = 28;
 49: 
 50:     /**
 51:      * Parses machine object (MO) format, independent of the machine's endian it
 52:      * was created on. Both 32bit and 64bit systems are supported.
 53:      *
 54:      * @param resource $resource The file to be parsed.
 55:      *
 56:      * @return array List of messages extracted from the file
 57:      * @throws \RuntimeException If stream content has an invalid format.
 58:      */
 59:     public function parse($resource)
 60:     {
 61:         $stream = fopen($resource, 'rb');
 62: 
 63:         $stat = fstat($stream);
 64: 
 65:         if ($stat['size'] < self::MO_HEADER_SIZE) {
 66:             throw new RuntimeException('Invalid format for MO translations file');
 67:         }
 68:         $magic = unpack('V1', fread($stream, 4));
 69:         $magic = hexdec(substr(dechex(current($magic)), -8));
 70: 
 71:         if ($magic == self::MO_LITTLE_ENDIAN_MAGIC) {
 72:             $isBigEndian = false;
 73:         } elseif ($magic == self::MO_BIG_ENDIAN_MAGIC) {
 74:             $isBigEndian = true;
 75:         } else {
 76:             throw new RuntimeException('Invalid format for MO translations file');
 77:         }
 78: 
 79:         // offset formatRevision
 80:         fread($stream, 4);
 81: 
 82:         $count = $this->_readLong($stream, $isBigEndian);
 83:         $offsetId = $this->_readLong($stream, $isBigEndian);
 84:         $offsetTranslated = $this->_readLong($stream, $isBigEndian);
 85: 
 86:         // Offset to start of translations
 87:         fread($stream, 8);
 88:         $messages = [];
 89: 
 90:         for ($i = 0; $i < $count; $i++) {
 91:             $pluralId = null;
 92:             $context = null;
 93:             $plurals = null;
 94: 
 95:             fseek($stream, $offsetId + $i * 8);
 96: 
 97:             $length = $this->_readLong($stream, $isBigEndian);
 98:             $offset = $this->_readLong($stream, $isBigEndian);
 99: 
100:             if ($length < 1) {
101:                 continue;
102:             }
103: 
104:             fseek($stream, $offset);
105:             $singularId = fread($stream, $length);
106: 
107:             if (strpos($singularId, "\x04") !== false) {
108:                 list($context, $singularId) = explode("\x04", $singularId);
109:             }
110: 
111:             if (strpos($singularId, "\000") !== false) {
112:                 list($singularId, $pluralId) = explode("\000", $singularId);
113:             }
114: 
115:             fseek($stream, $offsetTranslated + $i * 8);
116:             $length = $this->_readLong($stream, $isBigEndian);
117:             $offset = $this->_readLong($stream, $isBigEndian);
118:             fseek($stream, $offset);
119:             $translated = fread($stream, $length);
120: 
121:             if ($pluralId !== null || strpos($translated, "\000") !== false) {
122:                 $translated = explode("\000", $translated);
123:                 $plurals = $pluralId !== null ? array_map('stripcslashes', $translated) : null;
124:                 $translated = $translated[0];
125:             }
126: 
127:             $singular = stripcslashes($translated);
128:             if ($context !== null) {
129:                 $messages[$singularId]['_context'][$context] = $singular;
130:                 if ($pluralId !== null) {
131:                     $messages[$pluralId]['_context'][$context] = $plurals;
132:                 }
133:                 continue;
134:             }
135: 
136:             $messages[$singularId]['_context'][''] = $singular;
137:             if ($pluralId !== null) {
138:                 $messages[$pluralId]['_context'][''] = $plurals;
139:             }
140:         }
141: 
142:         fclose($stream);
143: 
144:         return $messages;
145:     }
146: 
147:     /**
148:      * Reads an unsigned long from stream respecting endianess.
149:      *
150:      * @param resource $stream The File being read.
151:      * @param bool $isBigEndian Whether or not the current platform is Big Endian
152:      * @return int
153:      */
154:     protected function _readLong($stream, $isBigEndian)
155:     {
156:         $result = unpack($isBigEndian ? 'N1' : 'V1', fread($stream, 4));
157:         $result = current($result);
158: 
159:         return (int)substr($result, -8);
160:     }
161: }
162: 
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