-
Notifications
You must be signed in to change notification settings - Fork 16
/
lang2.php
executable file
·278 lines (245 loc) · 7.96 KB
/
lang2.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php /*
ocPortal
Copyright (c) ocProducts, 2004-2013
See text/EN/licence.txt for full licencing information.
NOTE TO PROGRAMMERS:
Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
**** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****
*/
/**
* @license http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
* @copyright ocProducts Ltd
* @package core
*/
/**
* Get a list of languages files for the given language. ONLY those that are overridden.
*
* @param ?LANGUAGE_NAME The language (NULL: uses the current language)
* @return array The language files
*/
function get_lang_files($lang=NULL)
{
require_code('files');
if (is_null($lang)) $lang=get_site_default_lang();
$_dir=@opendir(get_file_base().'/lang/'.$lang);
$_lang_files=array();
if ($_dir!==false)
{
while (false!==($file=readdir($_dir)))
{
if (($file[0]!='.') && ((substr($file,-4)=='.ini') || (substr($file,-3)=='.po'))/* && (!should_ignore_file(get_file_base().'/lang/'.$lang.'/'.$file,0,0))*/)
{
if (substr($file,-3)=='.po')
{
$file=preg_replace('#\-'.preg_quote(strtolower($lang),'#').'$#','',substr($file,0,strlen($file)-3));
} else
{
$file=substr($file,0,strlen($file)-4);
}
$_lang_files[$file]='lang';
}
}
closedir($_dir);
}
$_dir=@opendir(get_custom_file_base().'/lang_custom/'.$lang);
if ($_dir!==false)
{
while (false!==($file=readdir($_dir)))
{
if (($file[0]!='.') && ((substr($file,-4)=='.ini') || (substr($file,-3)=='.po'))/* && (!should_ignore_file(get_custom_file_base().'/lang_custom/'.$lang.'/'.$file,0,0))*/)
{
if (substr($file,-3)=='.po')
{
$file=preg_replace('#\-'.preg_quote(strtolower($lang),'#').'$#','',substr($file,0,strlen($file)-3));
} else
{
$file=substr($file,0,strlen($file)-4);
}
$_lang_files[$file]='lang_custom';
}
}
closedir($_dir);
}
if (get_file_base()!=get_custom_file_base())
{
$_dir=@opendir(get_file_base().'/lang_custom/'.$lang);
if ($_dir!==false)
{
while (false!==($file=readdir($_dir)))
{
if (($file!='.') && ($file!='..') && ((substr($file,-4)=='.ini') || (substr($file,-3)=='.po')) && (!should_ignore_file(get_file_base().'/lang_custom/'.$lang.'/'.$file,0,0)))
{
if (substr($file,-3)=='.po')
{
$file=preg_replace('#\-'.preg_quote($lang,'#').'$#','',substr($file,0,strlen($file)-3));
} else
{
$file=substr($file,0,strlen($file)-4);
}
$_lang_files[$file]='lang_custom';
}
}
closedir($_dir);
}
}
return $_lang_files;
}
/**
* Search the database to find human-readable names for language IDs.
*
* @param array The language IDs (array of AUTO_LINK)
* @return array Human readable names (List of string against same IDs in input array or null for orphan strings)
*/
function find_lang_content_names($ids)
{
static $langidfields=NULL;
if (is_null($langidfields))
{
$query='SELECT m_name,m_table,m_type FROM '.$GLOBALS['SITE_DB']->get_table_prefix().'db_meta';
$all_fields=$GLOBALS['SITE_DB']->query($query);
$langidfields=array();
foreach ($all_fields as $f)
{
if (substr($f['m_type'],-6)=='_TRANS')
$langidfields[]=array('m_name'=>$f['m_name'],'m_table'=>$f['m_table'],'key'=>'');
}
foreach ($langidfields as $i=>$l)
{
foreach ($all_fields as $f)
{
if (($l['m_table']==$f['m_table']) && (substr($f['m_type'],0,1)=='*') && ($l['key']==''))
$langidfields[$i]['key']=$f['m_name'];
}
}
$langidfields[]=array('m_name'=>'config_value','m_table'=>'config','key'=>'the_name');
}
$ret=array();
foreach ($langidfields as $field)
{
$db=$GLOBALS[((substr($field['m_table'],0,2)=='f_')?'FORUM_DB':'SITE_DB')];
if (is_null($db)) continue; // None forum driver
$or_list='';
foreach ($ids as $id)
{
if (!isset($ret[$id])) // Try and lookup anything not found yet
{
if ($or_list!='') $or_list.=' OR ';
$or_list.=($field['m_table']=='config')?db_string_equal_to($field['m_name'],strval($id)):($field['m_name'].'='.strval($id));
}
}
if ($or_list!='')
{
$test=list_to_map($field['m_name'],$db->query('SELECT * FROM '.$db->get_table_prefix().$field['m_table'].' WHERE '.$or_list));
foreach ($ids as $id)
{
if (array_key_exists($id,$test))
{
$cma_hooks=find_all_hooks('systems','content_meta_aware');
foreach (array_keys($cma_hooks) as $hook)
{
require_code('content');
$ob=get_content_object($hook);
$info=$ob->info();
if ($info['table']==$field['m_table'])
{
if ($info['title_field_dereference'])
{
$ret[$id]=$field['m_table'].' \ '.get_translated_text($test[$id][$info['title_field']]).' \ '.$field['m_name'];
} else
{
$ret[$id]=$field['m_table'].' \ '.(is_integer($test[$id][$info['title_field']])?strval($test[$id][$info['title_field']]):$test[$id][$info['title_field']]).' \ '.$field['m_name'];
}
continue 2;
}
}
$ret[$id]=$field['m_table'].' \ '.(is_integer($test[$id][$field['key']])?strval($test[$id][$field['key']]):$test[$id][$field['key']]).' \ '.$field['m_name'];
} else
{
if (!array_key_exists($id,$ret)) $ret[$id]=NULL;
}
}
}
}
return $ret;
}
/**
* Get a nice formatted XHTML listed language file selector for the given language.
*
* @param ?LANGUAGE_NAME The language (NULL: uses the current language)
* @return tempcode The language file selector
*/
function nice_get_lang_files($lang=NULL)
{
$_lang_files=get_lang_files(is_null($lang)?get_site_default_lang():$lang);
ksort($_lang_files);
require_lang('lang');
$lang_files=new ocp_tempcode();
foreach (array_keys($_lang_files) as $lang_file)
{
if (!is_null($lang))
{
$base_map=get_lang_file_map(fallback_lang(),$lang_file,true);
$criticise_map=get_lang_file_map($lang,$lang_file);
$num_translated=0;
$num_english=count($base_map);
foreach ($base_map as $key=>$val)
{
if (array_key_exists($key,$criticise_map)) $num_translated++;
}
$lang_files->attach(form_input_list_entry($lang_file,false,do_lang_tempcode('TRANSLATION_PROGRESS',escape_html($lang_file),escape_html(integer_format($num_translated)),escape_html(integer_format($num_english)))));
} else
{
$lang_files->attach(form_input_list_entry($lang_file,false,$lang_file));
}
}
return $lang_files;
}
/**
* Get the full name of a language. e.g. 'EN' would become 'English'
*
* @param LANGUAGE_NAME The language
* @return string The full name of the language
*/
function lookup_language_full_name($code)
{
global $LANGS_MAP_CACHE;
if ($LANGS_MAP_CACHE===NULL)
{
require_code('files');
$map_file_a=get_file_base().'/lang/langs.ini';
$map_file_b=get_custom_file_base().'/lang_custom/langs.ini';
if (!is_file($map_file_b)) $map_file_b=$map_file_a;
$LANGS_MAP_CACHE=better_parse_ini_file($map_file_b);
}
return isset($LANGS_MAP_CACHE[$code])?$LANGS_MAP_CACHE[$code]:$code;
}
/**
* Get an array of all the INI description entries in the specified language.
*
* @param LANGUAGE_NAME The language
* @param ?ID_TEXT The language file (NULL: all non-custom language files)
* @return array The language descriptions
*/
function get_lang_file_descriptions($lang,$file=NULL)
{
if (is_null($file))
{
$dh=opendir(get_file_base().'/lang/'.$lang);
$descriptions=array();
while (($f=readdir($dh))!==false)
{
if (substr($f,-4)=='.ini')
$descriptions=array_merge($descriptions,get_lang_file_descriptions($lang,basename($f,'.ini')));
}
return $descriptions;
}
$a=get_custom_file_base().'/lang_custom/'.$lang.'/'.$file.'.ini';
if ((get_custom_file_base()!=get_file_base()) && (!is_file($a)))
$a=get_file_base().'/lang_custom/'.$lang.'/'.$file.'.ini';
$b=(is_file($a))?$a:get_file_base().'/lang/'.$lang.'/'.$file.'.ini';
if (!is_file($b)) $b=get_file_base().'/lang/'.fallback_lang().'/'.$file.'.ini';
$target=array();
require_code('lang_compile');
_get_lang_file_map($b,$target,true);
return $target;
}