[ Index ]

PHP Cross Reference of Eventum

title

Body

[close]

/include/ -> class.language.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 encoding=utf-8: */
   3  // +----------------------------------------------------------------------+
   4  // | Eventum - Issue Tracking System                                      |
   5  // +----------------------------------------------------------------------+
   6  // | Copyright (c) 2003, 2004, 2005, 2006, 2007 MySQL AB                  |
   7  // |                                                                      |
   8  // | This program is free software; you can redistribute it and/or modify |
   9  // | it under the terms of the GNU General Public License as published by |
  10  // | the Free Software Foundation; either version 2 of the License, or    |
  11  // | (at your option) any later version.                                  |
  12  // |                                                                      |
  13  // | This program is distributed in the hope that it will be useful,      |
  14  // | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
  15  // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
  16  // | GNU General Public License for more details.                         |
  17  // |                                                                      |
  18  // | You should have received a copy of the GNU General Public License    |
  19  // | along with this program; if not, write to:                           |
  20  // |                                                                      |
  21  // | Free Software Foundation, Inc.                                       |
  22  // | 59 Temple Place - Suite 330                                          |
  23  // | Boston, MA 02111-1307, USA.                                          |
  24  // +----------------------------------------------------------------------+
  25  // | Authors:                                                             |
  26  // |  Bryan Alsdorf <bryan@mysql.com>                                     |
  27  // |  Elan Ruusamäe <glen@delfi.ee>                                       |
  28  // +----------------------------------------------------------------------+
  29  //
  30  
  31  /**
  32   * Class to handle the logic behind the internationalization issues
  33   * of the application.
  34   *
  35   * @version 1.0
  36   * @author Bryan Alsdorf <bryan@mysql.com>
  37   */
  38  
  39  /**
  40   * List of available locales present in Eventum.
  41   *
  42   * Note that the locales are first tested before they are listed as choices in
  43   * Preferences page.
  44   */
  45  
  46  $avail_langs = array(
  47      'pl_PL' =>  'Polish',
  48  #    'en_US' =>  'English',
  49      'ru_RU' =>  'Russian',
  50  #    'de_DE' =>  'German',
  51  #    'fr_FR' =>  'French',
  52      'it_IT' =>  'Italian',
  53      'fi_FI' =>  'Finnish',
  54  #    'es_ES' =>  'Spanish',
  55  #    'nl_NL' =>  'Dutch',
  56      'sv_SE' =>  'Swedish',
  57  );
  58  
  59  class Language
  60  {
  61  
  62      /**
  63       * Method used to set application default locale.
  64       *
  65       * @access  public
  66       * @return  void
  67       */
  68      function setup()
  69      {
  70          // please add the following line to config.inc.php, changing to whatever language you prefer
  71          // define('APP_DEFAULT_LOCALE', 'en_US');
  72  
  73          ini_set('mbstring.internal_encoding', 'UTF8');
  74  
  75          Language::set(APP_DEFAULT_LOCALE);
  76      }
  77  
  78      /**
  79       * Method used to get available languages.
  80       * Uses $avail_langs array and verifies that the language can be used.
  81       *
  82       * @access  public
  83       * @return  array
  84       */
  85      function getAvailableLanguages()
  86      {
  87          global $avail_langs;
  88  
  89          $languages = array();
  90          foreach ($avail_langs as $code => $language) {
  91              $res = Language::set($code);
  92              if ($res) {
  93                  $languages[$code] = $language;
  94              }
  95          }
  96  
  97          Language::restore();
  98          return $languages;
  99      }
 100  
 101      /**
 102       * Method used to set the appropriate preference of the language
 103       * for the application based on user preference.
 104       *
 105       * @access  public
 106       * @return  void
 107       */
 108      function setPreference()
 109      {
 110          $usr_id = Auth::getUserID();
 111          $lang = null;
 112          if (!empty($usr_id)) {
 113              // try user preference
 114              $usr_lang = User::getLang($usr_id);
 115              if (Language::set($usr_lang)) {
 116                  $lang = $usr_lang;
 117              }
 118          }
 119  
 120          if ($lang == null) {
 121              // fall back to system default
 122              define('APP_CURRENT_LOCALE', APP_DEFAULT_LOCALE);
 123              // we don't need to set language again as APP_DEFAULT_LOCALE was set by Language::setup()
 124              // Language::set(APP_CURRENT_LOCALE);
 125          } else {
 126              define('APP_CURRENT_LOCALE', $lang);
 127          }
 128      }
 129  
 130      /**
 131       * Sets active language for for the application.
 132       * Returns false if locale is invalid or cannot be used.
 133       *
 134       * @access  public
 135       * @return  boolean
 136       */
 137  	function set($locale)
 138      {
 139          // XXX do not append charset to en_US locale
 140          if ($locale != 'en_US') {
 141              $locale = $locale . '.' . APP_CHARSET;
 142          }
 143          $res = _setlocale(LC_TIME, $locale);
 144          if ($res === false) {
 145              return false;
 146          }
 147  
 148          $res = _setlocale(LC_MESSAGES, $locale);
 149          if ($res === false) {
 150              return false;
 151          }
 152  
 153          // XXX do not require translations for en_US locale
 154          if ($locale != 'en_US') {
 155              // get translator info
 156              $res = _gettext('');
 157              // if empty gettext is returned then the mo catalog is not installed.
 158              if (empty($res)) {
 159                  return false;
 160              }
 161          }
 162          User::setLocalizedRoles();
 163  
 164          return true;
 165      }
 166  
 167  
 168      function restore()
 169      {
 170          $locale = defined('APP_CURRENT_LOCALE') ? APP_CURRENT_LOCALE : APP_DEFAULT_LOCALE;
 171          Language::set($locale);
 172      }
 173  }
 174  
 175  // if there is no gettext support built into PHP, or we are running in language compatability mode include PHP-gettext
 176  if (!function_exists('gettext') || (defined('APP_GETTEXT_MODE') && APP_GETTEXT_MODE == 'php')) {
 177      require_once(APP_INC_PATH . 'php-gettext/gettext.inc');
 178  
 179  	function ev_gettext($string) {
 180          if (func_num_args() > 1) {
 181              $arg = array();
 182              for($i = 1 ; $i < func_num_args(); $i++) {
 183                  $arg[] = func_get_arg($i);
 184              }
 185              $string = _gettext($string);
 186              return vsprintf($string, $arg);
 187          } else {
 188              return _gettext($string);
 189          }
 190      }
 191  	function ev_ngettext($string, $plural, $number) {
 192          return _ngettext($string, $plural, $number);
 193      }
 194  
 195  } else {
 196  	function ev_gettext($string) {
 197          if (func_num_args() > 1) {
 198              $arg = array();
 199              for($i = 1 ; $i < func_num_args(); $i++) {
 200                  $arg[] = func_get_arg($i);
 201              }
 202  
 203              $string = gettext($string);
 204              return vsprintf($string, $arg);
 205          } else {
 206              return gettext($string);
 207          }
 208      }
 209  
 210  	function ev_ngettext($string, $plural, $number) {
 211          return ngettext($string, $plural, $number);
 212      }
 213  	function _bind_textdomain_codeset($domain, $codeset) {
 214          return bind_textdomain_codeset($domain, $codeset);
 215      }
 216  	function _bindtextdomain($domain, $path) {
 217          return bindtextdomain($domain, $path);
 218      }
 219  	function _textdomain($domain) {
 220          return textdomain($domain);
 221      }
 222  	function _gettext($msgid) {
 223          return gettext($msgid);
 224      }
 225  	function _setlocale($category, $locale) {
 226          return setlocale($category, $locale);
 227      }
 228  }
 229  
 230  // this won't change over the request. so set it once and permanently
 231  _bindtextdomain('eventum', APP_PATH . 'misc/localization/');
 232  _bind_textdomain_codeset('eventum', APP_CHARSET);
 233  _textdomain('eventum');
 234  
 235  // benchmarking the included file (aka setup time)
 236  if (APP_BENCHMARK) {
 237      $GLOBALS['bench']->setMarker('Included Language Class');
 238  }


Generated: Wed Dec 19 21:21:33 2007 Cross-referenced by PHPXref 0.7