[ Index ]
 

Code source de Mantis 1.1.0rc3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/core/ -> lang_api.php (source)

   1  <?php
   2  # Mantis - a php based bugtracking system
   3  
   4  # Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
   5  # Copyright (C) 2002 - 2007  Mantis Team   - mantisbt-dev@lists.sourceforge.net
   6  
   7  # Mantis is free software: you can redistribute it and/or modify
   8  # it under the terms of the GNU General Public License as published by
   9  # the Free Software Foundation, either version 2 of the License, or
  10  # (at your option) any later version.
  11  #
  12  # Mantis is distributed in the hope that it will be useful,
  13  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  # GNU General Public License for more details.
  16  #
  17  # You should have received a copy of the GNU General Public License
  18  # along with Mantis.  If not, see <http://www.gnu.org/licenses/>.
  19  
  20      # --------------------------------------------------------
  21      # $Id: lang_api.php,v 1.40.4.1 2007-10-13 22:35:32 giallu Exp $
  22      # --------------------------------------------------------
  23  
  24      ### Language (Internationalization) API ##
  25  
  26      # Cache of localization strings in the language specified by the last
  27      # lang_load call
  28      $g_lang_strings = array();
  29  
  30      # stack for language overrides
  31      $g_lang_overrides = array();
  32  
  33      # To be used in custom_strings_inc.php :
  34      $g_active_language  = '';
  35  
  36      # ------------------
  37      # Loads the specified language and stores it in $g_lang_strings,
  38      # to be used by lang_get
  39  	function lang_load( $p_lang ) {
  40          global $g_lang_strings, $g_active_language;
  41  
  42          $g_active_language  = $p_lang;
  43          if ( isset( $g_lang_strings[ $p_lang ] ) ) {
  44              return;
  45          }
  46  
  47          $t_lang_dir = dirname ( dirname ( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR;
  48  
  49          require_once( $t_lang_dir . 'strings_' . $p_lang . '.txt' );
  50  
  51          # Allow overriding strings declared in the language file.
  52          # custom_strings_inc.php can use $g_active_language
  53          $t_custom_strings = dirname ( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'custom_strings_inc.php';
  54          if ( file_exists( $t_custom_strings ) ) {
  55              require( $t_custom_strings ); # this may be loaded multiple times, once per language
  56          }
  57  
  58          $t_vars = get_defined_vars();
  59  
  60          foreach ( array_keys( $t_vars ) as $t_var ) {
  61              $t_lang_var = ereg_replace( '^s_', '', $t_var );
  62              if ( $t_lang_var != $t_var || 'MANTIS_ERROR' == $t_var ) {
  63                  $g_lang_strings[ $p_lang ][ $t_lang_var ] = $$t_var;
  64              }
  65          }
  66      }
  67  
  68      # ------------------
  69      # Determine the preferred language
  70  	function lang_get_default() {
  71          global $g_active_language;
  72  
  73          $t_lang = false;
  74  
  75          # Confirm that the user's language can be determined
  76          if ( auth_is_user_authenticated() ) {
  77              $t_lang = user_pref_get_language( auth_get_current_user_id() );
  78          }
  79  
  80          # Otherwise fall back to default
  81          if ( false === $t_lang ) {
  82              $t_lang = config_get( 'default_language' );
  83          }
  84  
  85          if ( 'auto' == $t_lang ) {
  86              $t_lang = lang_map_auto();
  87          }
  88  
  89          # Remember the language
  90          $g_active_language = $t_lang;
  91  
  92          return $t_lang;
  93      }
  94  
  95      # ------------------
  96  
  97  	function lang_map_auto() {
  98          $t_lang = config_get( 'fallback_language' );
  99  
 100          if ( isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) {
 101              $t_accept_langs = explode( ',', $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
 102              $t_auto_map = config_get( 'language_auto_map' );
 103  
 104              # Expand language map
 105              $t_auto_map_exp = array();
 106              foreach( $t_auto_map as $t_encs => $t_enc_lang ) {
 107                  $t_encs_arr = explode( ',', $t_encs );
 108  
 109                  foreach ( $t_encs_arr as $t_enc ) {
 110                      $t_auto_map_exp[ trim( $t_enc ) ] = $t_enc_lang;
 111                  }
 112              }
 113  
 114              # Find encoding
 115              foreach ( $t_accept_langs as $t_accept_lang ) {
 116                  $t_tmp = explode( ';', strtolower( $t_accept_lang ) );
 117  
 118                  if ( isset( $t_auto_map_exp[ trim( $t_tmp[0] ) ] ) ) {
 119                      $t_valid_langs = config_get( 'language_choices_arr' );
 120                      $t_found_lang = $t_auto_map_exp[ trim( $t_tmp[0] ) ];
 121  
 122                      if ( in_array( $t_found_lang, $t_valid_langs, true ) ) {
 123                          $t_lang = $t_found_lang;
 124                          break;
 125                      }
 126                  }
 127              }
 128          }
 129  
 130          return $t_lang;
 131      }
 132  
 133      # Ensures that a language file has been loaded
 134  	function lang_ensure_loaded( $p_lang ) {
 135          global $g_lang_strings;
 136  
 137          if ( ! isset( $g_lang_strings[ $p_lang ] ) ) {
 138              lang_load( $p_lang );
 139          }
 140      }
 141  
 142  
 143  
 144      # ------------------
 145      # language stack implementation
 146      #
 147    # push a language onto the stack
 148    function lang_push( $p_lang=null ) {
 149          global $g_lang_overrides;
 150  
 151        # If no specific language is requested, we'll
 152          #  try to determine the language from the users
 153          #  preferences
 154  
 155          $t_lang = $p_lang;
 156  
 157          if ( null === $t_lang ) {
 158              $t_lang = config_get( 'default_language' );
 159          }
 160  
 161          # don't allow 'auto' as a language to be pushed onto the stack
 162          #  The results from auto are always the local user, not what the
 163          #  override wants, unless this is the first language setting
 164          if ( ( 'auto' == $t_lang ) && ( 0 < count( $g_lang_overrides ) ) ) {
 165              $t_lang = config_get( 'fallback_language' );
 166          }
 167  
 168          $g_lang_overrides[] = $t_lang;
 169  
 170          # Remember the language
 171          $g_active_language = $t_lang;
 172  
 173          # make sure it's loaded
 174          lang_ensure_loaded( $t_lang );
 175    }
 176  
 177    # pop a language onto the stack and return it
 178    function lang_pop( ) {
 179          global $g_lang_overrides;
 180  
 181          return array_pop( $g_lang_overrides );
 182    }
 183  
 184    # return value on top of the language stack
 185    #  return default if stack is empty
 186    function lang_get_current( ) {
 187          global $g_lang_overrides;
 188  
 189          $t_count_overrides = count($g_lang_overrides);
 190          if ($t_count_overrides > 0 ) {
 191              $t_lang = $g_lang_overrides[ $t_count_overrides - 1];
 192          } else {
 193              $t_lang = lang_get_default();
 194          }
 195  
 196          return $t_lang;
 197    }
 198  
 199  
 200  
 201  
 202      # ------------------
 203      # Retrieves an internationalized string
 204      #  This function will return one of (in order of preference):
 205      #    1. The string in the current user's preferred language (if defined)
 206      #    2. The string in English
 207  	function lang_get( $p_string, $p_lang = null ) {
 208          global $g_lang_strings;
 209  
 210          # If no specific language is requested, we'll
 211          #  try to determine the language from the users
 212          #  preferences
 213  
 214          $t_lang = $p_lang;
 215  
 216          if ( null === $t_lang ) {
 217              $t_lang = lang_get_current();
 218          }
 219  
 220          # Now we'll make sure that the requested language is loaded
 221  
 222          lang_ensure_loaded( $t_lang );
 223  
 224          # note in the current implementation we always return the same value
 225          #  because we don't have a concept of falling back on a language.  The
 226          #  language files actually *contain* English strings if none has been
 227          #  defined in the correct language
 228          # @@@ thraxisp - not sure if this is still true. Strings from last language loaded
 229          #      may still be in memeory if a new language is loaded.
 230  
 231          if ( lang_exists( $p_string, $t_lang ) ) {
 232              return $g_lang_strings[ $t_lang ][ $p_string];
 233          } else {
 234              if ( $t_lang == 'english' ) {
 235                  error_parameters( $p_string );
 236                  trigger_error( ERROR_LANG_STRING_NOT_FOUND, WARNING );
 237                  return '';
 238              } else {
 239                  # if string is not found in a language other than english, then retry using the english language.
 240                  return lang_get( $p_string, 'english' );
 241              }
 242          }
 243      }
 244  
 245      # ------------------
 246      # Check the language entry, if found return true, otherwise return false.
 247  	function lang_exists( $p_string, $p_lang ) {
 248          global $g_lang_strings;
 249  
 250          return ( isset( $g_lang_strings[ $p_lang ] )
 251              && isset( $g_lang_strings[ $p_lang ][ $p_string ] ) );
 252      }
 253  
 254      # ------------------
 255      # Get language:
 256      # - If found, return the appropriate string (as lang_get()).
 257      # - If not found, no default supplied, return the supplied string as is.
 258      # - If not found, default supplied, return default.
 259  	function lang_get_defaulted( $p_string, $p_default = null, $p_lang = null ) {
 260          $t_lang = $p_lang;
 261  
 262          if ( null === $t_lang ) {
 263              $t_lang = lang_get_current();
 264          }
 265  
 266          # Now we'll make sure that the requested language is loaded
 267          lang_ensure_loaded( $t_lang );
 268  
 269          if ( lang_exists( $p_string, $t_lang ) ) {
 270              return lang_get( $p_string );
 271          } else {
 272              if ( null === $p_default ) {
 273                  return $p_string;
 274              } else {
 275                  return $p_default;
 276              }
 277          }
 278      }
 279  ?>


Généré le : Thu Nov 29 09:42:17 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics