source-class-TMXResourceBundle

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
Overview

Classes

Interfaces

Exceptions

Functions

  1: <?php
  2: //============================================================+
  3: // File name   : tce_tmx.php
  4: // Begin       : 2004-10-19
  5: // Last Update : 2010-08-09
  6: //
  7: // Description : TMX-PHP Bridge Class
  8: // Platform    : PHP 5
  9: //
 10: // Author: Nicola Asuni
 11: //
 12: // (c) Copyright:
 13: //               Nicola Asuni
 14: //               Tecnick.com LTD
 15: //               www.tecnick.com
 16: //               info@tecnick.com
 17: //
 18: // License:
 19: //    Copyright (C) 2004-2010  Nicola Asuni - Tecnick.com LTD
 20: //
 21: //  This program is free software: you can redistribute it and/or modify
 22: //  it under the terms of the GNU Lesser General Public License as published by
 23: //  the Free Software Foundation, either version 2.1 of the License, or
 24: //  (at your option) any later version.
 25: //
 26: //  This program is distributed in the hope that it will be useful,
 27: //  but WITHOUT ANY WARRANTY; without even the implied warranty of
 28: //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 29: //  GNU Lesser General Public License for more details.
 30: //
 31: //  You should have received a copy of the GNU Lesser General Public License
 32: //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 33: //
 34: //    See LICENSE.TXT file for more information.
 35: //============================================================+
 36: 
 37: /**
 38:  * @file
 39:  * TMX-PHP Bridge Class (TMXResourceBundle).
 40:  * @package com.tecnick.tmxphpbridge
 41:  */
 42: 
 43: /**
 44:  * @class TMXResourceBundle
 45:  * This PHP Class reads resource text data directly from a TMX (XML) file.
 46:  * First, the XMLTMXResourceBundle class instantiates itself with two parameters:
 47:  * a TMX file name and a target language name. Then, using an XML parser, it
 48:  * reads all of a translation unit's properties for the key information and
 49:  * specified language data and populates the resource array with them (key -> value).
 50:  *
 51:  * @package com.tecnick.tmxphpbridge
 52:  * @brief TMX-PHP Bridge Class
 53:  * @author Nicola Asuni [www.tecnick.com]
 54:  * @version 1.1.005
 55:  */
 56: class TMXResourceBundle
 57: {
 58: 
 59:     /**
 60:      * Array used to contain key-translation couples.
 61:      * @private
 62:      */
 63:     private $resource = array();
 64: 
 65:     /**
 66:      * String Current tu -> tuid value.
 67:      * @private
 68:      */
 69:     private $current_key = '';
 70: 
 71:     /**
 72:      * String Current data value.
 73:      * @private
 74:      */
 75:     private $current_data = '';
 76: 
 77:     /**
 78:      * String Current tuv -> xml:lang value.
 79:      * @private
 80:      */
 81:     private $current_language = '';
 82: 
 83:     /**
 84:      * Boolean value true when we are inside a seg element
 85:      * @private
 86:      */
 87:     private $segdata = false;
 88: 
 89:     /**
 90:      * String ISO language identifier (a two- or three-letter code)
 91:      * @private
 92:      */
 93:     private $language = '';
 94: 
 95:     /**
 96:      * String filename for cache
 97:      * @private
 98:      */
 99:     private $cachefile = '';
100: 
101:     /**
102:      * Class constructor.
103:      * @param $tmxfile (string) TMX (XML) file name
104:      * @param $language (string) ISO language identifier (a two- or three-letter code)
105:      * @param $cachefile (string) set filename for cache (leave blank to exclude cache)
106:      */
107:     public function __construct($tmxfile, $language, $cachefile = '')
108:     {
109:         // reset array
110:         $this->resource = array();
111:         // set selecteed language
112:         $this->language = strtoupper($language);
113:         // set filename for cache
114:         $this->cachefile = $cachefile;
115: 
116:         if (file_exists($this->cachefile)) {
117:             // read data from cache
118:             require_once($this->cachefile);
119:             $this->resource = $tmx;
120:         } else {
121:             if (!empty($this->cachefile)) {
122:                 // open cache file
123:                 file_put_contents($this->cachefile, '<'.'?php'."\n".
124:                 '// CACHE FILE FOR LANGUAGE: '.substr($language, 0, 2)."\n".
125:                 '// DATE: '.date('Y-m-d H:i:s')."\n".
126:                 '// *** DELETE THIS FILE TO RELOAD DATA FROM TMX FILE ***'."\n", FILE_APPEND | LOCK_EX);
127:             }
128: 
129:             // creates a new XML parser to be used by the other XML functions
130:             $this->parser = xml_parser_create();
131:             // the following function allows to use parser inside object
132:             xml_set_object($this->parser, $this);
133:             // disable case-folding for this XML parser
134:             xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
135:             // sets the element handler functions for the XML parser
136:             xml_set_element_handler($this->parser, 'startElementHandler', 'endElementHandler');
137:             // sets the character data handler function for the XML parser
138:             xml_set_character_data_handler($this->parser, 'segContentHandler');
139:             // start parsing an XML document
140:             if (!xml_parse($this->parser, file_get_contents($tmxfile))) {
141:                 die(sprintf(
142:                     'ERROR TMXResourceBundle :: XML error: %s at line %d',
143:                     xml_error_string(xml_get_error_code($this->parser)),
144:                     xml_get_current_line_number($this->parser)
145:                 ));
146:             }
147:             // free this XML parser
148:             xml_parser_free($this->parser);
149:             if (!empty($this->cachefile)) {
150:                 // close cache file
151:                 file_put_contents($this->cachefile, "\n\n".'// --- EOF ---', FILE_APPEND);
152:             }
153:         }
154:     }
155: 
156:     /**
157:      * Class destructor; resets $resource array.
158:      */
159:     public function __destruct()
160:     {
161:         $resource = array(); // reset resource array
162:     }
163: 
164:     /**
165:      * Sets the start element handler function for the XML parser parser.start_element_handler.
166:      * @param $parser (resource) The first parameter, parser, is a reference to the XML parser calling the handler.
167:      * @param $name (string) The second parameter, name, contains the name of the element for which this handler is called. If case-folding is in effect for this parser, the element name will be in uppercase letters.
168:      * @param $attribs (array) The third parameter, attribs, contains an associative array with the element's attributes (if any). The keys of this array are the attribute names, the values are the attribute values. Attribute names are case-folded on the same criteria as element names. Attribute values are not case-folded. The original order of the attributes can be retrieved by walking through attribs the normal way, using each(). The first key in the array was the first attribute, and so on.
169:      * @private
170:      */
171:     private function startElementHandler($parser, $name, $attribs)
172:     {
173:         switch (strtolower($name)) {
174:             case 'tu': {
175:                 // translation unit element, unit father of every element to be translated. It can contain a unique identifier (tuid).
176:                 if (array_key_exists('tuid', $attribs)) {
177:                     $this->current_key = $attribs['tuid'];
178:                 }
179:                 break;
180:             }
181:             case 'tuv': {
182:                 // translation unit variant, unit that contains the language code of the translation (xml:lang)
183:                 if (array_key_exists('xml:lang', $attribs)) {
184:                     $this->current_language = strtoupper($attribs['xml:lang']);
185:                 }
186:                 break;
187:             }
188:             case 'seg': {
189:                 // segment, it contains the translated text
190:                 $this->segdata = true;
191:                 $this->current_data = '';
192:                 break;
193:             }
194:             default: {
195:                 break;
196:             }
197:         }
198:     }
199: 
200:     /**
201:      * Sets the end element handler function for the XML parser parser.end_element_handler.
202:      * @param $parser (resource) The first parameter, parser, is a reference to the XML parser calling the handler.
203:      * @param $name (string) The second parameter, name, contains the name of the element for which this handler is called. If case-folding is in effect for this parser, the element name will be in uppercase letters.
204:      * @private
205:      */
206:     private function endElementHandler($parser, $name)
207:     {
208:         switch (strtolower($name)) {
209:             case 'tu': {
210:                 // translation unit element, unit father of every element to be translated. It can contain a unique identifier (tuid).
211:                 $this->current_key = '';
212:                 break;
213:             }
214:             case 'tuv': {
215:                 // translation unit variant, unit that contains the language code of the translation (xml:lang)
216:                 $this->current_language = '';
217:                 break;
218:             }
219:             case 'seg': {
220:                 // segment, it contains the translated text
221:                 $this->segdata = false;
222:                 if (!empty($this->current_data) or !array_key_exists($this->current_key, $this->resource)) {
223:                     $this->resource[$this->current_key] = $this->current_data; // set new array element
224:                     if (!empty($this->cachefile) and ($this->current_language == $this->language)) {
225:                         // write element to cache file
226:                         file_put_contents($this->cachefile, "\n".'$'.'tmx[\''.$this->current_key.'\']=\''.str_replace('\'', '\\\'', $this->current_data).'\';', FILE_APPEND);
227:                     }
228:                 }
229:                 break;
230:             }
231:             default: {
232:                 break;
233:             }
234:         }
235:     }
236: 
237:     /**
238:      * Sets the character data handler function for the XML parser parser.handler.
239:      * @param $parser (resource) The first parameter, parser, is a reference to the XML parser calling the handler.
240:      * @param $data (string) The second parameter, data, contains the character data as a string.
241:      * @private
242:      */
243:     private function segContentHandler($parser, $data)
244:     {
245:         if ($this->segdata and (strlen($this->current_key)>0) and (strlen($this->current_language)>0)) {
246:             // we are inside a seg element
247:             if (strcasecmp($this->current_language, $this->language) == 0) {
248:                 // we have reached the requested language translation
249:                 $this->current_data .= $data;
250:             }
251:         }
252:     }
253: 
254:     /**
255:      * Returns the resource array containing the translated word/sentences.
256:      * @return Array.
257:      */
258:     public function getResource()
259:     {
260:         return $this->resource;
261:     }
262: } // END OF CLASS
263: 
264: //============================================================+
265: // END OF FILE
266: //============================================================+
267: 
 

© 2004-2018 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy