Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
LanguageTool.php
Go to the documentation of this file.
1 <?php
2 /*-------------------------------------------------------------------------------
3  * Xataface Web Application Framework
4  * Copyright (C) 2005-2008 Web Lite Solutions Corp (shannah@sfu.ca)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *-------------------------------------------------------------------------------
20  */
21 
23 
24  public static function &getInstance($lang=null){
25  if ( !isset($lang) ){
27  $lang = $app->_conf['lang'];
28  }
29  static $instance = 0;
30  if ( !is_array($instance) ){
31  $instance = array();
32  }
33 
34  if ( !isset($instance[$lang]) ){
35  $instance[$lang] = new Dataface_LanguageTool_Instance(array('lang'=>$lang));
36  }
37  return $instance[$lang];
38  }
39 
40 
41 
42 
47  public static function loadRealm($name){
48  return self::getInstance($this->app->_conf['default_language'])->loadRealm($name);
49 
50 
51  }
52 
53  public static function addRealm($name, $dictionary=null){
54  return self::getInstance()->addRealm($name, $dictionary);
55 
56  }
57 
58  public static function removeRealm($name){
59  return self::getInstance()->removeRealm($name);
60 
61  }
62 
63 
64 
65  public static function _loadLangINIFile(/*$path*/){
66 
67  return self::getInstance()->_loadLangINIFile();
68 
69 
70  }
71 
72  public static function translate($__translation_id, $__defaultText=null, $params=array(), $lang=null){
73  return self::getInstance($lang)->translate($__translation_id, $__defaultText, $params, $lang);
74  }
75 
90  public static function getLanguageSelectorHTML($params=array()){
91  return self::getInstance()->getLanguageSelectorHTML($params);
92 
93 
94  }
95 
96 
97 }
98 
99 
106 
111  var $app;
112 
118  var $realms = array();
119 
124  var $lang = null;
125 
126 
133  function __construct($conf=null){
134  if ( is_array($conf) and isset($conf['lang']) ) $this->lang = $conf['lang'];
135  $this->_loadLangINIFile();
136  $this->app =& Dataface_Application::getInstance();
137 
138 
139  }
140 
145  function loadRealm($name){
146  if ( !isset($this->realms[$name]) ){
147  $this->addRealm($name);
148  if ( $this->lang != $this->app->_conf['default_language'] ){
149  Dataface_LanguageTool::getInstance($this->app->_conf['default_language'])->loadRealm($name);
150  }
151  }
152  }
153 
154  function addRealm($name, $dictionary=null){
155  if ( !isset($dictionary) ){
156  $lang = $this->lang;
157  if ( !$lang ) $lang = $this->app->_conf['lang'];
158  if ( file_exists($name.'.'.$lang.'.ini') ){
159  $dictionary = parse_ini_file($name.'.'.$lang.'.ini');
160  } else {
161  $dictionary = array();
162  }
163  }
164  $this->realms[$name] =& $dictionary;
165  }
166 
167  function removeRealm($name){
168  unset($this->realms[$name]);
169  }
170 
171 
172 
173  function _loadLangINIFile(/*$path*/){
174 
176  $oldLang = $app->_conf['lang'];
177  if ( isset($this->lang) ) $app->_conf['lang'] = $this->lang;
178  $query =& $app->getQuery();
179  import('Dataface/ConfigTool.php');
180  $configTool =& Dataface_ConfigTool::getInstance();
181  $dictionary = $configTool->loadConfig('lang', null);
182  if ( isset($query['-table']) ) {
183  $tableDictionary = $configTool->loadConfig('lang', $query['-table']);
184  if (is_array($tableDictionary) ){
185  $dictionary = array_merge($dictionary, $configTool->loadConfig('lang',$query['-table']));
186  }
187  }
188  $app->_conf['lang'] = $oldLang;
189  $this->dictionary =& $dictionary;
190 
191 
192  }
193 
194  function translate($__translation_id, $__defaultText=null, $params=array(), $lang=null){
195  if ( isset($this) and is_a($this, 'Dataface_LanguageTool') and $this->lang == $lang ) $tool =& $this;
197 
198  $__found_text = null;
199  foreach ( array_reverse(array_keys($tool->realms)) as $realmName ){
200  if ( isset($tool->realms[$realmName][$__translation_id]) ){
201  $__found_text = $tool->realms[$realmName][$__translation_id];
202  break;
203  }
204  }
205  if ( !isset($__found_text) and isset($tool->dictionary[$__translation_id]) ){
206  $__found_text = $tool->dictionary[$__translation_id];
207  }
208  if ( isset($__found_text) ) {
209  // make sure that there are no conflicting variable names as we are about to extract the params
210  // array into local scope.
211  if ( isset($params['__translation_id']) ) unset($params['__translation_id']);
212  if ( isset($params['tool']) ) unset($params['tool']);
213  if (isset($params['__defaultText']) ) unset($params['__defaultText']);
214  if ( isset($params['params'])) unset($params['params']);
215  if ( isset($params['__found_text']) ) unset($params['__found_text']);
216 
217  extract($params);
218  @eval('$parsed = <<<END'."\n".$__found_text."\nEND\n;");
219  if ( !isset($parsed) ){
220  return $__defaultText;
221  }
222  return $parsed;
223  }
224 
225  if ( $tool->lang != $tool->app->_conf['default_language'] ){
226  return $tool->translate(
227  $__translation_id, $__defaultText, $params, $tool->app->_conf['default_language']
228  );
229  }
230 
231  return $__defaultText;
232  }
233 
248  function getLanguageSelectorHTML($params=array()){
249  if ( !isset($params['use_flags']) ) $params['use_flags'] = true;
250  import('I18Nv2/Language.php');
251  $langcode = ( isset($params['lang']) ? $params['lang'] : $this->app->_conf['lang']);
252  $languageCodes = new I18Nv2_Language($langcode);
253  $currentLanguage = $languageCodes->getName( $this->app->_conf['lang']);
254  $name = (isset($params['name']) ? $params['name'] : 'language');
255  $options = array();
256  $var = (isset($params['var']) ? $params['var'] : '-lang');
257  $selected = (isset($params['selected']) ? $params['selected'] : $this->app->_conf['lang']);
258  $selectedValue = $languageCodes->getName($selected);
259  $autosubmit = isset($params['autosubmit']) and $params['autosubmit'];
260  $type = ( isset($params['type']) ? $params['type'] : 'select');
261 
262  if ( isset($params['table']) ){
263  $table =& Dataface_Table::loadTable($params['table']);
264  $languages = array_keys($table->getTranslations());
265  } else {
266  $languages = $this->app->_conf['languages'];
267  }
268  if ( !is_array($languages) ) return '';
269 
270  if ( $autosubmit) {
271  $onchange = 'javascript:window.location=this.options[this.selectedIndex].value;';
272  foreach ( $languages as $lang ){
273  $curri18n = new I18Nv2_Language($langCode);
274  $langname = $curri18n->getName($lang);
275  $options[$this->app->url($var.'='.$lang)] = array('code'=>$lang, 'name'=>$langname);
276  }
277  } else {
278  $onchange = '';
279  foreach ($languages as $lang ){
280  $curri18n = new I18Nv2_Language($langCode);
281  $langname = $curri18n->getName($lang);
282  $options[$lang] = array('code'=>$lang, 'name'=>$langname);
283  }
284  }
285 
286  if (count($options) <= 1) return '';
287  ob_start();
288  if ( $type == 'select' ){
289 
290  echo '<select name="'.$name.'" '.($onchange ? 'onchange="'.$onchange.'"' : '').'>
291  ';
292  foreach ($options as $code => $value ){
293  echo '<option value="'.$code.'"'. ( ($value['code'] == $selected) ? ' selected' : '').'>'.$value['name'].'</option>
294  ';
295  }
296  echo '</select>';
297  } else {
298  echo '<ul id="'.$name.'" class="language-selection-list">
299  ';
300  foreach ( $languages as $code ){
301  if ( !isset($params['lang']) and $this->app->_conf['language_labels'][$code] != $code ){
302  $languageName = $this->app->_conf['language_labels'][$code];
303  } else {
304  $languageName = $languageCodes->getName($code);
305  }
306  //$languageName = $languageCodes->getName($code);
307  echo '<li class="language-selection-item '.( ($code == $this->app->_conf['lang']) ? ' selected-language' : '').'">
308  <a href="'.$this->app->url($var.'='.$code).'">';
309  if ( $params['use_flags'] ){
310  echo '<img src="'.DATAFACE_URL.'/images/flags/'.$code.'_small.gif" alt="'.$languageName.'" />';
311  } else {
312  echo $languageName;
313  }
314  echo '</a></li>';
315  }
316  echo "</ul>";
317  }
318  $out = ob_get_contents();
319  ob_end_clean();
320  return $out;
321 
322 
323  }
324 
325  function getLanguageLabel($code){
326  import('I18Nv2/Language.php');
327 
328  $langcode = $this->app->_conf['lang'];
329  $languageCodes = new I18Nv2_Language($langcode);
330  $languageName = null;
331  if ( @$this->app->_conf['language_labels'][$code] and $this->app->_conf['language_labels'][$code] != $code ){
332  $languageName = $this->app->_conf['language_labels'][$code];
333  } else {
334  //echo "Name : $code";
335  $languageName = $languageCodes->getName($code);
336  }
337  return $languageName;
338 
339  }
340 
341  function getLanguageFlag($code){
342  return DATAFACE_URL.'/images/flags/'.$code.'_small.gif';
343 
344  }
345 
346 
347 }