Xataface 2.0
Xataface Application Framework
Dataface/LanguageTool.php
Go to the documentation of this file.
00001 <?php
00002 /*-------------------------------------------------------------------------------
00003  * Xataface Web Application Framework
00004  * Copyright (C) 2005-2008 Web Lite Solutions Corp (shannah@sfu.ca)
00005  * 
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  * 
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019  *-------------------------------------------------------------------------------
00020  */
00021 
00022 class Dataface_LanguageTool {
00023         
00024         public static function &getInstance($lang=null){
00025                 if ( !isset($lang) ){
00026                         $app =& Dataface_Application::getInstance();
00027                         $lang = $app->_conf['lang'];
00028                 }
00029                 static $instance = 0;
00030                 if ( !is_array($instance) ){
00031                         $instance = array();
00032                 }
00033                 
00034                 if ( !isset($instance[$lang]) ){
00035                         $instance[$lang] = new Dataface_LanguageTool_Instance(array('lang'=>$lang));
00036                 }
00037                 return $instance[$lang];
00038         }
00039         
00040         
00041         
00042         
00047         public static function loadRealm($name){
00048                 return self::getInstance($this->app->_conf['default_language'])->loadRealm($name);
00049                 
00050                 
00051         }
00052         
00053         public static function addRealm($name, $dictionary=null){
00054                 return self::getInstance()->addRealm($name, $dictionary);
00055                 
00056         }
00057         
00058         public static function removeRealm($name){
00059                 return self::getInstance()->removeRealm($name);
00060                 
00061         }
00062         
00063         
00064         
00065         public static function _loadLangINIFile(/*$path*/){
00066 
00067                 return self::getInstance()->_loadLangINIFile();
00068 
00069                 
00070         }
00071         
00072         public static function translate($__translation_id, $__defaultText=null, $params=array(), $lang=null){
00073                 return self::getInstance($lang)->translate($__translation_id, $__defaultText, $params, $lang);
00074         }
00075         
00090         public static function getLanguageSelectorHTML($params=array()){
00091                 return self::getInstance()->getLanguageSelectorHTML($params);
00092                 
00093         
00094         }
00095         
00096         
00097 }
00098  
00099 
00100 class Dataface_LanguageTool_Instance {
00105         var $dictionary;
00106         
00111         var $app;
00112         
00118         var $realms = array();
00119         
00124         var $lang = null;
00125         
00126         
00133         function __construct($conf=null){
00134                 if ( is_array($conf) and isset($conf['lang']) ) $this->lang = $conf['lang'];
00135                 $this->_loadLangINIFile();
00136                 $this->app =& Dataface_Application::getInstance();
00137                 
00138                 
00139         }
00140         
00145         function loadRealm($name){
00146                 if ( !isset($this->realms[$name]) ){
00147                         $this->addRealm($name);
00148                         if ( $this->lang != $this->app->_conf['default_language'] ){
00149                                 Dataface_LanguageTool::getInstance($this->app->_conf['default_language'])->loadRealm($name);
00150                         }
00151                 }
00152         }
00153         
00154         function addRealm($name, $dictionary=null){
00155                 if ( !isset($dictionary) ){
00156                         $lang  = $this->lang;
00157                         if ( !$lang ) $lang = $this->app->_conf['lang'];
00158                         if ( file_exists($name.'.'.$lang.'.ini') ){
00159                                 $dictionary = parse_ini_file($name.'.'.$lang.'.ini');
00160                         } else {
00161                                 $dictionary = array();
00162                         }
00163                 }
00164                 $this->realms[$name] =& $dictionary;
00165         }
00166         
00167         function removeRealm($name){
00168                 unset($this->realms[$name]);
00169         }
00170         
00171         
00172         
00173         function _loadLangINIFile(/*$path*/){
00174 
00175                 $app =& Dataface_Application::getInstance();
00176                 $oldLang = $app->_conf['lang'];
00177                 if ( isset($this->lang) ) $app->_conf['lang'] = $this->lang;
00178                 $query =& $app->getQuery();
00179                 import('Dataface/ConfigTool.php');
00180                 $configTool =& Dataface_ConfigTool::getInstance();
00181                 $dictionary = $configTool->loadConfig('lang', null);
00182                 if ( isset($query['-table']) ) {
00183                         $tableDictionary = $configTool->loadConfig('lang', $query['-table']);
00184                         if (is_array($tableDictionary) ){
00185                                 $dictionary = array_merge($dictionary, $configTool->loadConfig('lang',$query['-table']));
00186                         }
00187                 }
00188                 $app->_conf['lang'] = $oldLang;
00189                 $this->dictionary =& $dictionary;
00190 
00191                 
00192         }
00193         
00194         function translate($__translation_id, $__defaultText=null, $params=array(), $lang=null){
00195                 if ( isset($this) and is_a($this, 'Dataface_LanguageTool')  and $this->lang == $lang ) $tool =& $this;
00196                 else $tool =& Dataface_LanguageTool::getInstance($lang);
00197                 
00198                 $__found_text = null;
00199                 foreach ( array_reverse(array_keys($tool->realms)) as $realmName ){
00200                         if ( isset($tool->realms[$realmName][$__translation_id]) ){
00201                                 $__found_text = $tool->realms[$realmName][$__translation_id];
00202                                 break;
00203                         }
00204                 }
00205                 if ( !isset($__found_text) and isset($tool->dictionary[$__translation_id]) ){
00206                         $__found_text = $tool->dictionary[$__translation_id];
00207                 }
00208                 if ( isset($__found_text) ) {
00209                         // make sure that there are no conflicting variable names as we are about to extract the params 
00210                         // array into local scope.
00211                         if ( isset($params['__translation_id']) ) unset($params['__translation_id']);
00212                         if ( isset($params['tool']) ) unset($params['tool']);
00213                         if (isset($params['__defaultText']) ) unset($params['__defaultText']);
00214                         if ( isset($params['params'])) unset($params['params']);
00215                         if ( isset($params['__found_text']) ) unset($params['__found_text']);
00216                         
00217                         extract($params);
00218                         @eval('$parsed = <<<END'."\n".$__found_text."\nEND\n;");
00219                         if ( !isset($parsed) ){
00220                                 return  $__defaultText;
00221                         }
00222                         return $parsed;
00223                 }
00224                 
00225                 if ( $tool->lang != $tool->app->_conf['default_language'] ){
00226                         return $tool->translate(
00227                                 $__translation_id, $__defaultText, $params, $tool->app->_conf['default_language']
00228                         );
00229                 }
00230                 
00231                 return $__defaultText;
00232         }
00233         
00248         function getLanguageSelectorHTML($params=array()){
00249                 if ( !isset($params['use_flags']) ) $params['use_flags'] = true;
00250                 import('I18Nv2/Language.php');
00251                 $langcode = ( isset($params['lang']) ? $params['lang'] : $this->app->_conf['lang']);
00252                 $languageCodes = new I18Nv2_Language($langcode);
00253                 $currentLanguage = $languageCodes->getName( $this->app->_conf['lang']);
00254                 $name = (isset($params['name']) ? $params['name'] : 'language');
00255                 $options = array();
00256                 $var = (isset($params['var']) ? $params['var'] : '-lang');
00257                 $selected = (isset($params['selected']) ? $params['selected'] : $this->app->_conf['lang']);
00258                 $selectedValue = $languageCodes->getName($selected);
00259                 $autosubmit = isset($params['autosubmit']) and $params['autosubmit'];
00260                 $type = ( isset($params['type']) ? $params['type'] : 'select');
00261                 
00262                 if ( isset($params['table']) ){
00263                         $table =& Dataface_Table::loadTable($params['table']);
00264                         $languages = array_keys($table->getTranslations());
00265                 } else {
00266                         $languages = $this->app->_conf['languages'];
00267                 }
00268                 if ( !is_array($languages) ) return '';
00269                 
00270                 if ( $autosubmit) {
00271                         $onchange = 'javascript:window.location=this.options[this.selectedIndex].value;';
00272                         foreach ( $languages as $lang ){
00273                                 $curri18n = new I18Nv2_Language($langCode);
00274                                 $langname = $curri18n->getName($lang);
00275                                 $options[$this->app->url($var.'='.$lang)] = array('code'=>$lang, 'name'=>$langname);
00276                         }
00277                 } else {
00278                         $onchange = '';
00279                         foreach ($languages as $lang ){
00280                                 $curri18n = new I18Nv2_Language($langCode);
00281                                 $langname = $curri18n->getName($lang);
00282                                 $options[$lang] = array('code'=>$lang, 'name'=>$langname);
00283                         }
00284                 }
00285 
00286                 if (count($options) <= 1) return '';
00287                 ob_start();
00288                 if ( $type == 'select' ){
00289                 
00290                         echo '<select name="'.$name.'" '.($onchange ? 'onchange="'.$onchange.'"' : '').'>
00291                         ';
00292                         foreach ($options as $code => $value ){
00293                                 echo '<option value="'.$code.'"'. ( ($value['code'] == $selected) ? ' selected' : '').'>'.$value['name'].'</option>
00294                                 ';
00295                         }
00296                         echo '</select>';
00297                 } else {
00298                         echo '<ul id="'.$name.'" class="language-selection-list">
00299                         ';
00300                         foreach ( $languages as $code  ){
00301                                 if ( !isset($params['lang']) and $this->app->_conf['language_labels'][$code] != $code ){
00302                                         $languageName = $this->app->_conf['language_labels'][$code];
00303                                 } else {
00304                                         $languageName = $languageCodes->getName($code);
00305                                 }
00306                                 //$languageName = $languageCodes->getName($code);
00307                                 echo '<li class="language-selection-item '.( ($code == $this->app->_conf['lang']) ? ' selected-language' : '').'">
00308                                 <a href="'.$this->app->url($var.'='.$code).'">';
00309                                 if ( $params['use_flags'] ){
00310                                         echo '<img src="'.DATAFACE_URL.'/images/flags/'.$code.'_small.gif" alt="'.$languageName.'" />';
00311                                 } else {
00312                                         echo $languageName;
00313                                 }
00314                                 echo '</a></li>';
00315                         }
00316                         echo "</ul>";
00317                 }
00318                 $out = ob_get_contents();
00319                 ob_end_clean();
00320                 return $out;
00321                 
00322         
00323         }
00324         
00325         function getLanguageLabel($code){
00326                 import('I18Nv2/Language.php');
00327                 
00328                 $langcode = $this->app->_conf['lang'];
00329                 $languageCodes = new I18Nv2_Language($langcode);
00330                 $languageName = null;
00331                 if ( @$this->app->_conf['language_labels'][$code] and $this->app->_conf['language_labels'][$code] != $code ){
00332                         $languageName = $this->app->_conf['language_labels'][$code];
00333                 } else {
00334                         //echo "Name : $code";
00335                         $languageName = $languageCodes->getName($code);
00336                 }
00337                 return $languageName;
00338 
00339         }
00340         
00341         function getLanguageFlag($code){
00342                 return DATAFACE_URL.'/images/flags/'.$code.'_small.gif';
00343         
00344         }
00345         
00346         
00347 }
 All Data Structures Namespaces Files Functions Variables Enumerations