[ Index ]

PHP Cross Reference of MantisBT

title

Body

[close]

/core/ -> lang_api.php (source)

   1  <?php
   2  # MantisBT - A PHP based bugtracking system
   3  
   4  # MantisBT is free software: you can redistribute it and/or modify
   5  # it under the terms of the GNU General Public License as published by
   6  # the Free Software Foundation, either version 2 of the License, or
   7  # (at your option) any later version.
   8  #
   9  # MantisBT is distributed in the hope that it will be useful,
  10  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  # GNU General Public License for more details.
  13  #
  14  # You should have received a copy of the GNU General Public License
  15  # along with MantisBT.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Language (Internationalization) API
  19   *
  20   * @package CoreAPI
  21   * @subpackage LanguageAPI
  22   * @copyright Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
  23   * @copyright Copyright (C) 2002 - 2011  MantisBT Team - mantisbt-dev@lists.sourceforge.net
  24   * @link http://www.mantisbt.org
  25   *
  26   * @uses authentication_api.php
  27   * @uses config_api.php
  28   * @uses constant_inc.php
  29   * @uses error_api.php
  30   * @uses plugin_api.php
  31   * @uses user_pref_api.php
  32   */
  33  
  34  require_api( 'authentication_api.php' );
  35  require_api( 'config_api.php' );
  36  require_api( 'constant_inc.php' );
  37  require_api( 'error_api.php' );
  38  require_api( 'plugin_api.php' );
  39  require_api( 'user_pref_api.php' );
  40  
  41  # Cache of localization strings in the language specified by the last
  42  # lang_load call
  43  $g_lang_strings = array();
  44  
  45  # stack for language overrides
  46  $g_lang_overrides = array();
  47  
  48  # To be used in custom_strings_inc.php :
  49  $g_active_language = '';
  50  
  51  /**
  52   * Loads the specified language and stores it in $g_lang_strings, to be used by lang_get
  53   * @param string $p_lang
  54   * @param string $p_dir
  55   * @return null
  56   */
  57  function lang_load( $p_lang, $p_dir = null ) {
  58      global $g_lang_strings, $g_active_language;
  59  
  60      $g_active_language = $p_lang;
  61      if( isset( $g_lang_strings[$p_lang] ) && is_null( $p_dir ) ) {
  62          return;
  63      }
  64  
  65      if( !lang_language_exists( $p_lang ) ) {
  66          return;
  67      }
  68  
  69      // Step 1 - Load Requested Language file
  70      // @@ and if file doesn't exist???
  71      if( $p_dir === null ) {
  72          include_once( config_get( 'language_path' ) . 'strings_' . $p_lang . '.txt' );
  73      } else {
  74          if( is_file( $p_dir . 'strings_' . $p_lang . '.txt' ) ) {
  75              include_once( $p_dir . 'strings_' . $p_lang . '.txt' );
  76          }
  77      }
  78  
  79      // Step 2 - Allow overriding strings declared in the language file.
  80      //          custom_strings_inc.php can use $g_active_language
  81      // 2 formats:
  82      // $s_* - old format
  83      // $s_custom_strings array - new format
  84      // NOTE: it's not expected that you'd mix/merge old/new formats within this file.
  85      $t_custom_strings = config_get( 'custom_strings_file' ) ;
  86      if( file_exists( $t_custom_strings ) ) {
  87          # this may be loaded multiple times, once per language
  88          require( $t_custom_strings );        
  89      }
  90      
  91      // Step 3  - New Language file format
  92      // Language file consists of an array
  93      if( isset( $s_messages ) ) {
  94          // lang strings array entry can only be set if $p_dir is not null - i.e. in a plugin
  95          if( isset( $g_lang_strings[$p_lang] ) ) {
  96              if( isset( $s_custom_messages[$p_lang] ) ) {
  97                  // Step 4 - handle merging in custom strings:
  98                  // Possible states:
  99                  // 4.a - new string format + new custom string format    
 100                  $g_lang_strings[$p_lang] = array_replace( ((array)$g_lang_strings[$p_lang]), (array)$s_messages, (array)$s_custom_messages[$p_lang]);
 101                  return;
 102              } else {
 103                  $g_lang_strings[$p_lang] = array_replace( ((array)$g_lang_strings[$p_lang]), (array)$s_messages);
 104              }
 105          } else {
 106              // new language loaded
 107              $g_lang_strings[$p_lang] = $s_messages;
 108              if( isset( $s_custom_messages[$p_lang] ) ) {
 109                  // 4.a - new string format + new custom string format    
 110                  $g_lang_strings[$p_lang] = array_replace( ((array)$g_lang_strings[$p_lang]), (array)$s_custom_messages[$p_lang]);
 111                  return;
 112              }
 113          }
 114      }
 115  
 116      // 4.b new string format + old custom string format
 117      // 4.c - old string format + old custom string format
 118      if( !isset( $s_messages ) || file_exists( $t_custom_strings ) ) {
 119          $t_vars = get_defined_vars();
 120  
 121          foreach( array_keys( $t_vars ) as $t_var ) {
 122              $t_lang_var = preg_replace( '/^s_/', '', $t_var );
 123              if( $t_lang_var != $t_var ) {
 124                   $g_lang_strings[$p_lang][$t_lang_var] = $$t_var;
 125               }
 126              else if( 'MANTIS_ERROR' == $t_var ) {
 127                  if( isset( $g_lang_strings[$p_lang][$t_lang_var] ) ) {
 128                      foreach( $$t_var as $key => $val ) {
 129                          $g_lang_strings[$p_lang][$t_lang_var][$key] = $val;
 130                      }
 131                  } else {
 132                      $g_lang_strings[$p_lang][$t_lang_var] = $$t_var;
 133                  }
 134              }
 135          }
 136          // 4.d old string format + new custom string format
 137          // merge new custom strings into array in same way we merge in 4.a
 138          if( isset( $s_custom_messages[$p_lang] ) ) {
 139              $g_lang_strings[$p_lang] = array_replace( ((array)$g_lang_strings[$p_lang]), (array)$s_custom_messages[$p_lang]);
 140          }
 141      }
 142  }
 143  
 144  /**
 145   * Determine the preferred language
 146   * @return string
 147   */
 148  function lang_get_default() {
 149      global $g_active_language;
 150  
 151      $t_lang = false;
 152  
 153      # Confirm that the user's language can be determined
 154      if( function_exists( 'auth_is_user_authenticated' ) && auth_is_user_authenticated() ) {
 155          $t_lang = user_pref_get_language( auth_get_current_user_id() );
 156      }
 157  
 158      # Otherwise fall back to default
 159      if( !$t_lang ) {
 160          $t_lang = config_get_global( 'default_language' );
 161      }
 162  
 163      if( $t_lang == 'auto' ) {
 164          $t_lang = lang_map_auto();
 165      }
 166  
 167      # Remember the language
 168      $g_active_language = $t_lang;
 169  
 170      return $t_lang;
 171  }
 172  
 173  /**
 174   *
 175   * @return string
 176   */
 177  function lang_map_auto() {
 178      $t_lang = config_get( 'fallback_language' );
 179  
 180      if( isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) {
 181          $t_accept_langs = explode( ',', $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
 182          $t_auto_map = config_get( 'language_auto_map' );
 183  
 184          # Expand language map
 185          $t_auto_map_exp = array();
 186          foreach( $t_auto_map as $t_encs => $t_enc_lang ) {
 187              $t_encs_arr = explode( ',', $t_encs );
 188  
 189              foreach( $t_encs_arr as $t_enc ) {
 190                  $t_auto_map_exp[trim( $t_enc )] = $t_enc_lang;
 191              }
 192          }
 193  
 194          # Find encoding
 195          foreach( $t_accept_langs as $t_accept_lang ) {
 196              $t_tmp = explode( ';', utf8_strtolower( $t_accept_lang ) );
 197  
 198              if( isset( $t_auto_map_exp[trim( $t_tmp[0] )] ) ) {
 199                  $t_valid_langs = config_get( 'language_choices_arr' );
 200                  $t_found_lang = $t_auto_map_exp[trim( $t_tmp[0] )];
 201  
 202                  if( in_array( $t_found_lang, $t_valid_langs, true ) ) {
 203                      $t_lang = $t_found_lang;
 204                      break;
 205                  }
 206              }
 207          }
 208      }
 209  
 210      return $t_lang;
 211  }
 212  
 213  /**
 214   * Ensures that a language file has been loaded
 215   * @param string $p_lang the language name
 216   * @return null
 217   */
 218  function lang_ensure_loaded( $p_lang ) {
 219      global $g_lang_strings;
 220  
 221      if( !isset( $g_lang_strings[$p_lang] ) ) {
 222          lang_load( $p_lang );
 223      }
 224  }
 225  
 226  /**
 227  * Check if the given language exists
 228  *
 229  * @param string $p_lang the language name
 230  * @return boolean
 231  */
 232  function lang_language_exists( $p_lang ) {
 233      $t_valid_langs = config_get( 'language_choices_arr' );
 234      $t_valid = in_array( $p_lang, $t_valid_langs, true );
 235      return $t_valid;
 236  }
 237  
 238  /**
 239   * language stack implementation
 240   * push a language onto the stack
 241   * @param string $p_lang
 242   * @return null
 243   */
 244  function lang_push( $p_lang = null ) {
 245      global $g_lang_overrides;
 246  
 247      # If no specific language is requested, we'll
 248      #  try to determine the language from the users
 249      #  preferences
 250  
 251      $t_lang = $p_lang;
 252  
 253      if( null === $t_lang ) {
 254          $t_lang = config_get( 'default_language' );
 255      }
 256  
 257      # don't allow 'auto' as a language to be pushed onto the stack
 258      #  The results from auto are always the local user, not what the
 259      #  override wants, unless this is the first language setting
 260      if(( 'auto' == $t_lang ) && ( 0 < count( $g_lang_overrides ) ) ) {
 261          $t_lang = config_get( 'fallback_language' );
 262      }
 263  
 264      $g_lang_overrides[] = $t_lang;
 265  
 266      # Remember the language
 267      $g_active_language = $t_lang;
 268  
 269      # make sure it's loaded
 270      lang_ensure_loaded( $t_lang );
 271  }
 272  
 273  /**
 274   * pop a language onto the stack and return it
 275   * @return string
 276   */
 277  function lang_pop() {
 278      global $g_lang_overrides;
 279  
 280      return array_pop( $g_lang_overrides );
 281  }
 282  
 283  /**
 284   * return value on top of the language stack
 285   * return default if stack is empty
 286   * @return string
 287   */
 288  function lang_get_current() {
 289      global $g_lang_overrides;
 290  
 291      $t_count_overrides = count( $g_lang_overrides );
 292      if( $t_count_overrides > 0 ) {
 293          $t_lang = $g_lang_overrides[$t_count_overrides - 1];
 294      } else {
 295          $t_lang = lang_get_default();
 296      }
 297  
 298      return $t_lang;
 299  }
 300  
 301  /**
 302   * Retrieves an internationalized string
 303   * This function will return one of (in order of preference):
 304   *  1. The string in the current user's preferred language (if defined)
 305   *  2. The string in English
 306   * @param string $p_string
 307   * @param string $p_lang
 308   * @param bool $p_error default: true - error if string not found
 309   * @return string
 310   */
 311  function lang_get( $p_string, $p_lang = null, $p_error = true ) {
 312      global $g_lang_strings;
 313  
 314      # If no specific language is requested, we'll
 315      #  try to determine the language from the users
 316      #  preferences
 317  
 318      $t_lang = $p_lang;
 319  
 320      if( null === $t_lang ) {
 321          $t_lang = lang_get_current();
 322      }
 323  
 324      // Now we'll make sure that the requested language is loaded
 325      lang_ensure_loaded( $t_lang );
 326  
 327      // Step 1 - see if language string exists in requested language
 328      if( lang_exists( $p_string, $t_lang ) ) {
 329          return $g_lang_strings[$t_lang][$p_string];
 330      } else {
 331          // Language string doesn't exist in requested language
 332          
 333          // Step 2 - See if language string exists in current plugin
 334          $t_plugin_current = plugin_get_current();
 335          if( !is_null( $t_plugin_current ) ) {
 336              // Step 3 - Plugin exists: load language file
 337              lang_load( $t_lang, config_get( 'plugin_path' ) . $t_plugin_current . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR );
 338              if( lang_exists( $p_string, $t_lang ) ) {
 339                  return $g_lang_strings[$t_lang][$p_string];
 340              }
 341              
 342              // Step 4 - Localised language entry didn't exist - fallback to english for plugin
 343              lang_load( 'english', config_get( 'plugin_path' ) . $t_plugin_current . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR );
 344              if( lang_exists( $p_string, $t_lang ) ) {
 345                  return $g_lang_strings[$t_lang][$p_string];
 346              }            
 347          }
 348  
 349          // Step 5 - string didn't exist, try fall back to english:
 350          if( $t_lang == 'english' ) {
 351              if( $p_error ) {
 352                  error_parameters( $p_string );
 353                  trigger_error( ERROR_LANG_STRING_NOT_FOUND, WARNING );
 354              }
 355              return '';
 356          } else {
 357              // if string is not found in a language other than english, then retry using the english language.
 358              return lang_get( $p_string, 'english' );
 359          }
 360      }
 361  }
 362  
 363  /**
 364   * Check the language entry, if found return true, otherwise return false.
 365   * @param string $p_string
 366   * @param string $p_lang
 367   * @return bool
 368   */
 369  function lang_exists( $p_string, $p_lang ) {
 370      global $g_lang_strings;
 371  
 372      return( isset( $g_lang_strings[$p_lang] ) && isset( $g_lang_strings[$p_lang][$p_string] ) );
 373  }
 374  
 375  /**
 376   * Get language:
 377   * - If found, return the appropriate string (as lang_get()).
 378   * - If not found, no default supplied, return the supplied string as is.
 379   * - If not found, default supplied, return default.
 380   * @param string $p_string
 381   * @param string $p_default
 382   * @param string $p_lang
 383   * @return string
 384   */
 385  function lang_get_defaulted( $p_string, $p_default = null, $p_lang = null ) {
 386      $t_lang = $p_lang;
 387  
 388      if( null === $t_lang ) {
 389          $t_lang = lang_get_current();
 390      }
 391  
 392      # Now we'll make sure that the requested language is loaded
 393      lang_ensure_loaded( $t_lang );
 394  
 395      if( lang_exists( $p_string, $t_lang ) ) {
 396          return lang_get( $p_string );
 397      } else {
 398          if( null === $p_default ) {
 399              return $p_string;
 400          } else {
 401              return $p_default;
 402          }
 403      }
 404  }


Generated: Thu Jul 28 15:48:31 2011 Cross-referenced by PHPXref 0.7