Overview

Namespaces

  • Jyxo
    • Beholder
      • TestCase
    • Gettext
      • Parser
    • Input
      • Chain
      • Filter
      • Validator
    • Mail
      • Email
        • Attachment
      • Parser
      • Sender
    • Rpc
      • Json
      • Xml
    • Shell
    • Spl
    • Svn
    • Time
    • Webdav
  • PHP

Classes

  • Header
  • Item

Exceptions

  • Exception
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * Jyxo PHP Library
  5:  *
  6:  * LICENSE
  7:  *
  8:  * This source file is subject to the new BSD license that is bundled
  9:  * with this package in the file license.txt.
 10:  * It is also available through the world-wide-web at this URL:
 11:  * https://github.com/jyxo/php/blob/master/license.txt
 12:  */
 13: 
 14: namespace Jyxo\Gettext\Parser;
 15: 
 16: /**
 17:  * Container class for translation properties.
 18:  *
 19:  * @category Jyxo
 20:  * @package Jyxo\Gettext
 21:  * @subpackage Parser
 22:  * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
 23:  * @license https://github.com/jyxo/php/blob/master/license.txt
 24:  * @author Matěj Humpál
 25:  */
 26: class Item
 27: {
 28:     /**
 29:      * Msgid.
 30:      *
 31:      * @var string
 32:      */
 33:     protected $msgid = '';
 34: 
 35:     /**
 36:      * Msgid translations.
 37:      *
 38:      * For each plural form one array field.
 39:      * If there is only one translation (no plural forms), it is stored as a string.
 40:      *
 41:      * @var string|array
 42:      */
 43:     protected $msgstr = '';
 44: 
 45:     /**
 46:      * Plural forms.
 47:      *
 48:      * @var string
 49:      */
 50:     protected $plural = '';
 51: 
 52:     /**
 53:      * Msgid position in the source code.
 54:      *
 55:      * @var string
 56:      */
 57:     protected $location = '';
 58: 
 59:     /**
 60:      * Is msgid fuzzy?
 61:      *
 62:      * @var boolean
 63:      */
 64:     protected $fuzzy = false;
 65: 
 66:     /**
 67:      * Is msgid obsolete?
 68:      *
 69:      * @var boolean
 70:      */
 71:     protected $obsolete = false;
 72: 
 73: 
 74:     /**
 75:      * Constructor.
 76:      *
 77:      * Retrieves a fragment of the PO file and parses it.
 78:      *
 79:      * @param string $chunk Translation fragment
 80:      * @throws \Jyxo\Gettext\Parser\Exception If msgid empty
 81:      */
 82:     public function __construct($chunk)
 83:     {
 84:         $array = explode("\n", $chunk);
 85:         $this->parse($array);
 86: 
 87:         if (empty($this->msgid)) {
 88:             throw new Exception('Msgid is empty which is not acceptable');
 89:         }
 90:     }
 91: 
 92:     /**
 93:      * The actual parser.
 94:      *
 95:      * @param array $chunks Lines of the PO file fragment
 96:      */
 97:     protected function parse(array $chunks)
 98:     {
 99: 
100:         foreach ($chunks as $chunk) {
101: 
102:             if (preg_match('/^"(.*)"/', $chunk, $matches)) {
103:                 $this->{$lastChunkType} .= $matches[1];
104:                 continue;
105:             }
106: 
107:             if (preg_match('/^msgid "(.*)"/', $chunk, $matches)) {
108:                 $lastChunkType = 'msgid';
109:                 $this->msgid = $matches[1];
110:             } elseif (preg_match('/^msgstr "(.*)"/', $chunk, $matches)) {
111:                 $lastChunkType = 'msgstr';
112:                 $this->msgstr = $matches[1];
113:             } elseif (preg_match('/^#~ msgid "(.*)"/', $chunk, $matches)) {
114:                 $lastChunkType = 'msgid';
115:                 $this->obsolete = true;
116:                 $this->msgid = $matches[1];
117:             } elseif (preg_match('/^#~ msgstr "(.*)"/', $chunk, $matches)) {
118:                 $lastChunkType = 'msgstr';
119:                 $this->obsolete = true;
120:                 $this->msgstr = $matches[1];
121:             } elseif (preg_match('/^(#: .+)$/', $chunk, $matches)) {
122:                 $lastChunkType = 'location';
123:                 $this->location .= $matches[1];
124:             } elseif (preg_match('/^#, fuzzy/', $chunk)) {
125:                 $lastChunkType = 'fuzzy';
126:                 $this->fuzzy = true;
127:             } elseif (preg_match('/^msgid_plural "(.*)"/', $chunk, $matches)) {
128:                 $lastChunkType = 'plural';
129:                 $this->plural = $matches[1];
130:                 $this->msgstr = array();
131:             } elseif (preg_match('/^msgstr\[([0-9])+\] "(.*)"/', $chunk, $matches)) {
132:                 $lastChunkType = 'msgstr';
133:                 $this->msgstr[$matches[1]] = $matches[2];
134:             }
135:         }
136:     }
137: 
138:     /**
139:      * Returns whether the msgid is fuzzy.
140:      *
141:      * @return boolean
142:      */
143:     public function isFuzzy()
144:     {
145:         return $this->fuzzy;
146:     }
147: 
148:     /**
149:      * Returns whether the msgid is obsolete.
150:      *
151:      * @return boolean
152:      */
153:     public function isObsolete()
154:     {
155:         return $this->obsolete;
156:     }
157: 
158:     /**
159:      * Returns whether the msgid has plural forms.
160:      *
161:      * @return boolean
162:      */
163:     public function hasPlural()
164:     {
165:         return !empty($this->plural);
166:     }
167: 
168:     /**
169:      * Returns msgid's position in source codes.
170:      *
171:      * @return string|array
172:      */
173:     public function getLocation()
174:     {
175:         return $this->location;
176:     }
177: 
178:     /**
179:      * Returns msgid.
180:      *
181:      * @return string|array
182:      */
183:     public function getMsgid()
184:     {
185:         return $this->msgid;
186:     }
187: 
188:     /**
189:      * Returns msgstr.
190:      *
191:      * @return string|array
192:      */
193:     public function getMsgstr()
194:     {
195:         return $this->msgstr;
196:     }
197: 
198:     /**
199:      * Returns plural translations.
200:      *
201:      * @return string
202:      */
203:     public function getPlural()
204:     {
205:         return $this->plural;
206:     }
207: }
208: 
Jyxo PHP Library API documentation generated by ApiGen 2.3.0