[ Index ]

PHP Cross Reference of MyBB 1.8.7

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


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