Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob
  • Smarty
    • Cacher
    • Compiler
    • Config
    • Debug
    • PluginsBlock
    • PluginsFilter
    • PluginsFunction
    • PluginsInternal
    • PluginsModifier
    • PluginsModifierCompiler
    • PluginsShared
    • Security
    • Template
    • TemplateResources
  • Swift
    • ByteStream
    • CharacterStream
    • Encoder
    • Events
    • KeyCache
    • Mailer
    • Mime
    • Plugins
    • Transport

Classes

  • Swift_FailoverTransport
  • Swift_LoadBalancedTransport
  • Swift_MailTransport
  • Swift_Plugins_Loggers_ArrayLogger
  • Swift_Plugins_Loggers_EchoLogger
  • Swift_SendmailTransport
  • Swift_SmtpTransport
  • Swift_Transport_AbstractSmtpTransport
  • Swift_Transport_Esmtp_Auth_CramMd5Authenticator
  • Swift_Transport_Esmtp_Auth_LoginAuthenticator
  • Swift_Transport_Esmtp_Auth_PlainAuthenticator
  • Swift_Transport_Esmtp_AuthHandler
  • Swift_Transport_EsmtpTransport
  • Swift_Transport_FailoverTransport
  • Swift_Transport_LoadBalancedTransport
  • Swift_Transport_MailTransport
  • Swift_Transport_SendmailTransport
  • Swift_Transport_SimpleMailInvoker
  • Swift_Transport_StreamBuffer

Interfaces

  • Swift_Plugins_Logger
  • Swift_Plugins_Pop_Pop3Exception
  • Swift_Transport
  • Swift_Transport_Esmtp_Authenticator
  • Swift_Transport_EsmtpHandler
  • Swift_Transport_IoBuffer
  • Swift_Transport_MailInvoker
  • Swift_Transport_SmtpAgent
  • Swift_TransportException
  • Overview
  • Package
  • Function
  • Todo
  • Download
  1: <?php
  2: /**
  3:  * This file contains the module file translation class.
  4:  * TODO: Rework comments of this class.
  5:  *
  6:  * @package    Core
  7:  * @subpackage Backend
  8:  * @version    SVN Revision $Rev:$
  9:  *
 10:  * @author     Rusmir Jusufovic
 11:  * @copyright  four for business AG <www.4fb.de>
 12:  * @license    http://www.contenido.org/license/LIZENZ.txt
 13:  * @link       http://www.4fb.de
 14:  * @link       http://www.contenido.org
 15:  */
 16: 
 17: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 18: 
 19: /**
 20:  * This class save the translations from a modul in a file
 21:  * and get it from file.
 22:  *
 23:  * @package    Core
 24:  * @subpackage Backend
 25:  */
 26: class cModuleFileTranslation extends cModuleHandler {
 27: 
 28:     /**
 29:      * Path to the modul directory
 30:      *
 31:      * @var string
 32:      */
 33:     private $_modulePath;
 34: 
 35:     /**
 36:      * Name of the translations file
 37:      *
 38:      * @var string
 39:      */
 40:     static $fileName = '';
 41: 
 42:     /**
 43:      * Translation array.
 44:      *
 45:      * @var array
 46:      */
 47:     static $langArray = array();
 48: 
 49:     /**
 50:      * The id of the modul
 51:      *
 52:      * @var int
 53:      */
 54:     static $savedIdMod = NULL;
 55:     static $originalTranslationDivider = '=';
 56: 
 57:     /**
 58:      *
 59:      * @param int $idmodul
 60:      * @param bool $static if true it will load once the translation from file
 61:      * @param int $overrideIdlang use different language if not NULL
 62:      */
 63:     public function __construct($idmodul = NULL, $static = false, $overrideIdlang = NULL) {
 64:         parent::__construct($idmodul);
 65: 
 66:         // $this->_debug = true;
 67: 
 68:         if ($idmodul != NULL) {
 69:             $this->_modulePath = $this->getModulePath();
 70:         }
 71: 
 72:         // override language if specified
 73:         if ($overrideIdlang != NULL) {
 74:             $this->_idlang = $overrideIdlang;
 75:         }
 76: 
 77:         $this->_encoding = self::getEncoding($this->_idlang);
 78: 
 79:         // dont open the translations file for each mi18n call
 80:         if ($static == true) {
 81:             if (self::$savedIdMod != $idmodul) {
 82:                 // set filename lang_[language]_[Country].txt
 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:             // set filename lang_[language]_[Country].txt
 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:      * Get the value of a item from properties db.
102:      *
103:      * @param string $type
104:      * @param string $name
105:      * @return string value
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:      * Get the lang array.
116:      *
117:      * @return array
118:      */
119:     public function getLangArray() {
120:         return self::$langArray;
121:     }
122: 
123:     /**
124:      * Save the hole translations for a idmod and lang.
125:      * For the upgrade/setup.
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:             // set filename lang_[language]_[Country].txt
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:      * This method serialize a array.
174:      * $key.[Divider].$value."\r\n"
175:      *
176:      * @param array $wordListArray
177:      * @return string
178:      */
179:     private function _serializeArray($wordListArray) {
180:         $retString = '';
181:         foreach ($wordListArray as $key => $value) {
182:             // Originall String [Divider] Translation String
183:             $retString .= $key . self::$originalTranslationDivider . $value . "\r\n";
184:         }
185: 
186:         return $retString;
187:     }
188: 
189:     /**
190:      * This method unserialize a string.
191:      * The contents of file looks like original String [Divider] Translation
192:      * String.
193:      * If divider is =
194:      * Example: Hello World=Hallo Welt
195:      *
196:      * @param string $string the contents of the file
197:      * @return array
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:                 // CON-1671 never use end(array_keys(...))
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:      * Save the contents of the wordListArray in file.
223:      *
224:      * @param array $wordListArray
225:      * @return boolean true if success else false
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:      * Get the translations array.
250:      *
251:      * @return array
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: 
CMS CONTENIDO 4.9.7 API documentation generated by ApiGen