[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

/inc/ -> class_language.php (source)

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


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1