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\'';
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
diff
changeset
+ − 92
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
diff
changeset
+ − 93
$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
+ − 94
+ − 95
if ( !$q )
+ − 96
$db->_die('lang.php - main select query');
+ − 97
+ − 98
if ( $db->numrows() < 1 )
+ − 99
$db->_die('lang.php - There are no languages installed');
+ − 100
+ − 101
$row = $db->fetchrow();
+ − 102
+ − 103
$this->lang_id = intval( $row['lang_id'] );
+ − 104
$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
+ − 105
$this->lang_timestamp = $row['last_changed'];
205
+ − 106
}
+ − 107
+ − 108
/**
+ − 109
* Fetches language strings from the database, or a cache file if it's available.
+ − 110
* @param bool If true (default), allows the cache to be used.
+ − 111
*/
+ − 112
+ − 113
function fetch($allow_cache = true)
+ − 114
{
+ − 115
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 116
+ − 117
$lang_file = ENANO_ROOT . "/cache/lang_{$this->lang_id}.php";
+ − 118
// Attempt to load the strings from a cache file
+ − 119
if ( file_exists($lang_file) && $allow_cache )
+ − 120
{
+ − 121
// Yay! found it
+ − 122
$this->load_cache_file($lang_file);
+ − 123
}
+ − 124
else
+ − 125
{
+ − 126
// No cache file - select and retrieve from the database
+ − 127
$q = $db->sql_unbuffered_query("SELECT string_category, string_name, string_content FROM " . table_prefix . "language_strings WHERE lang_id = {$this->lang_id};");
+ − 128
if ( !$q )
+ − 129
$db->_die('lang.php - selecting language string data');
+ − 130
if ( $row = $db->fetchrow() )
+ − 131
{
+ − 132
$strings = array();
+ − 133
do
+ − 134
{
+ − 135
$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
+ − 136
if ( !is_array(@$strings[$cat]) )
205
+ − 137
{
+ − 138
$strings[$cat] = array();
+ − 139
}
+ − 140
$strings[$cat][ $row['string_name'] ] = $row['string_content'];
+ − 141
}
+ − 142
while ( $row = $db->fetchrow() );
+ − 143
// all done fetching
+ − 144
$this->merge($strings);
+ − 145
}
+ − 146
else
+ − 147
{
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
+ − 148
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
+ − 149
$db->_die('lang.php - No strings for language ' . $this->lang_code);
205
+ − 150
}
+ − 151
}
+ − 152
}
+ − 153
+ − 154
/**
+ − 155
* Loads a file from the disk cache (treated as PHP) and merges it into RAM.
+ − 156
* @param string File to load
+ − 157
*/
+ − 158
+ − 159
function load_cache_file($file)
+ − 160
{
+ − 161
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 162
+ − 163
// We're using eval() here because it makes handling scope easier.
+ − 164
+ − 165
if ( !file_exists($file) )
+ − 166
$db->_die('lang.php - requested cache file doesn\'t exist');
+ − 167
+ − 168
$contents = file_get_contents($file);
+ − 169
$contents = preg_replace('/([\s]*)<\?php/', '', $contents);
+ − 170
+ − 171
@eval($contents);
+ − 172
+ − 173
if ( !isset($lang_cache) || ( isset($lang_cache) && !is_array($lang_cache) ) )
+ − 174
$db->_die('lang.php - the cache file is invalid (didn\'t set $lang_cache as an array)');
+ − 175
+ − 176
$this->merge($lang_cache);
+ − 177
}
+ − 178
+ − 179
/**
243
+ − 180
* Loads a JSON language file and parses the strings into RAM. Will use the cache if possible, but stays far away from the database,
+ − 181
* which we assume doesn't exist yet.
+ − 182
*/
+ − 183
+ − 184
function load_file($file)
+ − 185
{
+ − 186
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 187
+ − 188
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
+ − 189
{
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 190
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
+ − 191
{
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 192
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
+ − 193
}
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 194
else
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 195
{
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 196
$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
+ − 197
}
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 198
}
243
+ − 199
+ − 200
$contents = trim(@file_get_contents($file));
+ − 201
if ( empty($contents) )
+ − 202
$db->_die('lang.php - empty language file...');
+ − 203
+ − 204
// Trim off all text before and after the starting and ending braces
+ − 205
$contents = preg_replace('/^([^{]+)\{/', '{', $contents);
+ − 206
$contents = preg_replace('/\}([^}]+)$/', '}', $contents);
+ − 207
$contents = trim($contents);
+ − 208
+ − 209
if ( empty($contents) )
+ − 210
$db->_die('lang.php - no meat to the language file...');
+ − 211
+ − 212
$checksum = md5($contents);
+ − 213
if ( file_exists("./cache/lang_json_{$checksum}.php") )
+ − 214
{
+ − 215
$this->load_cache_file("./cache/lang_json_{$checksum}.php");
+ − 216
}
+ − 217
else
+ − 218
{
348
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 219
// 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
+ − 220
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 221
// eliminate comments
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 222
$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
+ − 223
// 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
+ − 224
'#^\s*//(.+)$#m',
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 225
// 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
+ − 226
'#^\s*/\*(.+)\*/#Us',
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 227
// 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
+ − 228
'#/\*(.+)\*/\s*$#Us'
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 229
), '', $contents);
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 230
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 231
$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
+ − 232
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 233
try
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 234
{
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 235
$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
+ − 236
}
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 237
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
+ − 238
{
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 239
$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
+ − 240
exit;
87e08a6e4fec
Welcome to the new Enano installer. Much distance still to be covered but the basics are there.
Dan
diff
changeset
+ − 241
}
243
+ − 242
+ − 243
if ( !is_array($langdata) )
+ − 244
$db->_die('lang.php - invalid language file');
+ − 245
+ − 246
if ( !isset($langdata['categories']) || !isset($langdata['strings']) )
+ − 247
$db->_die('lang.php - language file does not contain the proper items');
+ − 248
+ − 249
$this->merge($langdata['strings']);
+ − 250
+ − 251
$lang_file = "./cache/lang_json_{$checksum}.php";
+ − 252
+ − 253
$handle = @fopen($lang_file, 'w');
+ − 254
if ( !$handle )
+ − 255
// Couldn't open the file. Silently fail and let the strings come from RAM.
+ − 256
return false;
+ − 257
+ − 258
// The file's open, that means we should be good.
+ − 259
fwrite($handle, '<?php
+ − 260
// This file was generated automatically by Enano. You should not edit this file because any changes you make
+ − 261
// to it will not be visible in the ACP and all changes will be lost upon any changes to strings in the admin panel.
+ − 262
+ − 263
$lang_cache = ');
+ − 264
+ − 265
$exported = $this->var_export_string($this->strings);
+ − 266
if ( empty($exported) )
+ − 267
// Ehh, that's not good
+ − 268
$db->_die('lang.php - load_file(): var_export_string() failed');
+ − 269
+ − 270
fwrite($handle, $exported . '; ?>');
+ − 271
+ − 272
// Clean up
+ − 273
unset($exported, $langdata);
+ − 274
+ − 275
// Done =)
+ − 276
fclose($handle);
+ − 277
}
+ − 278
}
+ − 279
+ − 280
/**
205
+ − 281
* Merges a standard language assoc array ($arr[cat][stringid]) with the master in RAM.
+ − 282
* @param array
+ − 283
*/
+ − 284
+ − 285
function merge($strings)
+ − 286
{
+ − 287
// This is stupidly simple.
+ − 288
foreach ( $strings as $cat_id => $contents )
+ − 289
{
243
+ − 290
if ( !isset($this->strings[$cat_id]) || ( isset($this->strings[$cat_id]) && !is_array($this->strings[$cat_id]) ) )
205
+ − 291
$this->strings[$cat_id] = array();
+ − 292
foreach ( $contents as $string_id => $string )
+ − 293
{
+ − 294
$this->strings[$cat_id][$string_id] = $string;
+ − 295
}
+ − 296
}
+ − 297
}
+ − 298
+ − 299
/**
+ − 300
* Imports a JSON-format language file into the database and merges with current strings.
+ − 301
* @param string Path to the JSON file to load
+ − 302
*/
+ − 303
+ − 304
function import($file)
+ − 305
{
+ − 306
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 307
+ − 308
if ( !file_exists($file) )
+ − 309
$db->_die('lang.php - can\'t import language file: string file doesn\'t exist');
+ − 310
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
+ − 311
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
+ − 312
$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
+ − 313
205
+ − 314
$contents = trim(@file_get_contents($file));
+ − 315
+ − 316
if ( empty($contents) )
+ − 317
$db->_die('lang.php - can\'t load the contents of the language file');
+ − 318
+ − 319
// Trim off all text before and after the starting and ending braces
+ − 320
$contents = preg_replace('/^([^{]+)\{/', '{', $contents);
+ − 321
$contents = preg_replace('/\}([^}]+)$/', '}', $contents);
+ − 322
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
+ − 323
// 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
diff
changeset
+ − 324
$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
diff
changeset
+ − 325
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
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
+ − 327
{
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
$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
+ − 329
}
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
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
+ − 331
{
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
+ − 332
$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
+ − 333
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
+ − 334
}
205
+ − 335
+ − 336
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
+ − 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
$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
+ − 339
}
205
+ − 340
+ − 341
if ( !isset($langdata['categories']) || !isset($langdata['strings']) )
+ − 342
$db->_die('lang.php - language file does not contain the proper items');
+ − 343
+ − 344
$insert_list = array();
+ − 345
$delete_list = array();
+ − 346
+ − 347
foreach ( $langdata['categories'] as $category )
+ − 348
{
+ − 349
if ( isset($langdata['strings'][$category]) )
+ − 350
{
+ − 351
foreach ( $langdata['strings'][$category] as $string_name => $string_value )
+ − 352
{
+ − 353
$string_name = $db->escape($string_name);
+ − 354
$string_value = $db->escape($string_value);
+ − 355
$category_name = $db->escape($category);
+ − 356
$insert_list[] = "({$this->lang_id}, '$category_name', '$string_name', '$string_value')";
+ − 357
$delete_list[] = "( lang_id = {$this->lang_id} AND string_category = '$category_name' AND string_name = '$string_name' )";
+ − 358
}
+ − 359
}
+ − 360
}
+ − 361
+ − 362
$delete_list = implode(" OR\n ", $delete_list);
+ − 363
$sql = "DELETE FROM " . table_prefix . "language_strings WHERE $delete_list;";
+ − 364
+ − 365
// Free some memory
+ − 366
unset($delete_list);
+ − 367
+ − 368
// Run the query
+ − 369
$q = $db->sql_query($sql);
+ − 370
if ( !$q )
+ − 371
$db->_die('lang.php - couldn\'t kill off them old strings');
+ − 372
+ − 373
$insert_list = implode(",\n ", $insert_list);
+ − 374
$sql = "INSERT INTO " . table_prefix . "language_strings(lang_id, string_category, string_name, string_content) VALUES\n $insert_list;";
+ − 375
+ − 376
// Free some memory
+ − 377
unset($insert_list);
+ − 378
+ − 379
// Run the query
+ − 380
$q = $db->sql_query($sql);
+ − 381
if ( !$q )
+ − 382
$db->_die('lang.php - couldn\'t insert strings in import()');
+ − 383
+ − 384
// YAY! done!
+ − 385
// This will regenerate the cache file if possible.
+ − 386
$this->regen_caches();
+ − 387
}
+ − 388
+ − 389
/**
+ − 390
* Refetches the strings and writes out the cache file.
+ − 391
*/
+ − 392
+ − 393
function regen_caches()
+ − 394
{
+ − 395
global $db, $session, $paths, $template, $plugins; // Common objects
+ − 396
+ − 397
$lang_file = ENANO_ROOT . "/cache/lang_{$this->lang_id}.php";
+ − 398
+ − 399
// Refresh the strings in RAM to the latest copies in the DB
+ − 400
$this->fetch(false);
+ − 401
+ − 402
$handle = @fopen($lang_file, 'w');
+ − 403
if ( !$handle )
+ − 404
// Couldn't open the file. Silently fail and let the strings come from the database.
+ − 405
return false;
+ − 406
+ − 407
// The file's open, that means we should be good.
+ − 408
fwrite($handle, '<?php
+ − 409
// This file was generated automatically by Enano. You should not edit this file because any changes you make
+ − 410
// to it will not be visible in the ACP and all changes will be lost upon any changes to strings in the admin panel.
+ − 411
+ − 412
$lang_cache = ');
+ − 413
+ − 414
$exported = $this->var_export_string($this->strings);
+ − 415
if ( empty($exported) )
+ − 416
// Ehh, that's not good
+ − 417
$db->_die('lang.php - var_export_string() failed');
+ − 418
+ − 419
fwrite($handle, $exported . '; ?>');
+ − 420
210
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 421
// 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
+ − 422
$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
+ − 423
if ( !$q )
2b283402e4e4
Added language export to JSON page and localization for Javascript using $lang.get(). Localized AJAX login interface.
Dan
diff
changeset
+ − 424
$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
+ − 425
205
+ − 426
// Done =)
+ − 427
fclose($handle);
+ − 428
}
+ − 429
+ − 430
/**
+ − 431
* Calls var_export() on whatever, and returns the function's output.
+ − 432
* @param mixed Whatever you want var_exported. Usually an array.
+ − 433
* @return string
+ − 434
*/
+ − 435
+ − 436
function var_export_string($val)
+ − 437
{
+ − 438
ob_start();
+ − 439
var_export($val);
+ − 440
$contents = ob_get_contents();
+ − 441
ob_end_clean();
+ − 442
return $contents;
+ − 443
}
+ − 444
+ − 445
/**
+ − 446
* 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
+ − 447
* in the default language. If even then the string can't be found, this function will return what was passed to it.
+ − 448
*
+ − 449
* 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
+ − 450
* of 'foo' => 'foo substitute'.
+ − 451
*
+ − 452
* @param string ID of the string to fetch. This will always be in the format of category_stringid.
+ − 453
* @param array Optional. Associative array of substitutions.
+ − 454
* @return string
+ − 455
*/
+ − 456
+ − 457
function get($string_id, $substitutions = false)
+ − 458
{
+ − 459
// Extract the category and string ID
+ − 460
$category = substr($string_id, 0, ( strpos($string_id, '_') ));
+ − 461
$string_name = substr($string_id, ( strpos($string_id, '_') + 1 ));
+ − 462
$found = false;
+ − 463
if ( isset($this->strings[$category]) && isset($this->strings[$category][$string_name]) )
+ − 464
{
+ − 465
$found = true;
+ − 466
$string = $this->strings[$category][$string_name];
+ − 467
}
+ − 468
if ( !$found )
+ − 469
{
+ − 470
// Ehh, the string wasn't found. Rerun fetch() and try again.
243
+ − 471
if ( defined('IN_ENANO_INSTALL') )
+ − 472
{
+ − 473
return $string_id;
+ − 474
}
205
+ − 475
$this->fetch();
+ − 476
if ( isset($this->strings[$category]) && isset($this->strings[$category][$string_name]) )
+ − 477
{
+ − 478
$found = true;
+ − 479
$string = $this->strings[$category][$string_name];
+ − 480
}
+ − 481
if ( !$found )
+ − 482
{
+ − 483
// STILL not found. Check the default language.
+ − 484
$lang_default = ( $x = getConfig('default_language') ) ? intval($x) : $this->lang_id;
+ − 485
if ( $lang_default != $this->lang_id )
+ − 486
{
+ − 487
if ( !is_object($this->default) )
+ − 488
$this->default = new Language($lang_default);
+ − 489
return $this->default->get($string_id, $substitutions);
+ − 490
}
+ − 491
}
+ − 492
}
+ − 493
if ( !$found )
+ − 494
{
+ − 495
// Alright, it's nowhere. Return the input, grumble grumble...
+ − 496
return $string_id;
+ − 497
}
+ − 498
// Found it!
+ − 499
// Perform substitutions.
209
+ − 500
// if ( is_array($substitutions) )
+ − 501
// die('<pre>' . print_r($substitutions, true) . '</pre>');
205
+ − 502
if ( !is_array($substitutions) )
+ − 503
$substitutions = array();
+ − 504
return $this->substitute($string, $substitutions);
+ − 505
}
+ − 506
+ − 507
/**
+ − 508
* Processes substitutions.
+ − 509
* @param string
+ − 510
* @param array
+ − 511
* @return string
+ − 512
*/
+ − 513
+ − 514
function substitute($string, $subs)
+ − 515
{
+ − 516
preg_match_all('/%this\.([a-z0-9_]+)%/', $string, $matches);
+ − 517
if ( count($matches[0]) > 0 )
+ − 518
{
+ − 519
foreach ( $matches[1] as $i => $string_id )
+ − 520
{
+ − 521
$result = $this->get($string_id);
+ − 522
$string = str_replace($matches[0][$i], $result, $string);
+ − 523
}
+ − 524
}
209
+ − 525
preg_match_all('/%config\.([a-z0-9_]+)%/', $string, $matches);
+ − 526
if ( count($matches[0]) > 0 )
+ − 527
{
+ − 528
foreach ( $matches[1] as $i => $string_id )
+ − 529
{
+ − 530
$result = getConfig($string_id);
+ − 531
$string = str_replace($matches[0][$i], $result, $string);
+ − 532
}
+ − 533
}
205
+ − 534
foreach ( $subs as $key => $value )
+ − 535
{
209
+ − 536
$subs[$key] = strval($value);
+ − 537
$string = str_replace("%{$key}%", "{$subs[$key]}", $string);
205
+ − 538
}
355
+ − 539
return $string . '*';
205
+ − 540
}
+ − 541
+ − 542
} // class Language
+ − 543
+ − 544
?>