Overview

Packages

  • Jyxo_Beholder
  • Jyxo_Charset
  • Jyxo_Color
  • Jyxo_Css
  • Jyxo_ErrorHandling
  • Jyxo_FirePhp
  • Jyxo_Gettext
    • Parser
  • Jyxo_Html
  • Jyxo_Input
    • Chain
    • Filter
    • Validator
  • Jyxo_Mail
    • Email
    • Parser
    • Sender
  • Jyxo_Rpc
    • Json
    • Xml
  • Jyxo_Shell
  • Jyxo_SpamFilter
  • Jyxo_Spl
  • Jyxo_String
  • Jyxo_Svn
  • Jyxo_Time
  • Jyxo_Timer
  • Jyxo_Webdav
  • Jyxo_XmlReader
  • PHP

Classes

  • Jyxo_Gettext_Parser
  • Jyxo_Gettext_Parser_Header
  • Jyxo_Gettext_Parser_Item

Exceptions

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