includes/lang.php
author Dan
Sun, 30 Mar 2008 07:24:48 -0700
changeset 514 ecbfb747743e
parent 507 586fd7d3202d
child 516 9a52679143db
permissions -rw-r--r--
Added debugging switch to $lang->import() for help with optimization
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
     1
<?php
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
     2
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
     3
/*
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
     4
 * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
     5
 * Version 1.1.1
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
     6
 * Copyright (C) 2006-2007 Dan Fuhry
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
     7
 *
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
     8
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
     9
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    10
 *
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    11
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    12
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    13
 */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    14
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    15
/**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    16
 * Language class - processes, stores, and retrieves language strings.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    17
 * @package Enano
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    18
 * @subpackage Localization
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    19
 * @copyright 2007 Dan Fuhry
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    20
 * @license GNU General Public License
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    21
 */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    22
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    23
class Language
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    24
{
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    25
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    26
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    27
   * The numerical ID of the loaded language.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    28
   * @var int
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    29
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    30
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    31
  var $lang_id;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    32
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    33
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    34
   * The ISO-639-3 code for the loaded language. This should be grabbed directly from the database.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    35
   * @var string
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    36
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    37
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    38
  var $lang_code;
210
2b283402e4e4 Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
parents: 209
diff changeset
    39
2b283402e4e4 Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
parents: 209
diff changeset
    40
  /**
2b283402e4e4 Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
parents: 209
diff changeset
    41
   * Used to track when a language was last changed, to allow browsers to cache language data
2b283402e4e4 Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
parents: 209
diff changeset
    42
   * @var int
2b283402e4e4 Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
parents: 209
diff changeset
    43
   */
2b283402e4e4 Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
parents: 209
diff changeset
    44
  
2b283402e4e4 Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
parents: 209
diff changeset
    45
  var $lang_timestamp;
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    46
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    47
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    48
   * Will be an object that holds an instance of the class configured with the site's default language. Only instanciated when needed.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    49
   * @var object
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    50
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    51
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    52
  var $default;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    53
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    54
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    55
   * The list of loaded strings.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    56
   * @var array
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    57
   * @access private
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    58
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    59
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    60
  var $strings = array();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    61
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    62
  /**
504
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
    63
   * Switch for debug mode. If true, will show an asterisk after localized strings. This
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
    64
   * can be useful if you're localizing a component and need to see what's already done.
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
    65
   * @var bool
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
    66
   */
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
    67
  
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
    68
  var $debug = false;
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
    69
  
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
    70
  /**
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    71
   * Constructor.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    72
   * @param int|string Language ID or code to load.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    73
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    74
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    75
  function __construct($lang)
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    76
  {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    77
    global $db, $session, $paths, $template, $plugins; // Common objects
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    78
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    79
    if ( defined('IN_ENANO_INSTALL') )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    80
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    81
      // 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.
243
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
    82
      $this->lang_id   = 1;
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
    83
      $this->lang_code = $lang;
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    84
      return true;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    85
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    86
    if ( is_string($lang) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    87
    {
371
dc6026376919 Improved compatibility with PostgreSQL and fixed a number of installer bugs; fixed missing "meta" category declaration in language files
Dan
parents: 355
diff changeset
    88
      $sql_col = 'lang_code=\'' . $db->escape($lang) . '\'';
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    89
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    90
    else if ( is_int($lang) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    91
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    92
      $sql_col = 'lang_id=' . $lang . '';
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    93
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    94
    else
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    95
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    96
      $db->_die('lang.php - attempting to pass invalid value to constructor');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    97
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
    98
    
239
0f1b353570a7 Fix a comparison logic SQL error in lang.php; fix attempt to call mysql_real_escape_string() in install without a working DB connection
Dan
parents: 215
diff changeset
    99
    $lang_default = ( $x = getConfig('default_language') ) ? intval($x) : '\'def\'';
372
5bd429428101 A number of scattered changes. Profiler added and only enabled in debug mode (currently on), but awfully useful for fixing performance in the future. Started work on Admin:LangManager
Dan
parents: 371
diff changeset
   100
    
5bd429428101 A number of scattered changes. Profiler added and only enabled in debug mode (currently on), but awfully useful for fixing performance in the future. Started work on Admin:LangManager
Dan
parents: 371
diff changeset
   101
    $q = $db->sql_query("SELECT lang_id, lang_code, last_changed, ( lang_id = $lang_default ) AS is_default FROM " . table_prefix . "language WHERE $sql_col OR lang_id = $lang_default ORDER BY is_default ASC LIMIT 1;");
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   102
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   103
    if ( !$q )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   104
      $db->_die('lang.php - main select query');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   105
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   106
    if ( $db->numrows() < 1 )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   107
      $db->_die('lang.php - There are no languages installed');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   108
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   109
    $row = $db->fetchrow();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   110
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   111
    $this->lang_id   = intval( $row['lang_id'] );
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   112
    $this->lang_code = $row['lang_code'];
210
2b283402e4e4 Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
parents: 209
diff changeset
   113
    $this->lang_timestamp = $row['last_changed'];
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   114
  }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   115
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   116
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   117
   * Fetches language strings from the database, or a cache file if it's available.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   118
   * @param bool If true (default), allows the cache to be used.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   119
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   120
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   121
  function fetch($allow_cache = true)
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   122
  {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   123
    global $db, $session, $paths, $template, $plugins; // Common objects
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   124
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   125
    $lang_file = ENANO_ROOT . "/cache/lang_{$this->lang_id}.php";
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   126
    // Attempt to load the strings from a cache file
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   127
    if ( file_exists($lang_file) && $allow_cache )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   128
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   129
      // Yay! found it
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   130
      $this->load_cache_file($lang_file);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   131
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   132
    else
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   133
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   134
      // No cache file - select and retrieve from the database
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   135
      $q = $db->sql_unbuffered_query("SELECT string_category, string_name, string_content FROM " . table_prefix . "language_strings WHERE lang_id = {$this->lang_id};");
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   136
      if ( !$q )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   137
        $db->_die('lang.php - selecting language string data');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   138
      if ( $row = $db->fetchrow() )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   139
      {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   140
        $strings = array();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   141
        do
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   142
        {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   143
          $cat =& $row['string_category'];
371
dc6026376919 Improved compatibility with PostgreSQL and fixed a number of installer bugs; fixed missing "meta" category declaration in language files
Dan
parents: 355
diff changeset
   144
          if ( !is_array(@$strings[$cat]) )
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   145
          {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   146
            $strings[$cat] = array();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   147
          }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   148
          $strings[$cat][ $row['string_name'] ] = $row['string_content'];
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   149
        }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   150
        while ( $row = $db->fetchrow() );
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   151
        // all done fetching
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   152
        $this->merge($strings);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   153
      }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   154
      else
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   155
      {
241
c671f3bb8aed Trying to get lang import to work in the installer; it's not working ATM - cache file is generated with lang_id = 0. Syncing to Nighthawk.
Dan
parents: 239
diff changeset
   156
        if ( !defined('ENANO_ALLOW_LOAD_NOLANG') )
c671f3bb8aed Trying to get lang import to work in the installer; it's not working ATM - cache file is generated with lang_id = 0. Syncing to Nighthawk.
Dan
parents: 239
diff changeset
   157
          $db->_die('lang.php - No strings for language ' . $this->lang_code);
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   158
      }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   159
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   160
  }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   161
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   162
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   163
   * Loads a file from the disk cache (treated as PHP) and merges it into RAM.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   164
   * @param string File to load
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   165
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   166
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   167
  function load_cache_file($file)
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   168
  {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   169
    global $db, $session, $paths, $template, $plugins; // Common objects
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   170
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   171
    // We're using eval() here because it makes handling scope easier.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   172
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   173
    if ( !file_exists($file) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   174
      $db->_die('lang.php - requested cache file doesn\'t exist');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   175
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   176
    $contents = file_get_contents($file);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   177
    $contents = preg_replace('/([\s]*)<\?php/', '', $contents);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   178
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   179
    @eval($contents);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   180
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   181
    if ( !isset($lang_cache) || ( isset($lang_cache) && !is_array($lang_cache) ) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   182
      $db->_die('lang.php - the cache file is invalid (didn\'t set $lang_cache as an array)');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   183
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   184
    $this->merge($lang_cache);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   185
  }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   186
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   187
  /**
243
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   188
   * Loads a JSON language file and parses the strings into RAM. Will use the cache if possible, but stays far away from the database,
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   189
   * which we assume doesn't exist yet.
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   190
   */
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   191
  
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   192
  function load_file($file)
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   193
  {
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   194
    global $db, $session, $paths, $template, $plugins; // Common objects
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   195
    
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   196
    if ( !file_exists($file) )
348
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   197
    {
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   198
      if ( defined('IN_ENANO_INSTALL') )
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   199
      {
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   200
        die('lang.php - requested JSON file (' . htmlspecialchars($file) . ') doesn\'t exist');
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   201
      }
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   202
      else
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   203
      {
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   204
        $db->_die('lang.php - requested JSON file doesn\'t exist');
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   205
      }
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   206
    }
243
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   207
    
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   208
    $contents = trim(@file_get_contents($file));
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   209
    if ( empty($contents) )
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   210
      $db->_die('lang.php - empty language file...');
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   211
    
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   212
    // Trim off all text before and after the starting and ending braces
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   213
    $contents = preg_replace('/^([^{]+)\{/', '{', $contents);
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   214
    $contents = preg_replace('/\}([^}]+)$/', '}', $contents);
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   215
    $contents = trim($contents);
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   216
    
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   217
    if ( empty($contents) )
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   218
      $db->_die('lang.php - no meat to the language file...');
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   219
    
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   220
    $checksum = md5($contents);
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   221
    if ( file_exists("./cache/lang_json_{$checksum}.php") )
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   222
    {
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   223
      $this->load_cache_file("./cache/lang_json_{$checksum}.php");
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   224
    }
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   225
    else
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   226
    {
348
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   227
      // Correct syntax to be nice to the json parser
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   228
    
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   229
      // eliminate comments
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   230
      $contents = preg_replace(array(
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   231
              // eliminate single line comments in '// ...' form
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   232
              '#^\s*//(.+)$#m',
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   233
              // eliminate multi-line comments in '/* ... */' form, at start of string
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   234
              '#^\s*/\*(.+)\*/#Us',
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   235
              // eliminate multi-line comments in '/* ... */' form, at end of string
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   236
              '#/\*(.+)\*/\s*$#Us'
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   237
            ), '', $contents);
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   238
      
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   239
      $contents = preg_replace('/([,\{\[])([\s]*?)([a-z0-9_]+)([\s]*?):/', '\\1\\2"\\3" :', $contents);
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   240
      
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   241
      try
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   242
      {
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   243
        $langdata = enano_json_decode($contents);
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   244
      }
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   245
      catch(Zend_Json_Exception $e)
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   246
      {
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   247
        $db->_die('lang.php - Exception caught by JSON parser</p><pre>' . htmlspecialchars(print_r($e, true)) . '</pre><p>');
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   248
        exit;
87e08a6e4fec Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
parents: 345
diff changeset
   249
      }
243
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   250
    
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   251
      if ( !is_array($langdata) )
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   252
        $db->_die('lang.php - invalid language file');
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   253
      
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   254
      if ( !isset($langdata['categories']) || !isset($langdata['strings']) )
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   255
        $db->_die('lang.php - language file does not contain the proper items');
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   256
      
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   257
      $this->merge($langdata['strings']);
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   258
      
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   259
      $lang_file = "./cache/lang_json_{$checksum}.php";
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   260
      
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   261
      $handle = @fopen($lang_file, 'w');
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   262
      if ( !$handle )
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   263
        // Couldn't open the file. Silently fail and let the strings come from RAM.
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   264
        return false;
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   265
        
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   266
      // The file's open, that means we should be good.
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   267
      fwrite($handle, '<?php
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   268
// This file was generated automatically by Enano. You should not edit this file because any changes you make
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   269
// to it will not be visible in the ACP and all changes will be lost upon any changes to strings in the admin panel.
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   270
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   271
$lang_cache = ');
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   272
      
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   273
      $exported = $this->var_export_string($this->strings);
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   274
      if ( empty($exported) )
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   275
        // Ehh, that's not good
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   276
        $db->_die('lang.php - load_file(): var_export_string() failed');
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   277
      
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   278
      fwrite($handle, $exported . '; ?>');
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   279
      
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   280
      // Clean up
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   281
      unset($exported, $langdata);
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   282
      
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   283
      // Done =)
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   284
      fclose($handle);
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   285
    }
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   286
  }
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   287
  
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   288
  /**
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   289
   * Merges a standard language assoc array ($arr[cat][stringid]) with the master in RAM.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   290
   * @param array
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   291
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   292
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   293
  function merge($strings)
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   294
  {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   295
    // This is stupidly simple.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   296
    foreach ( $strings as $cat_id => $contents )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   297
    {
243
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   298
      if ( !isset($this->strings[$cat_id]) || ( isset($this->strings[$cat_id]) && !is_array($this->strings[$cat_id]) ) )
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   299
        $this->strings[$cat_id] = array();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   300
      foreach ( $contents as $string_id => $string )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   301
      {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   302
        $this->strings[$cat_id][$string_id] = $string;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   303
      }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   304
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   305
  }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   306
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   307
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   308
   * Imports a JSON-format language file into the database and merges with current strings.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   309
   * @param string Path to the JSON file to load
514
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   310
   * @param bool Enable debugging output, makes the process over CLI more interesting
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   311
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   312
  
514
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   313
  function import($file, $debug = false)
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   314
  {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   315
    global $db, $session, $paths, $template, $plugins; // Common objects
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   316
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   317
    if ( !file_exists($file) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   318
      $db->_die('lang.php - can\'t import language file: string file doesn\'t exist');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   319
    
241
c671f3bb8aed Trying to get lang import to work in the installer; it's not working ATM - cache file is generated with lang_id = 0. Syncing to Nighthawk.
Dan
parents: 239
diff changeset
   320
    if ( $this->lang_id == 0 )
c671f3bb8aed Trying to get lang import to work in the installer; it's not working ATM - cache file is generated with lang_id = 0. Syncing to Nighthawk.
Dan
parents: 239
diff changeset
   321
      $db->_die('lang.php - BUG: trying to perform import when $lang->lang_id == 0');
c671f3bb8aed Trying to get lang import to work in the installer; it's not working ATM - cache file is generated with lang_id = 0. Syncing to Nighthawk.
Dan
parents: 239
diff changeset
   322
    
514
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   323
    if ( $debug )
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   324
      echo "Importing file: $file\n  Checking file...\n";
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   325
    
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   326
    $contents = trim(@file_get_contents($file));
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   327
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   328
    if ( empty($contents) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   329
      $db->_die('lang.php - can\'t load the contents of the language file');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   330
    
514
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   331
    if ( $debug )
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   332
      echo "  Cleaning up JSON\n";
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   333
    
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   334
    // Trim off all text before and after the starting and ending braces
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   335
    $contents = preg_replace('/^([^{]+)\{/', '{', $contents);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   336
    $contents = preg_replace('/\}([^}]+)$/', '}', $contents);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   337
    
334
c72b545f1304 More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
parents: 243
diff changeset
   338
    // Correct syntax to be nice to the json parser
372
5bd429428101 A number of scattered changes. Profiler added and only enabled in debug mode (currently on), but awfully useful for fixing performance in the future. Started work on Admin:LangManager
Dan
parents: 371
diff changeset
   339
    $contents = enano_clean_json($contents);
334
c72b545f1304 More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
parents: 243
diff changeset
   340
    
514
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   341
    if ( $debug )
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   342
      echo "  Decoding JSON stream\n";
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   343
    
334
c72b545f1304 More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
parents: 243
diff changeset
   344
    try
c72b545f1304 More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
parents: 243
diff changeset
   345
    {
c72b545f1304 More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
parents: 243
diff changeset
   346
      $langdata = enano_json_decode($contents);
c72b545f1304 More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
parents: 243
diff changeset
   347
    }
c72b545f1304 More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
parents: 243
diff changeset
   348
    catch(Zend_Json_Exception $e)
c72b545f1304 More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
parents: 243
diff changeset
   349
    {
335
67bd3121a12e Replaced TinyMCE 2.x with 3.0 beta 3. Supports everything but IE. Also rewrote the editor interface completely from the ground up.
Dan
parents: 334
diff changeset
   350
      $db->_die('lang.php - Exception caught by JSON parser</p><pre>' . htmlspecialchars(print_r($e, true)) . '</pre><p>');
334
c72b545f1304 More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
parents: 243
diff changeset
   351
      exit;
c72b545f1304 More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
parents: 243
diff changeset
   352
    }
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   353
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   354
    if ( !is_array($langdata) )
334
c72b545f1304 More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
parents: 243
diff changeset
   355
    {
c72b545f1304 More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
parents: 243
diff changeset
   356
      $db->_die('lang.php - invalid or non-well-formed language file');
c72b545f1304 More localization work. Resolved major issue with JSON parser not parsing files over ~50KB. Switched JSON parser to the one from the Zend Framework (BSD licensed). Forced to split enano.json into five different files.
Dan
parents: 243
diff changeset
   357
    }
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   358
    
514
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   359
    if ( $debug )
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   360
      echo "  Starting string import\n";
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   361
    
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   362
    return $this->import_array($langdata, $debug);
504
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   363
  }
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   364
  
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   365
  /**
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   366
   * Imports a JSON-format language file into the database and merges with current strings.
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   367
   * @param string Path to plugin file
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   368
   */
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   369
  
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   370
  function import_plugin($file)
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   371
  {
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   372
    global $db, $session, $paths, $template, $plugins; // Common objects
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   373
    
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   374
    if ( !file_exists($file) )
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   375
      $db->_die('lang.php - can\'t import language file: string file doesn\'t exist');
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   376
    
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   377
    if ( $this->lang_id == 0 )
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   378
      $db->_die('lang.php - BUG: trying to perform import when $lang->lang_id == 0');
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   379
    
507
586fd7d3202d Fixed some stray version numbers (again!); added support for Diffie-Hellman logins in the normal login form (not AJAX) - even works in IE
Dan
parents: 504
diff changeset
   380
    $block = pluginLoader::parse_plugin_blocks($file, 'language');
586fd7d3202d Fixed some stray version numbers (again!); added support for Diffie-Hellman logins in the normal login form (not AJAX) - even works in IE
Dan
parents: 504
diff changeset
   381
    if ( !is_array($block) )
586fd7d3202d Fixed some stray version numbers (again!); added support for Diffie-Hellman logins in the normal login form (not AJAX) - even works in IE
Dan
parents: 504
diff changeset
   382
      return false;
586fd7d3202d Fixed some stray version numbers (again!); added support for Diffie-Hellman logins in the normal login form (not AJAX) - even works in IE
Dan
parents: 504
diff changeset
   383
    if ( !isset($block[0]) )
586fd7d3202d Fixed some stray version numbers (again!); added support for Diffie-Hellman logins in the normal login form (not AJAX) - even works in IE
Dan
parents: 504
diff changeset
   384
      return false;
504
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   385
    
507
586fd7d3202d Fixed some stray version numbers (again!); added support for Diffie-Hellman logins in the normal login form (not AJAX) - even works in IE
Dan
parents: 504
diff changeset
   386
    $contents =& $block[0]['value'];
504
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   387
    
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   388
    // Trim off all text before and after the starting and ending braces
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   389
    $contents = preg_replace('/^([^{]+)\{/', '{', $contents);
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   390
    $contents = preg_replace('/\}([^}]+)$/', '}', $contents);
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   391
    
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   392
    // Correct syntax to be nice to the json parser
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   393
    $contents = enano_clean_json($contents);
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   394
    
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   395
    try
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   396
    {
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   397
      $langdata = enano_json_decode($contents);
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   398
    }
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   399
    catch(Zend_Json_Exception $e)
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   400
    {
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   401
      $db->_die('lang.php - Exception caught by JSON parser</p><pre>' . htmlspecialchars(print_r($e, true)) . '</pre><p>');
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   402
      exit;
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   403
    }
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   404
    
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   405
    if ( !is_array($langdata) )
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   406
    {
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   407
      $db->_die('lang.php - invalid or non-well-formed language file');
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   408
    }
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   409
    
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   410
    // Does the plugin support the current language?
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   411
    if ( isset($langdata[$this->lang_code]) )
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   412
    {
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   413
      // Yes, import that
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   414
      return $this->import_array($langdata[$this->lang_code]);
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   415
    }
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   416
    
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   417
    // Just import the first language we run across.
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   418
    $supported_langs = array_keys($langdata);
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   419
    
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   420
    if ( !isset($supported_langs[0]) )
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   421
    {
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   422
      $db->_die('lang.php - plugin has an invalid or corrupt language block');
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   423
    }
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   424
    
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   425
    $first_lang = $supported_langs[0];
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   426
    
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   427
    return $this->import_array($langdata[$first_lang]);
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   428
  }
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   429
  
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   430
  /**
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   431
   * Performs the actual import of string data.
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   432
   * @param array Parsed JSON object, should be in the form of an array
514
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   433
   * @param bool Enable debugging output
504
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   434
   * @access private
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   435
   */
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   436
  
514
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   437
  protected function import_array($langdata, $debug = false) 
504
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   438
  {
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   439
    global $db, $session, $paths, $template, $plugins; // Common objects
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   440
    
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   441
    if ( !isset($langdata['categories']) || !isset($langdata['strings']) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   442
      $db->_die('lang.php - language file does not contain the proper items');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   443
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   444
    $insert_list = array();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   445
    $delete_list = array();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   446
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   447
    foreach ( $langdata['categories'] as $category )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   448
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   449
      if ( isset($langdata['strings'][$category]) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   450
      {
514
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   451
        if ( $debug )
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   452
        {
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   453
          $desc = ( isset($langdata['strings']['meta'][$category]) ) ? $langdata['strings']['meta'][$category] : $this->get("meta_$category");
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   454
          echo "  Indexing category: $category ({$desc})\n";
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   455
        }
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   456
        foreach ( $langdata['strings'][$category] as $string_name => $string_value )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   457
        {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   458
          $string_name = $db->escape($string_name);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   459
          $string_value = $db->escape($string_value);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   460
          $category_name = $db->escape($category);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   461
          $insert_list[] = "({$this->lang_id}, '$category_name', '$string_name', '$string_value')";
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   462
          $delete_list[] = "( lang_id = {$this->lang_id} AND string_category = '$category_name' AND string_name = '$string_name' )";
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   463
        }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   464
      }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   465
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   466
    
514
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   467
    if ( $debug )
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   468
    {
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   469
      echo "  Running deletion of old strings...";
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   470
      $start = microtime_float();
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   471
    }
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   472
    $delete_list = implode(" OR\n  ", $delete_list);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   473
    
377
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   474
    if ( !empty($delete_list) )
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   475
    {
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   476
      $sql = "DELETE FROM " . table_prefix . "language_strings WHERE $delete_list;";
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   477
      
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   478
      // Free some memory
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   479
      unset($delete_list);
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   480
      
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   481
      // Run the query
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   482
      $q = $db->sql_query($sql);
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   483
      if ( !$q )
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   484
        $db->_die('lang.php - couldn\'t kill off them old strings');
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   485
    }
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   486
    
514
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   487
    if ( $debug )
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   488
    {
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   489
      $time = round(microtime_float() - $start, 5);
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   490
      echo "({$time}s)\n";
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   491
    }
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   492
    
377
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   493
    if ( !empty($insert_list) )
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   494
    {
514
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   495
      if ( $debug )
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   496
      {
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   497
        echo "  Inserting strings...";
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   498
        $start = microtime_float();
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   499
      }
377
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   500
      $insert_list = implode(",\n  ", $insert_list);
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   501
      $sql = "INSERT INTO " . table_prefix . "language_strings(lang_id, string_category, string_name, string_content) VALUES\n  $insert_list;";
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   502
      
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   503
      // Free some memory
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   504
      unset($insert_list);
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   505
      
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   506
      // Run the query
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   507
      $q = $db->sql_query($sql);
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   508
      if ( !$q )
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   509
        $db->_die('lang.php - couldn\'t insert strings in import()');
514
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   510
      
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   511
      if ( $debug )
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   512
      {
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   513
        $time = round(microtime_float() - $start, 5);
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   514
        echo "({$time}s)\n";
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   515
      }
377
bb3e6c3bd4f4 Removed stray debugging info from ACL editor success notification; added ability for guests to set language on URI (?lang=eng); added html_in_pages ACL type and separated from php_in_pages so HTML can be embedded but not PHP; rewote portions of the path manager to better abstract URL input; added Zend Framework into list of BSD-licensed libraries; localized some remaining strings; got the migration script working, but just barely; fixed display bug in Special:Contributions; localized Main Page button in admin panel
Dan
parents: 376
diff changeset
   516
    }
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   517
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   518
    // YAY! done!
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   519
    // This will regenerate the cache file if possible.
514
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   520
    if ( $debug )
ecbfb747743e Added debugging switch to $lang->import() for help with optimization
Dan
parents: 507
diff changeset
   521
      echo "  Regenerating cache file\n";
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   522
    $this->regen_caches();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   523
  }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   524
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   525
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   526
   * Refetches the strings and writes out the cache file.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   527
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   528
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   529
  function regen_caches()
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   530
  {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   531
    global $db, $session, $paths, $template, $plugins; // Common objects
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   532
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   533
    $lang_file = ENANO_ROOT . "/cache/lang_{$this->lang_id}.php";
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   534
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   535
    // Refresh the strings in RAM to the latest copies in the DB
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   536
    $this->fetch(false);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   537
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   538
    $handle = @fopen($lang_file, 'w');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   539
    if ( !$handle )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   540
      // Couldn't open the file. Silently fail and let the strings come from the database.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   541
      return false;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   542
      
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   543
    // The file's open, that means we should be good.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   544
    fwrite($handle, '<?php
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   545
// This file was generated automatically by Enano. You should not edit this file because any changes you make
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   546
// to it will not be visible in the ACP and all changes will be lost upon any changes to strings in the admin panel.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   547
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   548
$lang_cache = ');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   549
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   550
    $exported = $this->var_export_string($this->strings);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   551
    if ( empty($exported) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   552
      // Ehh, that's not good
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   553
      $db->_die('lang.php - var_export_string() failed');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   554
    
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   555
    fwrite($handle, $exported . '; ?>');
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   556
    
210
2b283402e4e4 Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
parents: 209
diff changeset
   557
    // Update timestamp in database
2b283402e4e4 Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
parents: 209
diff changeset
   558
    $q = $db->sql_query('UPDATE ' . table_prefix . 'language SET last_changed = ' . time() . ' WHERE lang_id = ' . $this->lang_id . ';');
2b283402e4e4 Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
parents: 209
diff changeset
   559
    if ( !$q )
2b283402e4e4 Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
parents: 209
diff changeset
   560
      $db->_die('lang.php - updating timestamp on language');
2b283402e4e4 Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
parents: 209
diff changeset
   561
    
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   562
    // Done =)
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   563
    fclose($handle);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   564
  }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   565
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   566
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   567
   * Calls var_export() on whatever, and returns the function's output.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   568
   * @param mixed Whatever you want var_exported. Usually an array.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   569
   * @return string
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   570
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   571
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   572
  function var_export_string($val)
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   573
  {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   574
    ob_start();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   575
    var_export($val);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   576
    $contents = ob_get_contents();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   577
    ob_end_clean();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   578
    return $contents;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   579
  }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   580
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   581
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   582
   * Fetches a language string from the cache in RAM. If it isn't there, it will call fetch() again and then try. If it still can't find it, it will ask for the string
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   583
   * in the default language. If even then the string can't be found, this function will return what was passed to it.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   584
   *
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   585
   * This will also templatize strings. If a string contains variables in the format %foo%, you may specify the second parameter as an associative array in the format
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   586
   * of 'foo' => 'foo substitute'.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   587
   *
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   588
   * @param string ID of the string to fetch. This will always be in the format of category_stringid.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   589
   * @param array Optional. Associative array of substitutions.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   590
   * @return string
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   591
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   592
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   593
  function get($string_id, $substitutions = false)
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   594
  {
376
66732bd4532c Finished (or nearly finished) the admin language CP
Dan
parents: 372
diff changeset
   595
    if ( !is_array($substitutions) )
66732bd4532c Finished (or nearly finished) the admin language CP
Dan
parents: 372
diff changeset
   596
      $substitutions = array();
66732bd4532c Finished (or nearly finished) the admin language CP
Dan
parents: 372
diff changeset
   597
    return $this->substitute($this->get_uncensored($string_id), $substitutions);
66732bd4532c Finished (or nearly finished) the admin language CP
Dan
parents: 372
diff changeset
   598
  }
66732bd4532c Finished (or nearly finished) the admin language CP
Dan
parents: 372
diff changeset
   599
  
66732bd4532c Finished (or nearly finished) the admin language CP
Dan
parents: 372
diff changeset
   600
  /**
66732bd4532c Finished (or nearly finished) the admin language CP
Dan
parents: 372
diff changeset
   601
   * The same as get(), but does not perform any substitution or filtering. Used in get() (of course) and in the admin panel, where
66732bd4532c Finished (or nearly finished) the admin language CP
Dan
parents: 372
diff changeset
   602
   * strings are updated only if they were changed.
66732bd4532c Finished (or nearly finished) the admin language CP
Dan
parents: 372
diff changeset
   603
   *
66732bd4532c Finished (or nearly finished) the admin language CP
Dan
parents: 372
diff changeset
   604
   * @param string ID of the string to fetch. This will always be in the format of category_stringid.
66732bd4532c Finished (or nearly finished) the admin language CP
Dan
parents: 372
diff changeset
   605
   * @param array Optional. Associative array of substitutions.
66732bd4532c Finished (or nearly finished) the admin language CP
Dan
parents: 372
diff changeset
   606
   * @return string
66732bd4532c Finished (or nearly finished) the admin language CP
Dan
parents: 372
diff changeset
   607
   */
66732bd4532c Finished (or nearly finished) the admin language CP
Dan
parents: 372
diff changeset
   608
  
66732bd4532c Finished (or nearly finished) the admin language CP
Dan
parents: 372
diff changeset
   609
  function get_uncensored($string_id, $substitutions = false)
66732bd4532c Finished (or nearly finished) the admin language CP
Dan
parents: 372
diff changeset
   610
  {
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   611
    // Extract the category and string ID
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   612
    $category = substr($string_id, 0, ( strpos($string_id, '_') ));
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   613
    $string_name = substr($string_id, ( strpos($string_id, '_') + 1 ));
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   614
    $found = false;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   615
    if ( isset($this->strings[$category]) && isset($this->strings[$category][$string_name]) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   616
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   617
      $found = true;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   618
      $string = $this->strings[$category][$string_name];
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   619
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   620
    if ( !$found )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   621
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   622
      // Ehh, the string wasn't found. Rerun fetch() and try again.
243
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   623
      if ( defined('IN_ENANO_INSTALL') )
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   624
      {
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   625
        return $string_id;
a7d0f2711df1 Installer localization started. Welcome, License, and SysReqs pages are fully localized.
Dan
parents: 241
diff changeset
   626
      }
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   627
      $this->fetch();
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   628
      if ( isset($this->strings[$category]) && isset($this->strings[$category][$string_name]) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   629
      {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   630
        $found = true;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   631
        $string = $this->strings[$category][$string_name];
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   632
      }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   633
      if ( !$found )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   634
      {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   635
        // STILL not found. Check the default language.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   636
        $lang_default = ( $x = getConfig('default_language') ) ? intval($x) : $this->lang_id;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   637
        if ( $lang_default != $this->lang_id )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   638
        {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   639
          if ( !is_object($this->default) )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   640
            $this->default = new Language($lang_default);
376
66732bd4532c Finished (or nearly finished) the admin language CP
Dan
parents: 372
diff changeset
   641
          return $this->default->get_uncensored($string_id);
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   642
        }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   643
      }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   644
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   645
    if ( !$found )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   646
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   647
      // Alright, it's nowhere. Return the input, grumble grumble...
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   648
      return $string_id;
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   649
    }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   650
    // Found it!
376
66732bd4532c Finished (or nearly finished) the admin language CP
Dan
parents: 372
diff changeset
   651
    return $string;
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   652
  }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   653
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   654
  /**
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   655
   * Processes substitutions.
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   656
   * @param string
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   657
   * @param array
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   658
   * @return string
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   659
   */
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   660
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   661
  function substitute($string, $subs)
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   662
  {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   663
    preg_match_all('/%this\.([a-z0-9_]+)%/', $string, $matches);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   664
    if ( count($matches[0]) > 0 )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   665
    {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   666
      foreach ( $matches[1] as $i => $string_id )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   667
      {
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   668
        $result = $this->get($string_id);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   669
        $string = str_replace($matches[0][$i], $result, $string);
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   670
      }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   671
    }
209
8a00247d1dee Login page mostly localized
Dan
parents: 205
diff changeset
   672
    preg_match_all('/%config\.([a-z0-9_]+)%/', $string, $matches);
8a00247d1dee Login page mostly localized
Dan
parents: 205
diff changeset
   673
    if ( count($matches[0]) > 0 )
8a00247d1dee Login page mostly localized
Dan
parents: 205
diff changeset
   674
    {
8a00247d1dee Login page mostly localized
Dan
parents: 205
diff changeset
   675
      foreach ( $matches[1] as $i => $string_id )
8a00247d1dee Login page mostly localized
Dan
parents: 205
diff changeset
   676
      {
8a00247d1dee Login page mostly localized
Dan
parents: 205
diff changeset
   677
        $result = getConfig($string_id);
8a00247d1dee Login page mostly localized
Dan
parents: 205
diff changeset
   678
        $string = str_replace($matches[0][$i], $result, $string);
8a00247d1dee Login page mostly localized
Dan
parents: 205
diff changeset
   679
      }
8a00247d1dee Login page mostly localized
Dan
parents: 205
diff changeset
   680
    }
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   681
    foreach ( $subs as $key => $value )
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   682
    {
209
8a00247d1dee Login page mostly localized
Dan
parents: 205
diff changeset
   683
      $subs[$key] = strval($value);
8a00247d1dee Login page mostly localized
Dan
parents: 205
diff changeset
   684
      $string = str_replace("%{$key}%", "{$subs[$key]}", $string);
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   685
    }
504
bc8e0e9ee01d Added support for embedding language data into plugins; updated all version numbers on plugin files
Dan
parents: 391
diff changeset
   686
    return ( $this->debug ) ? "$string*" : $string;
205
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   687
  }
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   688
  
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   689
} // class Language
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   690
c4542792db2b Localization low-level framework added
Dan
parents:
diff changeset
   691
?>