1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23: 24:
25: class cModuleFileTranslation extends cModuleHandler {
26:
27: 28: 29: 30: 31:
32: private $_modulePath;
33:
34: 35: 36: 37: 38:
39: static $fileName = '';
40:
41: 42: 43: 44: 45:
46: static $langArray = array();
47:
48: 49: 50: 51: 52:
53: static $savedIdMod = NULL;
54: static $originalTranslationDivider = '=';
55:
56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67:
68: public function __construct($idmodul = NULL, $static = false, $overrideIdlang = NULL) {
69: parent::__construct($idmodul);
70:
71:
72:
73: if ($idmodul != NULL) {
74: $this->_modulePath = $this->getModulePath();
75: }
76:
77:
78: if ($overrideIdlang != NULL) {
79: $this->_idlang = $overrideIdlang;
80: }
81:
82: $this->_encoding = self::getEncoding($this->_idlang);
83:
84:
85: if ($static == true) {
86: if (self::$savedIdMod != $idmodul) {
87:
88: $language = $this->_getValueFromProperties('language', 'code');
89: $country = $this->_getValueFromProperties('country', 'code');
90: self::$fileName = 'lang_' . $language . '_' . cString::toUpperCase($country) . '.txt';
91:
92: self::$langArray = $this->getTranslationArray();
93: self::$savedIdMod = $idmodul;
94: }
95: } else {
96: self::$savedIdMod = -1;
97:
98:
99: $language = $this->_getValueFromProperties('language', 'code');
100: $country = $this->_getValueFromProperties('country', 'code');
101: self::$fileName = 'lang_' . $language . '_' . cString::toUpperCase($country) . '.txt';
102: }
103: }
104:
105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116:
117: private function _getValueFromProperties($type, $name) {
118: cApiPropertyCollection::reset();
119: $propColl = new cApiPropertyCollection();
120: $propColl->changeClient($this->_client);
121: return $propColl->getValue('idlang', $this->_idlang, $type, $name, '');
122: }
123:
124: 125: 126: 127: 128:
129: public function getLangArray() {
130: return self::$langArray;
131: }
132:
133: 134: 135: 136: 137: 138: 139:
140: public function saveTranslations() {
141: $db = cRegistry::getDb();
142:
143: $oLangColl = new cApiLanguageCollection();
144: $ids = $oLangColl->getAllIds();
145: foreach ($ids as $idlang) {
146: $sql = 'SELECT * FROM `%s` WHERE idlang = %d AND idmod = %d';
147: $sql = $db->prepare($sql, $this->_cfg['tab']['mod_translations'], $idlang, $this->_idmod);
148: $db->query($sql);
149:
150: $this->_idlang = $idlang;
151:
152: $language = $this->_getValueFromProperties('language', 'code');
153: $country = $this->_getValueFromProperties('country', 'code');
154: self::$fileName = 'lang_' . $language . '_' . cString::toUpperCase($country) . '.txt';
155:
156: $translations = array();
157: while ($db->nextRecord()) {
158: $original = mb_convert_encoding(urldecode(cSecurity::unFilter($db->f('original'))), "UTF-8");
159: $translation = mb_convert_encoding(urldecode(cSecurity::unFilter($db->f('translation'))), "UTF-8");
160: $translations[$original] = $translation;
161: }
162:
163: $text = $this->readInput();
164: if (!$text) {
165: $text = "";
166: }
167: $text .= $this->readOutput();
168:
169: mb_ereg_search_init($text, 'mi18n\(["|\'](.*?)["|\']\)');
170: while(mb_ereg_search()) {
171: $translation = mb_ereg_search_getregs();
172: if(!isset($translations[$translation[1]])) {
173: $translations[$translation[1]] = $translation[1];
174: }
175: }
176:
177: if (count($translations) != 0) {
178: if ($this->saveTranslationArray($translations) == false) {
179: cWarning(__FILE__, __LINE__, 'Could not save translate idmod=' . $this->_idmod . ' !');
180: }
181: }
182: }
183: }
184:
185: 186: 187: 188: 189: 190: 191: 192:
193: private function _serializeArray($wordListArray) {
194: $retString = '';
195: foreach ($wordListArray as $key => $value) {
196:
197: if (cString::getStringLength($key) > 0) {
198: $retString .= trim($key . self::$originalTranslationDivider . $value) . "\r\n";
199: }
200: }
201:
202: return trim($retString);
203: }
204:
205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217:
218: private function _unserializeArray($string) {
219: $retArray = array();
220:
221: $string = $string . PHP_EOL;
222: $words = preg_split('((\r\n)|(\r)|(\n))', cString::getPartOfString($string, 0, cString::getStringLength($string) - cString::getStringLength(PHP_EOL)));
223:
224: foreach ($words as $key => $value) {
225: $oriTrans = preg_split('/(?<!\\\\)' . self::$originalTranslationDivider . '/', $value);
226:
227: if (isset($oriTrans[1])) {
228: $retArray[cString::recodeString($oriTrans[0], $this->_fileEncoding, $this->_encoding)] = cString::recodeString(str_replace("\=", "=", $oriTrans[1]), $this->_fileEncoding, $this->_encoding);
229: } else {
230:
231: $keys = array_keys($retArray);
232: $lastKey = end($keys);
233: $newValue = PHP_EOL . cString::recodeString(str_replace("\=", "=", $oriTrans[0]), $this->_fileEncoding, $this->_encoding);
234: $retArray[$lastKey] .= $newValue;
235: }
236: }
237:
238: return $retArray;
239: }
240:
241: 242: 243: 244: 245: 246: 247: 248: 249:
250: public function saveTranslationArray($wordListArray) {
251: $fileName = $this->_modulePath . $this->_directories['lang'] . self::$fileName;
252:
253: if (!$this->createModuleDirectory('lang') || !$this->isWritable($fileName, $this->_modulePath . $this->_directories['lang'])) {
254: return false;
255: }
256:
257: $escapedArray = array();
258: foreach ($wordListArray as $key => $value) {
259: $newKey = cString::ereg_replace("=", "\\=", $key);
260: $newValue = cString::ereg_replace("=", "\\=", $value);
261: $escapedArray[$newKey] = $newValue;
262: }
263:
264: if (cFileHandler::write($fileName, $this->_serializeArray($escapedArray) . "\r\n") === false) {
265: return false;
266: } else {
267: return true;
268: }
269: }
270:
271: 272: 273: 274: 275: 276:
277: public function getTranslationArray() {
278: if (cFileHandler::exists($this->_modulePath . $this->_directories['lang'] . self::$fileName)) {
279: $array = $this->_unserializeArray(cFileHandler::read($this->_modulePath . $this->_directories['lang'] . self::$fileName));
280: return $array;
281: } else {
282: return array();
283: }
284: }
285:
286: }
287: