[ Index ]

PHP Cross Reference of MyBB 1.6.0

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


Generated: Tue Aug 3 20:35:36 2010 Cross-referenced by PHPXref 0.7