[ Index ]

PHP Cross Reference of MyBB 1.8.37

title

Body

[close]

/inc/ -> class_language.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.8
   4   * Copyright 2014 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://www.mybb.com
   7   * License: http://www.mybb.com/about/license
   8   *
   9   */
  10  
  11  #[AllowDynamicProperties]
  12  class MyLanguage
  13  {
  14  
  15      /**
  16       * The path to the languages folder.
  17       *
  18       * @var string
  19       */
  20      public $path;
  21  
  22      /**
  23       * The language we are using and the area (if admin).
  24       * 
  25       * For example 'english' or 'english/admin'.
  26       *
  27       * @var string
  28       */
  29      public $language;
  30  
  31      /**
  32       * The fallback language we are using and the area (if admin).
  33       * 
  34       * For example 'english' or 'english/admin'.
  35       *
  36       * @var string
  37       */
  38      public $fallback = 'english';
  39  
  40      /**
  41       * The fallback language we are using.
  42       *
  43       * @var string
  44       */
  45      public $fallbackLanguage = 'english';
  46  
  47      /**
  48       * Information about the current language.
  49       *
  50       * @var array
  51       */
  52      public $settings;
  53  
  54      /**
  55       * Set the path for the language folder.
  56       *
  57       * @param string $path The path to the language folder.
  58       */
  59  	function set_path($path)
  60      {
  61          $this->path = $path;
  62      }
  63  
  64      /**
  65       * Check if a specific language exists.
  66       *
  67       * @param string $language The language to check for.
  68       * @return boolean True when exists, false when does not exist.
  69       */
  70  	function language_exists($language)
  71      {
  72          $language = preg_replace("#[^a-z0-9\-_]#i", "", $language);
  73          if(file_exists($this->path."/".$language.".php"))
  74          {
  75              return true;
  76          }
  77          else
  78          {
  79              return false;
  80          }
  81      }
  82  
  83      /**
  84       * Set the language for an area.
  85       *
  86       * @param string $language The language to use.
  87       * @param string $area The area to set the language for.
  88       */
  89  	function set_language($language="", $area="user")
  90      {
  91          global $mybb;
  92  
  93          $language = preg_replace("#[^a-z0-9\-_]#i", "", $language);
  94  
  95          // Use the board's default language
  96          if($language == "")
  97          {
  98              $language = $mybb->settings['bblanguage'];
  99          }
 100  
 101          // Check if the language exists.
 102          if(!$this->language_exists($language))
 103          {
 104              die("Language $language ($this->path/$language) is not installed");
 105          }
 106  
 107          $this->language = $language;
 108          require $this->path."/".$language.".php";
 109          $this->settings = $langinfo;
 110  
 111          // Load the admin language files as well, if needed.
 112          if($area == "admin")
 113          {
 114              if(!is_dir($this->path."/".$language."/{$area}"))
 115              {
 116                  if(!is_dir($this->path."/".$mybb->settings['cplanguage']."/{$area}"))
 117                  {
 118                      if(!is_dir($this->path."/english/{$area}"))
 119                      {
 120                          die("Your forum does not contain an Administration set. Please reupload the english language administration pack.");
 121                      }
 122                      else
 123                      {
 124                          $language = "english";
 125                      }
 126                  }
 127                  else
 128                  {
 129                      $language = $mybb->settings['cplanguage'];
 130                  }
 131              }
 132              $this->language = $language."/{$area}";
 133              $this->fallback = $this->fallbackLanguage."/{$area}";
 134          }
 135      }
 136  
 137      /**
 138       * Load the language variables for a section.
 139       *
 140       * @param string $section The section name.
 141       * @param boolean $forceuserarea Should use the user area even if in admin? For example for datahandlers
 142       * @param boolean $supress_error supress the error if the file doesn't exist?
 143       */
 144  	function load($section, $forceuserarea=false, $supress_error=false)
 145      {
 146          $language = $this->language;
 147          $fallback = $this->fallback;
 148  
 149          if($forceuserarea === true)
 150          {
 151              $language = str_replace('/admin', '', $language);
 152              $fallback = str_replace('/admin', '', $fallback);
 153          }
 154  
 155          $lfile = $this->path."/".$language."/".$section.".lang.php";
 156          $ffile = $this->path."/".$fallback."/".$section.".lang.php";
 157  
 158          if(file_exists($lfile))
 159          {
 160              require $lfile;
 161          }
 162          elseif(file_exists($ffile))
 163          {
 164              require $ffile;
 165          }
 166          else
 167          {
 168              if($supress_error != true)
 169              {
 170                  die("$lfile does not exist");
 171              }
 172          }
 173  
 174          // We must unite and protect our language variables!
 175          $lang_keys_ignore = array('language', 'fallback', 'fallbackLanguage', 'path', 'settings');
 176  
 177          if(isset($l) && is_array($l))
 178          {
 179              foreach($l as $key => $val)
 180              {
 181                  if((empty($this->$key) || $this->$key != $val) && !in_array($key, $lang_keys_ignore))
 182                  {
 183                      $this->$key = $val;
 184                  }
 185              }
 186          }
 187      }
 188  
 189      /**
 190       * @param string $string
 191       *
 192       * @return string
 193       */
 194  	function sprintf($string)
 195      {
 196          $arg_list = func_get_args();
 197          $num_args = count($arg_list);
 198  
 199          for($i = 1; $i < $num_args; $i++)
 200          {
 201              $string = str_replace('{'.$i.'}', $arg_list[$i], $string);
 202          }
 203  
 204          return $string;
 205      }
 206  
 207      /**
 208       * Get the language variables for a section.
 209       *
 210       * @param boolean $admin Admin variables when true, user when false.
 211       * @return array The language variables.
 212       */
 213  	function get_languages($admin=false)
 214      {
 215          $dir = @opendir($this->path);
 216          while($lang = readdir($dir))
 217          {
 218              $ext = my_strtolower(get_extension($lang));
 219              if($lang != "." && $lang != ".." && $ext == "php")
 220              {
 221                  $lname = str_replace(".".$ext, "", $lang);
 222                  require $this->path."/".$lang;
 223                  if(!$admin || ($admin && $langinfo['admin']))
 224                  {
 225                      $languages[$lname] = $langinfo['name'];
 226                  }
 227              }
 228          }
 229          @ksort($languages);
 230          return $languages;
 231      }
 232  
 233      /**
 234       * Parse contents for language variables.
 235       *
 236       * @param string $contents The contents to parse.
 237       * @return string The parsed contents.
 238       */
 239  	function parse($contents)
 240      {
 241          $contents = preg_replace_callback("#<lang:([a-zA-Z0-9_]+)>#", array($this, 'parse_replace'), $contents);
 242          return $contents;
 243      }
 244  
 245      /**
 246       * Replace content with language variable.
 247       *
 248       * @param array $matches Matches.
 249       * @return string Language variable.
 250       */
 251  	function parse_replace($matches)
 252      {
 253          return $this->{$matches[1]};
 254      }
 255  }


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref