-
Notifications
You must be signed in to change notification settings - Fork 16
/
lang_compile.php
executable file
·234 lines (206 loc) · 7.15 KB
/
lang_compile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php /*
ocPortal
Copyright (c) ocProducts, 2004-2013
See text/EN/licence.txt for full licencing information.
*/
/**
* @license http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
* @copyright ocProducts Ltd
* @package core
*/
if (!function_exists('init__lang_compile'))
{
/**
* Standard code module initialisation function.
*/
function init__lang_compile()
{
global $DECACHED_COMCODE_LANG_STRINGS;
$DECACHED_COMCODE_LANG_STRINGS=false;
}
}
if (!function_exists('require_lang_compile'))
{
/**
* Load up a language file, compiling it (it's not cached yet).
*
* @param ID_TEXT The language file name
* @param ?LANGUAGE_NAME The language (NULL: uses the current language)
* @param ?string The language type (lang_custom, or custom) (NULL: normal priorities are used)
* @set lang_custom custom
* @param PATH Where we are cacheing too
* @param boolean Whether to just return if there was a loading error
* @return boolean Whether we FAILED to load
*/
function require_lang_compile($codename,$lang,$type,$cache_path,$ignore_errors=false)
{
global $LANGUAGE_STRINGS_CACHE,$REQUIRE_LANG_LOOP,$LANG_LOADED_LANG;
$desire_cache=(function_exists('get_option')) && ((get_option('is_on_lang_cache',true)=='1') || (get_param_integer('keep_cache',0)==1) || (get_param_integer('cache',0)==1)) && (get_param_integer('keep_cache',NULL)!==0) && (get_param_integer('cache',NULL)!==0);
if ($desire_cache)
{
if ($GLOBALS['IN_MINIKERNEL_VERSION']==0)
{
global $DECACHED_COMCODE_LANG_STRINGS;
// Cleanup language strings
if (!$DECACHED_COMCODE_LANG_STRINGS)
{
$comcode_lang_strings=$GLOBALS['SITE_DB']->query_select('cached_comcode_pages',array('string_index'),array('the_zone'=>'!'),'',NULL,NULL,true);
if (!is_null($comcode_lang_strings))
{
$GLOBALS['SITE_DB']->query_delete('cached_comcode_pages',array('the_zone'=>'!'));
foreach ($comcode_lang_strings as $comcode_lang_string)
{
delete_lang($comcode_lang_string['string_index']);
}
}
$DECACHED_COMCODE_LANG_STRINGS=true;
}
}
$load_target=array();
} else
{
$load_target=&$LANGUAGE_STRINGS_CACHE[$lang];
}
global $FILE_ARRAY;
if ((@is_array($FILE_ARRAY)) && (file_array_exists('lang/'.$lang.'/'.$codename.'.ini')))
{
$lang_file='lang/'.$lang.'/'.$codename.'.ini';
$file=file_array_get($lang_file);
_get_lang_file_map($file,$load_target,NULL,true);
$bad=true;
}
else
{
$bad=true;
$dirty=false;
// Load originals
$lang_file=get_file_base().'/lang/'.$lang.'/'.filter_naughty($codename).'.ini';
if (file_exists($lang_file)) // Non-custom, Proper language
{
_get_lang_file_map($lang_file,$load_target,NULL,false);
$bad=false;
}
// Load overrides now if they are there
if ($type!='lang')
{
$lang_file=get_custom_file_base().'/lang_custom/'.$lang.'/'.$codename.'.ini';
if ((!file_exists($lang_file)) && (get_file_base()!=get_custom_file_base())) $lang_file=get_file_base().'/lang_custom/'.$lang.'/'.$codename.'.ini';
if (!file_exists($lang_file))
{
$lang_file=get_custom_file_base().'/lang_custom/'.$lang.'/'.$codename.'.po';
if (!file_exists($lang_file))
$lang_file=get_file_base().'/lang_custom/'.$lang.'/'.$codename.'-'.strtolower($lang).'.po';
}
}
if (($type!='lang') && (file_exists($lang_file)))
{
_get_lang_file_map($lang_file,$load_target,NULL,false);
$bad=false;
$dirty=true; // Tainted from the official pack, so can't store server wide
}
// NB: Merge op doesn't happen in require_lang. It happens when do_lang fails and then decides it has to force a recursion to do_lang(xx,fallback_lang()) which triggers require_lang(xx,fallback_lang()) when it sees it's not loaded
if (($bad) && ($lang!=fallback_lang())) // Still some hope
{
require_lang($codename,fallback_lang(),$type,$ignore_errors);
$REQUIRE_LANG_LOOP--;
$fallback_cache_path=get_custom_file_base().'/lang_cached/'.fallback_lang().'/'.$codename.'.lcd';
if (file_exists($fallback_cache_path))
{
require_code('files');
$fallback_map=unserialize(file_get_contents($fallback_cache_path));
$sep='<span class="notranslate">----</span>';
$to_translate='';
$i=0;
$from=0;
$lang_codes=array_keys($fallback_map);
foreach ($fallback_map as $value)
{
if (strlen($to_translate.$sep.$to_translate)>=3000)
{
$translated=preg_split('#<span class="notranslate">[^<>]*----[^<>]*</span>#',google_translate($to_translate,$lang));
foreach ($translated as $j=>$t_value)
{
if (strtolower($lang_codes[$from+$j])==$lang_codes[$from+$j]) $t_value=$fallback_map[$lang_codes[$from+$j]];
if ($lang_codes[$from+$j]=='locale') $t_value=strtolower($lang).'_'.strtoupper($lang);
$fallback_map[$lang_codes[$from+$j]]=$t_value;
$load_target[$lang_codes[$from+$j]]=$t_value;
}
$from=$i;
$to_translate='';
}
if ($to_translate!='') $to_translate.=$sep;
$to_translate.=$value;
$i++;
}
$translated=preg_split('#<span class="notranslate">[^<>]*----[^<>]*</span>#',google_translate($to_translate,$lang));
foreach ($translated as $j=>$t_value)
{
if (strtolower($lang_codes[$from+$j])==$lang_codes[$from+$j]) $t_value=$fallback_map[$lang_codes[$from+$j]];
if ($lang_codes[$from+$j]=='locale') $t_value=strtolower($lang).'_'.strtoupper($lang);
$fallback_map[$lang_codes[$from+$j]]=$t_value;
$load_target[$lang_codes[$from+$j]]=$t_value;
}
if ((function_exists('ocp_mb_substr')) && ($codename=='dates'))
{
foreach (array_keys($fallback_map) as $key)
{
if (substr($key,0,3)=='FC_')
{
$test=ocp_mb_substr(trim($fallback_map[substr($key,3)]),0,1,true);
if ($test!==false)
$fallback_map[$key]=$test;
}
}
}
$myfile=fopen($cache_path,'wb');
fwrite($myfile,serialize($fallback_map));
fclose($myfile);
fix_permissions($cache_path);
}
if (!array_key_exists($lang,$LANG_LOADED_LANG)) $LANG_LOADED_LANG[$lang]=array();
$LANG_LOADED_LANG[$lang][$codename]=1;
if (!$bad) $LANGUAGE_STRINGS_CACHE[$lang]+=$fallback_map;
return $bad;
}
if ($bad) // Out of hope
{
if ($ignore_errors) return true;
if (($codename!='critical_error') || ($lang!=get_site_default_lang()))
{
fatal_exit(do_lang_tempcode('MISSING_LANG_FILE',escape_html($codename),escape_html($lang)));
} else
{
critical_error('CRIT_LANG');
}
}
}
if (is_null($GLOBALS['MEM_CACHE']))
{
// Cache
if ($desire_cache)
{
$file=@fopen($cache_path,'wt'); // Will fail if cache dir missing .. e.g. in quick installer
if ($file)
{
if (fwrite($file,serialize($load_target))>0)
{
// Success
fclose($file);
require_code('files');
fix_permissions($cache_path);
} else
{
// Failure
fclose($file);
@unlink($cache_path);
}
}
}
} else
{
persistent_cache_set(array('LANG',$lang,$codename),$load_target,!$dirty);
}
if ($desire_cache) $LANGUAGE_STRINGS_CACHE[$lang]+=$load_target;
return $bad;
}
}