includes/lang.php
changeset 243 a7d0f2711df1
parent 241 c671f3bb8aed
child 334 c72b545f1304
equal deleted inserted replaced
241:c671f3bb8aed 243:a7d0f2711df1
    69     global $db, $session, $paths, $template, $plugins; // Common objects
    69     global $db, $session, $paths, $template, $plugins; // Common objects
    70     
    70     
    71     if ( defined('IN_ENANO_INSTALL') )
    71     if ( defined('IN_ENANO_INSTALL') )
    72     {
    72     {
    73       // special case for the Enano installer: it will load its own strings from a JSON file and just use this API for fetching and templatizing them.
    73       // special case for the Enano installer: it will load its own strings from a JSON file and just use this API for fetching and templatizing them.
    74       $this->lang_id   = LANG_DEFAULT;
    74       $this->lang_id   = 1;
    75       $this->lang_code = 'neutral';
    75       $this->lang_code = $lang;
    76       return true;
    76       return true;
    77     }
    77     }
    78     if ( is_string($lang) )
    78     if ( is_string($lang) )
    79     {
    79     {
    80       $sql_col = 'lang_code="' . $db->escape($lang) . '"';
    80       $sql_col = 'lang_code="' . $db->escape($lang) . '"';
   184     
   184     
   185     $this->merge($lang_cache);
   185     $this->merge($lang_cache);
   186   }
   186   }
   187   
   187   
   188   /**
   188   /**
       
   189    * Loads a JSON language file and parses the strings into RAM. Will use the cache if possible, but stays far away from the database,
       
   190    * which we assume doesn't exist yet.
       
   191    */
       
   192   
       
   193   function load_file($file)
       
   194   {
       
   195     global $db, $session, $paths, $template, $plugins; // Common objects
       
   196     
       
   197     if ( !file_exists($file) )
       
   198       $db->_die('lang.php - requested JSON file doesn\'t exist');
       
   199     
       
   200     $contents = trim(@file_get_contents($file));
       
   201     if ( empty($contents) )
       
   202       $db->_die('lang.php - empty language file...');
       
   203     
       
   204     // Trim off all text before and after the starting and ending braces
       
   205     $contents = preg_replace('/^([^{]+)\{/', '{', $contents);
       
   206     $contents = preg_replace('/\}([^}]+)$/', '}', $contents);
       
   207     $contents = trim($contents);
       
   208     
       
   209     if ( empty($contents) )
       
   210       $db->_die('lang.php - no meat to the language file...');
       
   211     
       
   212     $checksum = md5($contents);
       
   213     if ( file_exists("./cache/lang_json_{$checksum}.php") )
       
   214     {
       
   215       $this->load_cache_file("./cache/lang_json_{$checksum}.php");
       
   216     }
       
   217     else
       
   218     {
       
   219       $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
       
   220       $langdata = $json->decode($contents);
       
   221     
       
   222       if ( !is_array($langdata) )
       
   223         $db->_die('lang.php - invalid language file');
       
   224       
       
   225       if ( !isset($langdata['categories']) || !isset($langdata['strings']) )
       
   226         $db->_die('lang.php - language file does not contain the proper items');
       
   227       
       
   228       $this->merge($langdata['strings']);
       
   229       
       
   230       $lang_file = "./cache/lang_json_{$checksum}.php";
       
   231       
       
   232       $handle = @fopen($lang_file, 'w');
       
   233       if ( !$handle )
       
   234         // Couldn't open the file. Silently fail and let the strings come from RAM.
       
   235         return false;
       
   236         
       
   237       // The file's open, that means we should be good.
       
   238       fwrite($handle, '<?php
       
   239 // This file was generated automatically by Enano. You should not edit this file because any changes you make
       
   240 // to it will not be visible in the ACP and all changes will be lost upon any changes to strings in the admin panel.
       
   241 
       
   242 $lang_cache = ');
       
   243       
       
   244       $exported = $this->var_export_string($this->strings);
       
   245       if ( empty($exported) )
       
   246         // Ehh, that's not good
       
   247         $db->_die('lang.php - load_file(): var_export_string() failed');
       
   248       
       
   249       fwrite($handle, $exported . '; ?>');
       
   250       
       
   251       // Clean up
       
   252       unset($exported, $langdata);
       
   253       
       
   254       // Done =)
       
   255       fclose($handle);
       
   256     }
       
   257   }
       
   258   
       
   259   /**
   189    * Merges a standard language assoc array ($arr[cat][stringid]) with the master in RAM.
   260    * Merges a standard language assoc array ($arr[cat][stringid]) with the master in RAM.
   190    * @param array
   261    * @param array
   191    */
   262    */
   192   
   263   
   193   function merge($strings)
   264   function merge($strings)
   194   {
   265   {
   195     // This is stupidly simple.
   266     // This is stupidly simple.
   196     foreach ( $strings as $cat_id => $contents )
   267     foreach ( $strings as $cat_id => $contents )
   197     {
   268     {
   198       if ( !is_array($this->strings[$cat_id]) )
   269       if ( !isset($this->strings[$cat_id]) || ( isset($this->strings[$cat_id]) && !is_array($this->strings[$cat_id]) ) )
   199         $this->strings[$cat_id] = array();
   270         $this->strings[$cat_id] = array();
   200       foreach ( $contents as $string_id => $string )
   271       foreach ( $contents as $string_id => $string )
   201       {
   272       {
   202         $this->strings[$cat_id][$string_id] = $string;
   273         $this->strings[$cat_id][$string_id] = $string;
   203       }
   274       }
   362       $string = $this->strings[$category][$string_name];
   433       $string = $this->strings[$category][$string_name];
   363     }
   434     }
   364     if ( !$found )
   435     if ( !$found )
   365     {
   436     {
   366       // Ehh, the string wasn't found. Rerun fetch() and try again.
   437       // Ehh, the string wasn't found. Rerun fetch() and try again.
       
   438       if ( defined('IN_ENANO_INSTALL') )
       
   439       {
       
   440         return $string_id;
       
   441       }
   367       $this->fetch();
   442       $this->fetch();
   368       if ( isset($this->strings[$category]) && isset($this->strings[$category][$string_name]) )
   443       if ( isset($this->strings[$category]) && isset($this->strings[$category][$string_name]) )
   369       {
   444       {
   370         $found = true;
   445         $found = true;
   371         $string = $this->strings[$category][$string_name];
   446         $string = $this->strings[$category][$string_name];
   426     foreach ( $subs as $key => $value )
   501     foreach ( $subs as $key => $value )
   427     {
   502     {
   428       $subs[$key] = strval($value);
   503       $subs[$key] = strval($value);
   429       $string = str_replace("%{$key}%", "{$subs[$key]}", $string);
   504       $string = str_replace("%{$key}%", "{$subs[$key]}", $string);
   430     }
   505     }
   431     return "L $string";
   506     return "{$string}*";
   432   }
   507   }
   433   
   508   
   434 } // class Language
   509 } // class Language
   435 
   510 
   436 ?>
   511 ?>