includes/lang.php
changeset 504 bc8e0e9ee01d
parent 391 85f91037cd4f
child 507 586fd7d3202d
equal deleted inserted replaced
503:f205f4b201ed 504:bc8e0e9ee01d
    58    */
    58    */
    59   
    59   
    60   var $strings = array();
    60   var $strings = array();
    61   
    61   
    62   /**
    62   /**
       
    63    * Switch for debug mode. If true, will show an asterisk after localized strings. This
       
    64    * can be useful if you're localizing a component and need to see what's already done.
       
    65    * @var bool
       
    66    */
       
    67   
       
    68   var $debug = false;
       
    69   
       
    70   /**
    63    * Constructor.
    71    * Constructor.
    64    * @param int|string Language ID or code to load.
    72    * @param int|string Language ID or code to load.
    65    */
    73    */
    66   
    74   
    67   function __construct($lang)
    75   function __construct($lang)
   336     if ( !is_array($langdata) )
   344     if ( !is_array($langdata) )
   337     {
   345     {
   338       $db->_die('lang.php - invalid or non-well-formed language file');
   346       $db->_die('lang.php - invalid or non-well-formed language file');
   339     }
   347     }
   340     
   348     
       
   349     return $this->import_array($langdata);
       
   350   }
       
   351   
       
   352   /**
       
   353    * Imports a JSON-format language file into the database and merges with current strings.
       
   354    * @param string Path to plugin file
       
   355    */
       
   356   
       
   357   function import_plugin($file)
       
   358   {
       
   359     global $db, $session, $paths, $template, $plugins; // Common objects
       
   360     
       
   361     if ( !file_exists($file) )
       
   362       $db->_die('lang.php - can\'t import language file: string file doesn\'t exist');
       
   363     
       
   364     if ( $this->lang_id == 0 )
       
   365       $db->_die('lang.php - BUG: trying to perform import when $lang->lang_id == 0');
       
   366     
       
   367     $contents = trim(@file_get_contents($file));
       
   368     
       
   369     if ( empty($contents) )
       
   370       $db->_die('lang.php - can\'t load the contents of the language file');
       
   371     
       
   372     // If there isn't a specially formed comment block, bail out quietly.
       
   373     if ( !strpos($contents, PLUGIN_METABLOCK_LANGUAGE_START) || !strpos($contents, PLUGIN_METABLOCK_LANGUAGE_END) )
       
   374       return null;
       
   375     
       
   376     // Get all data in the language block
       
   377     $block_start = strpos($contents, PLUGIN_METABLOCK_LANGUAGE_START) + strlen(PLUGIN_METABLOCK_LANGUAGE_START);
       
   378     $block_end = strpos($contents, PLUGIN_METABLOCK_LANGUAGE_END);
       
   379     $block_len = $block_end - $block_start;
       
   380     if ( $block_len < 1 )
       
   381       $db->_die('lang.php - plugin file contains corrupt language data');
       
   382     
       
   383     $contents = substr($contents, $block_start, $block_len);
       
   384     
       
   385     // Trim off all text before and after the starting and ending braces
       
   386     $contents = preg_replace('/^([^{]+)\{/', '{', $contents);
       
   387     $contents = preg_replace('/\}([^}]+)$/', '}', $contents);
       
   388     
       
   389     // Correct syntax to be nice to the json parser
       
   390     $contents = enano_clean_json($contents);
       
   391     
       
   392     try
       
   393     {
       
   394       $langdata = enano_json_decode($contents);
       
   395     }
       
   396     catch(Zend_Json_Exception $e)
       
   397     {
       
   398       $db->_die('lang.php - Exception caught by JSON parser</p><pre>' . htmlspecialchars(print_r($e, true)) . '</pre><p>');
       
   399       exit;
       
   400     }
       
   401     
       
   402     if ( !is_array($langdata) )
       
   403     {
       
   404       $db->_die('lang.php - invalid or non-well-formed language file');
       
   405     }
       
   406     
       
   407     // Does the plugin support the current language?
       
   408     if ( isset($langdata[$this->lang_code]) )
       
   409     {
       
   410       // Yes, import that
       
   411       return $this->import_array($langdata[$this->lang_code]);
       
   412     }
       
   413     
       
   414     // Just import the first language we run across.
       
   415     $supported_langs = array_keys($langdata);
       
   416     
       
   417     if ( !isset($supported_langs[0]) )
       
   418     {
       
   419       $db->_die('lang.php - plugin has an invalid or corrupt language block');
       
   420     }
       
   421     
       
   422     $first_lang = $supported_langs[0];
       
   423     
       
   424     return $this->import_array($langdata[$first_lang]);
       
   425   }
       
   426   
       
   427   /**
       
   428    * Performs the actual import of string data.
       
   429    * @param array Parsed JSON object, should be in the form of an array
       
   430    * @access private
       
   431    */
       
   432   
       
   433   protected function import_array($langdata) 
       
   434   {
       
   435     global $db, $session, $paths, $template, $plugins; // Common objects
       
   436     
   341     if ( !isset($langdata['categories']) || !isset($langdata['strings']) )
   437     if ( !isset($langdata['categories']) || !isset($langdata['strings']) )
   342       $db->_die('lang.php - language file does not contain the proper items');
   438       $db->_die('lang.php - language file does not contain the proper items');
   343     
   439     
   344     $insert_list = array();
   440     $insert_list = array();
   345     $delete_list = array();
   441     $delete_list = array();
   552     foreach ( $subs as $key => $value )
   648     foreach ( $subs as $key => $value )
   553     {
   649     {
   554       $subs[$key] = strval($value);
   650       $subs[$key] = strval($value);
   555       $string = str_replace("%{$key}%", "{$subs[$key]}", $string);
   651       $string = str_replace("%{$key}%", "{$subs[$key]}", $string);
   556     }
   652     }
   557     return $string;
   653     return ( $this->debug ) ? "$string*" : $string;
   558   }
   654   }
   559   
   655   
   560 } // class Language
   656 } // class Language
   561 
   657 
   562 ?>
   658 ?>