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