205
+ − 1
<?php
+ − 2
+ − 3
/*
+ − 4
* Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
+ − 5
* Version 1.1.1
+ − 6
* Copyright (C) 2006-2007 Dan Fuhry
+ − 7
*
+ − 8
* This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
+ − 9
* as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
+ − 10
*
+ − 11
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
+ − 12
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
+ − 13
*/
+ − 14
+ − 15
/**
+ − 16
* Language class - processes, stores, and retrieves language strings.
+ − 17
* @package Enano
+ − 18
* @subpackage Localization
+ − 19
* @copyright 2007 Dan Fuhry
+ − 20
* @license GNU General Public License
+ − 21
*/
+ − 22
+ − 23
class Language
+ − 24
{
+ − 25
+ − 26
/**
+ − 27
* The numerical ID of the loaded language.
+ − 28
* @var int
+ − 29
*/
+ − 30
+ − 31
var $lang_id;
+ − 32
+ − 33
/**
+ − 34
* The ISO-639-3 code for the loaded language. This should be grabbed directly from the database.
+ − 35
* @var string
+ − 36
*/
+ − 37
+ − 38
var $lang_code;
210
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 39
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 40
/**
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
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
diff
changeset
+ − 42
* @var int
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 43
*/
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 44
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 45
var $lang_timestamp;
205
+ − 46
+ − 47
/**
+ − 48
* Will be an object that holds an instance of the class configured with the site's default language. Only instanciated when needed.
+ − 49
* @var object
+ − 50
*/
+ − 51
+ − 52
var $default;
+ − 53
+ − 54
/**
+ − 55
* The list of loaded strings.
+ − 56
* @var array
+ − 57
* @access private
+ − 58
*/
+ − 59
+ − 60
var $strings = array();
+ − 61
+ − 62
/**
+ − 63
* Constructor.
+ − 64
* @param int|string Language ID or code to load.
+ − 65
*/
+ − 66
+ − 67
function __construct($lang)
+ − 68
{
+ − 69
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 70
+ − 71
if ( defined('IN_ENANO_INSTALL') )
+ − 72
{
+ − 73
// special case for the Enano installer: it will load its own strings from a JSON file and just use this API for fetching and templatizing them.
243
+ − 74
$this->lang_id = 1;
+ − 75
$this->lang_code = $lang;
205
+ − 76
return true;
+ − 77
}
+ − 78
if ( is_string($lang) )
+ − 79
{
371
dc6026376919
Improved compatibility with PostgreSQL and fixed a number of installer bugs; fixed missing "meta" category declaration in language files
Dan
diff
changeset
+ − 80
$sql_col = 'lang_code=\'' . $db->escape($lang) . '\'';
205
+ − 81
}
+ − 82
else if ( is_int($lang) )
+ − 83
{
+ − 84
$sql_col = 'lang_id=' . $lang . '';
+ − 85
}
+ − 86
else
+ − 87
{
+ − 88
$db->_die('lang.php - attempting to pass invalid value to constructor');
+ − 89
}
+ − 90
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
diff
changeset
+ − 91
$lang_default = ( $x = getConfig('default_language') ) ? intval($x) : '\'def\'';
210
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 92
$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 DESC LIMIT 1;");
205
+ − 93
+ − 94
if ( !$q )
+ − 95
$db->_die('lang.php - main select query');
+ − 96
+ − 97
if ( $db->numrows() < 1 )
+ − 98
$db->_die('lang.php - There are no languages installed');
+ − 99
+ − 100
$row = $db->fetchrow();
+ − 101
+ − 102
$this->lang_id = intval( $row['lang_id'] );
+ − 103
$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
diff
changeset
+ − 104
$this->lang_timestamp = $row['last_changed'];
205
+ − 105
}
+ − 106
+ − 107
/**
+ − 108
* Fetches language strings from the database, or a cache file if it's available.
+ − 109
* @param bool If true (default), allows the cache to be used.
+ − 110
*/
+ − 111
+ − 112
function fetch($allow_cache = true)
+ − 113
{
+ − 114
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 115
+ − 116
$lang_file = ENANO_ROOT . "/cache/lang_{$this->lang_id}.php";
+ − 117
// Attempt to load the strings from a cache file
+ − 118
if ( file_exists($lang_file) && $allow_cache )
+ − 119
{
+ − 120
// Yay! found it
+ − 121
$this->load_cache_file($lang_file);
+ − 122
}
+ − 123
else
+ − 124
{
+ − 125
// No cache file - select and retrieve from the database
+ − 126
$q = $db->sql_unbuffered_query("SELECT string_category, string_name, string_content FROM " . table_prefix . "language_strings WHERE lang_id = {$this->lang_id};");
+ − 127
if ( !$q )
+ − 128
$db->_die('lang.php - selecting language string data');
+ − 129
if ( $row = $db->fetchrow() )
+ − 130
{
+ − 131
$strings = array();
+ − 132
do
+ − 133
{
+ − 134
$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
diff
changeset
+ − 135
if ( !is_array(@$strings[$cat]) )
205
+ − 136
{
+ − 137
$strings[$cat] = array();
+ − 138
}
+ − 139
$strings[$cat][ $row['string_name'] ] = $row['string_content'];
+ − 140
}
+ − 141
while ( $row = $db->fetchrow() );
+ − 142
// all done fetching
+ − 143
$this->merge($strings);
+ − 144
}
+ − 145
else
+ − 146
{
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
diff
changeset
+ − 147
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
diff
changeset
+ − 148
$db->_die('lang.php - No strings for language ' . $this->lang_code);
205
+ − 149
}
+ − 150
}
+ − 151
}
+ − 152
+ − 153
/**
+ − 154
* Loads a file from the disk cache (treated as PHP) and merges it into RAM.
+ − 155
* @param string File to load
+ − 156
*/
+ − 157
+ − 158
function load_cache_file($file)
+ − 159
{
+ − 160
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 161
+ − 162
// We're using eval() here because it makes handling scope easier.
+ − 163
+ − 164
if ( !file_exists($file) )
+ − 165
$db->_die('lang.php - requested cache file doesn\'t exist');
+ − 166
+ − 167
$contents = file_get_contents($file);
+ − 168
$contents = preg_replace('/([\s]*)<\?php/', '', $contents);
+ − 169
+ − 170
@eval($contents);
+ − 171
+ − 172
if ( !isset($lang_cache) || ( isset($lang_cache) && !is_array($lang_cache) ) )
+ − 173
$db->_die('lang.php - the cache file is invalid (didn\'t set $lang_cache as an array)');
+ − 174
+ − 175
$this->merge($lang_cache);
+ − 176
}
+ − 177
+ − 178
/**
243
+ − 179
* Loads a JSON language file and parses the strings into RAM. Will use the cache if possible, but stays far away from the database,
+ − 180
* which we assume doesn't exist yet.
+ − 181
*/
+ − 182
+ − 183
function load_file($file)
+ − 184
{
+ − 185
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 186
+ − 187
if ( !file_exists($file) )
348
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 188
{
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 189
if ( defined('IN_ENANO_INSTALL') )
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 190
{
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 191
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
diff
changeset
+ − 192
}
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 193
else
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 194
{
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 195
$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
diff
changeset
+ − 196
}
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 197
}
243
+ − 198
+ − 199
$contents = trim(@file_get_contents($file));
+ − 200
if ( empty($contents) )
+ − 201
$db->_die('lang.php - empty language file...');
+ − 202
+ − 203
// Trim off all text before and after the starting and ending braces
+ − 204
$contents = preg_replace('/^([^{]+)\{/', '{', $contents);
+ − 205
$contents = preg_replace('/\}([^}]+)$/', '}', $contents);
+ − 206
$contents = trim($contents);
+ − 207
+ − 208
if ( empty($contents) )
+ − 209
$db->_die('lang.php - no meat to the language file...');
+ − 210
+ − 211
$checksum = md5($contents);
+ − 212
if ( file_exists("./cache/lang_json_{$checksum}.php") )
+ − 213
{
+ − 214
$this->load_cache_file("./cache/lang_json_{$checksum}.php");
+ − 215
}
+ − 216
else
+ − 217
{
348
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 218
// 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
diff
changeset
+ − 219
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 220
// eliminate comments
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 221
$contents = preg_replace(array(
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 222
// 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
diff
changeset
+ − 223
'#^\s*//(.+)$#m',
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 224
// 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
diff
changeset
+ − 225
'#^\s*/\*(.+)\*/#Us',
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 226
// 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
diff
changeset
+ − 227
'#/\*(.+)\*/\s*$#Us'
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 228
), '', $contents);
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 229
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 230
$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
diff
changeset
+ − 231
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 232
try
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 233
{
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 234
$langdata = enano_json_decode($contents);
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 235
}
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 236
catch(Zend_Json_Exception $e)
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 237
{
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 238
$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
diff
changeset
+ − 239
exit;
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 240
}
243
+ − 241
+ − 242
if ( !is_array($langdata) )
+ − 243
$db->_die('lang.php - invalid language file');
+ − 244
+ − 245
if ( !isset($langdata['categories']) || !isset($langdata['strings']) )
+ − 246
$db->_die('lang.php - language file does not contain the proper items');
+ − 247
+ − 248
$this->merge($langdata['strings']);
+ − 249
+ − 250
$lang_file = "./cache/lang_json_{$checksum}.php";
+ − 251
+ − 252
$handle = @fopen($lang_file, 'w');
+ − 253
if ( !$handle )
+ − 254
// Couldn't open the file. Silently fail and let the strings come from RAM.
+ − 255
return false;
+ − 256
+ − 257
// The file's open, that means we should be good.
+ − 258
fwrite($handle, '<?php
+ − 259
// This file was generated automatically by Enano. You should not edit this file because any changes you make
+ − 260
// to it will not be visible in the ACP and all changes will be lost upon any changes to strings in the admin panel.
+ − 261
+ − 262
$lang_cache = ');
+ − 263
+ − 264
$exported = $this->var_export_string($this->strings);
+ − 265
if ( empty($exported) )
+ − 266
// Ehh, that's not good
+ − 267
$db->_die('lang.php - load_file(): var_export_string() failed');
+ − 268
+ − 269
fwrite($handle, $exported . '; ?>');
+ − 270
+ − 271
// Clean up
+ − 272
unset($exported, $langdata);
+ − 273
+ − 274
// Done =)
+ − 275
fclose($handle);
+ − 276
}
+ − 277
}
+ − 278
+ − 279
/**
205
+ − 280
* Merges a standard language assoc array ($arr[cat][stringid]) with the master in RAM.
+ − 281
* @param array
+ − 282
*/
+ − 283
+ − 284
function merge($strings)
+ − 285
{
+ − 286
// This is stupidly simple.
+ − 287
foreach ( $strings as $cat_id => $contents )
+ − 288
{
243
+ − 289
if ( !isset($this->strings[$cat_id]) || ( isset($this->strings[$cat_id]) && !is_array($this->strings[$cat_id]) ) )
205
+ − 290
$this->strings[$cat_id] = array();
+ − 291
foreach ( $contents as $string_id => $string )
+ − 292
{
+ − 293
$this->strings[$cat_id][$string_id] = $string;
+ − 294
}
+ − 295
}
+ − 296
}
+ − 297
+ − 298
/**
+ − 299
* Imports a JSON-format language file into the database and merges with current strings.
+ − 300
* @param string Path to the JSON file to load
+ − 301
*/
+ − 302
+ − 303
function import($file)
+ − 304
{
+ − 305
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 306
+ − 307
if ( !file_exists($file) )
+ − 308
$db->_die('lang.php - can\'t import language file: string file doesn\'t exist');
+ − 309
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
diff
changeset
+ − 310
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
diff
changeset
+ − 311
$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
diff
changeset
+ − 312
205
+ − 313
$contents = trim(@file_get_contents($file));
+ − 314
+ − 315
if ( empty($contents) )
+ − 316
$db->_die('lang.php - can\'t load the contents of the language file');
+ − 317
+ − 318
// Trim off all text before and after the starting and ending braces
+ − 319
$contents = preg_replace('/^([^{]+)\{/', '{', $contents);
+ − 320
$contents = preg_replace('/\}([^}]+)$/', '}', $contents);
+ − 321
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
diff
changeset
+ − 322
// Correct syntax to be nice to the json parser
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
diff
changeset
+ − 323
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
diff
changeset
+ − 324
// eliminate comments
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
diff
changeset
+ − 325
$contents = preg_replace(array(
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
diff
changeset
+ − 326
// eliminate single line comments in '// ...' form
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
diff
changeset
+ − 327
'#^\s*//(.+)$#m',
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
diff
changeset
+ − 328
// eliminate multi-line comments in '/* ... */' form, at start of string
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
diff
changeset
+ − 329
'#^\s*/\*(.+)\*/#Us',
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
diff
changeset
+ − 330
// eliminate multi-line comments in '/* ... */' form, at end of string
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
diff
changeset
+ − 331
'#/\*(.+)\*/\s*$#Us'
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
diff
changeset
+ − 332
), '', $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
diff
changeset
+ − 333
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
diff
changeset
+ − 334
$contents = preg_replace('/([,\{\[])([\s]*?)([a-z0-9_]+)([\s]*?):/', '\\1\\2"\\3" :', $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
diff
changeset
+ − 335
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
diff
changeset
+ − 336
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
diff
changeset
+ − 337
{
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
diff
changeset
+ − 338
$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
diff
changeset
+ − 339
}
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
diff
changeset
+ − 340
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
diff
changeset
+ − 341
{
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
diff
changeset
+ − 342
$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
diff
changeset
+ − 343
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
diff
changeset
+ − 344
}
205
+ − 345
+ − 346
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
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
diff
changeset
+ − 348
$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
diff
changeset
+ − 349
}
205
+ − 350
+ − 351
if ( !isset($langdata['categories']) || !isset($langdata['strings']) )
+ − 352
$db->_die('lang.php - language file does not contain the proper items');
+ − 353
+ − 354
$insert_list = array();
+ − 355
$delete_list = array();
+ − 356
+ − 357
foreach ( $langdata['categories'] as $category )
+ − 358
{
+ − 359
if ( isset($langdata['strings'][$category]) )
+ − 360
{
+ − 361
foreach ( $langdata['strings'][$category] as $string_name => $string_value )
+ − 362
{
+ − 363
$string_name = $db->escape($string_name);
+ − 364
$string_value = $db->escape($string_value);
+ − 365
$category_name = $db->escape($category);
+ − 366
$insert_list[] = "({$this->lang_id}, '$category_name', '$string_name', '$string_value')";
+ − 367
$delete_list[] = "( lang_id = {$this->lang_id} AND string_category = '$category_name' AND string_name = '$string_name' )";
+ − 368
}
+ − 369
}
+ − 370
}
+ − 371
+ − 372
$delete_list = implode(" OR\n ", $delete_list);
+ − 373
$sql = "DELETE FROM " . table_prefix . "language_strings WHERE $delete_list;";
+ − 374
+ − 375
// Free some memory
+ − 376
unset($delete_list);
+ − 377
+ − 378
// Run the query
+ − 379
$q = $db->sql_query($sql);
+ − 380
if ( !$q )
+ − 381
$db->_die('lang.php - couldn\'t kill off them old strings');
+ − 382
+ − 383
$insert_list = implode(",\n ", $insert_list);
+ − 384
$sql = "INSERT INTO " . table_prefix . "language_strings(lang_id, string_category, string_name, string_content) VALUES\n $insert_list;";
+ − 385
+ − 386
// Free some memory
+ − 387
unset($insert_list);
+ − 388
+ − 389
// Run the query
+ − 390
$q = $db->sql_query($sql);
+ − 391
if ( !$q )
+ − 392
$db->_die('lang.php - couldn\'t insert strings in import()');
+ − 393
+ − 394
// YAY! done!
+ − 395
// This will regenerate the cache file if possible.
+ − 396
$this->regen_caches();
+ − 397
}
+ − 398
+ − 399
/**
+ − 400
* Refetches the strings and writes out the cache file.
+ − 401
*/
+ − 402
+ − 403
function regen_caches()
+ − 404
{
+ − 405
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 406
+ − 407
$lang_file = ENANO_ROOT . "/cache/lang_{$this->lang_id}.php";
+ − 408
+ − 409
// Refresh the strings in RAM to the latest copies in the DB
+ − 410
$this->fetch(false);
+ − 411
+ − 412
$handle = @fopen($lang_file, 'w');
+ − 413
if ( !$handle )
+ − 414
// Couldn't open the file. Silently fail and let the strings come from the database.
+ − 415
return false;
+ − 416
+ − 417
// The file's open, that means we should be good.
+ − 418
fwrite($handle, '<?php
+ − 419
// This file was generated automatically by Enano. You should not edit this file because any changes you make
+ − 420
// to it will not be visible in the ACP and all changes will be lost upon any changes to strings in the admin panel.
+ − 421
+ − 422
$lang_cache = ');
+ − 423
+ − 424
$exported = $this->var_export_string($this->strings);
+ − 425
if ( empty($exported) )
+ − 426
// Ehh, that's not good
+ − 427
$db->_die('lang.php - var_export_string() failed');
+ − 428
+ − 429
fwrite($handle, $exported . '; ?>');
+ − 430
210
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 431
// Update timestamp in database
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 432
$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
diff
changeset
+ − 433
if ( !$q )
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 434
$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
diff
changeset
+ − 435
205
+ − 436
// Done =)
+ − 437
fclose($handle);
+ − 438
}
+ − 439
+ − 440
/**
+ − 441
* Calls var_export() on whatever, and returns the function's output.
+ − 442
* @param mixed Whatever you want var_exported. Usually an array.
+ − 443
* @return string
+ − 444
*/
+ − 445
+ − 446
function var_export_string($val)
+ − 447
{
+ − 448
ob_start();
+ − 449
var_export($val);
+ − 450
$contents = ob_get_contents();
+ − 451
ob_end_clean();
+ − 452
return $contents;
+ − 453
}
+ − 454
+ − 455
/**
+ − 456
* 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
+ − 457
* in the default language. If even then the string can't be found, this function will return what was passed to it.
+ − 458
*
+ − 459
* 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
+ − 460
* of 'foo' => 'foo substitute'.
+ − 461
*
+ − 462
* @param string ID of the string to fetch. This will always be in the format of category_stringid.
+ − 463
* @param array Optional. Associative array of substitutions.
+ − 464
* @return string
+ − 465
*/
+ − 466
+ − 467
function get($string_id, $substitutions = false)
+ − 468
{
+ − 469
// Extract the category and string ID
+ − 470
$category = substr($string_id, 0, ( strpos($string_id, '_') ));
+ − 471
$string_name = substr($string_id, ( strpos($string_id, '_') + 1 ));
+ − 472
$found = false;
+ − 473
if ( isset($this->strings[$category]) && isset($this->strings[$category][$string_name]) )
+ − 474
{
+ − 475
$found = true;
+ − 476
$string = $this->strings[$category][$string_name];
+ − 477
}
+ − 478
if ( !$found )
+ − 479
{
+ − 480
// Ehh, the string wasn't found. Rerun fetch() and try again.
243
+ − 481
if ( defined('IN_ENANO_INSTALL') )
+ − 482
{
+ − 483
return $string_id;
+ − 484
}
205
+ − 485
$this->fetch();
+ − 486
if ( isset($this->strings[$category]) && isset($this->strings[$category][$string_name]) )
+ − 487
{
+ − 488
$found = true;
+ − 489
$string = $this->strings[$category][$string_name];
+ − 490
}
+ − 491
if ( !$found )
+ − 492
{
+ − 493
// STILL not found. Check the default language.
+ − 494
$lang_default = ( $x = getConfig('default_language') ) ? intval($x) : $this->lang_id;
+ − 495
if ( $lang_default != $this->lang_id )
+ − 496
{
+ − 497
if ( !is_object($this->default) )
+ − 498
$this->default = new Language($lang_default);
+ − 499
return $this->default->get($string_id, $substitutions);
+ − 500
}
+ − 501
}
+ − 502
}
+ − 503
if ( !$found )
+ − 504
{
+ − 505
// Alright, it's nowhere. Return the input, grumble grumble...
+ − 506
return $string_id;
+ − 507
}
+ − 508
// Found it!
+ − 509
// Perform substitutions.
209
+ − 510
// if ( is_array($substitutions) )
+ − 511
// die('<pre>' . print_r($substitutions, true) . '</pre>');
205
+ − 512
if ( !is_array($substitutions) )
+ − 513
$substitutions = array();
+ − 514
return $this->substitute($string, $substitutions);
+ − 515
}
+ − 516
+ − 517
/**
+ − 518
* Processes substitutions.
+ − 519
* @param string
+ − 520
* @param array
+ − 521
* @return string
+ − 522
*/
+ − 523
+ − 524
function substitute($string, $subs)
+ − 525
{
+ − 526
preg_match_all('/%this\.([a-z0-9_]+)%/', $string, $matches);
+ − 527
if ( count($matches[0]) > 0 )
+ − 528
{
+ − 529
foreach ( $matches[1] as $i => $string_id )
+ − 530
{
+ − 531
$result = $this->get($string_id);
+ − 532
$string = str_replace($matches[0][$i], $result, $string);
+ − 533
}
+ − 534
}
209
+ − 535
preg_match_all('/%config\.([a-z0-9_]+)%/', $string, $matches);
+ − 536
if ( count($matches[0]) > 0 )
+ − 537
{
+ − 538
foreach ( $matches[1] as $i => $string_id )
+ − 539
{
+ − 540
$result = getConfig($string_id);
+ − 541
$string = str_replace($matches[0][$i], $result, $string);
+ − 542
}
+ − 543
}
205
+ − 544
foreach ( $subs as $key => $value )
+ − 545
{
209
+ − 546
$subs[$key] = strval($value);
+ − 547
$string = str_replace("%{$key}%", "{$subs[$key]}", $string);
205
+ − 548
}
355
+ − 549
return $string . '*';
205
+ − 550
}
+ − 551
+ − 552
} // class Language
+ − 553
+ − 554
?>