[ Index ]

PHP Cross Reference of MyBB 1.8.27

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


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