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