1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19: 20: 21: 22: 23:
24: class Jyxo_Gettext_Parser_Item
25: {
26: 27: 28: 29: 30:
31: protected $msgid = '';
32:
33: 34: 35: 36: 37: 38: 39: 40:
41: protected $msgstr = '';
42:
43: 44: 45: 46: 47:
48: protected $plural = '';
49:
50: 51: 52: 53: 54:
55: protected $location = '';
56:
57: 58: 59: 60: 61:
62: protected $fuzzy = false;
63:
64: 65: 66: 67: 68:
69: protected $obsolete = false;
70:
71:
72: 73: 74: 75: 76: 77: 78: 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: 92: 93: 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: 138: 139: 140:
141: public function isFuzzy()
142: {
143: return $this->fuzzy;
144: }
145:
146: 147: 148: 149: 150:
151: public function isObsolete()
152: {
153: return $this->obsolete;
154: }
155:
156: 157: 158: 159: 160:
161: public function hasPlural()
162: {
163: return !empty($this->plural);
164: }
165:
166: 167: 168: 169: 170:
171: public function getLocation()
172: {
173: return $this->location;
174: }
175:
176: 177: 178: 179: 180:
181: public function getMsgid()
182: {
183: return $this->msgid;
184: }
185:
186: 187: 188: 189: 190:
191: public function getMsgstr()
192: {
193: return $this->msgstr;
194: }
195:
196: 197: 198: 199: 200:
201: public function getPlural()
202: {
203: return $this->plural;
204: }
205: }
206: