1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alixar\Base; |
4
|
|
|
|
5
|
|
|
use Alxarafe\Helpers\Debug; |
6
|
|
|
use Alixar\Helpers\AlDolUtils; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class to manage translations |
10
|
|
|
*/ |
11
|
|
|
class Translate |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
public $dir; // Directories that contains /langs subdirectory |
15
|
|
|
public $defaultlang; // Current language for current user |
16
|
|
|
public $charset_output = 'UTF-8'; // Codage used by "trans" method outputs |
17
|
|
|
public $tab_translate = array(); // Array of all translations key=>value |
18
|
|
|
public $cache_labels = array(); // Array to store result after loading each language file |
19
|
|
|
public $cache_currencies = array(); // Cache for labels return by getLabelFromKey method |
20
|
|
|
private $_tab_loaded = array(); // Cache to store currency symbols |
21
|
|
|
private $cache_currencies_all_loaded = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor |
25
|
|
|
* |
26
|
|
|
* @param string $dir Force directory that contains /langs subdirectory (value is sometimes '..' like into install/* pages or support/* pages). Use '' by default. |
27
|
|
|
* @param Conf $conf Object with Dolibarr configuration |
28
|
|
|
*/ |
29
|
|
|
function __construct($dir, $conf) |
30
|
|
|
{ |
31
|
|
|
Debug::addMessage('Deprecated', 'Unnecessary definition of first parameter ($dir) in Translate'); |
32
|
|
|
$dir = DOL_BASE_PATH; |
33
|
|
|
if (!empty($conf->file->character_set_client)) |
34
|
|
|
$this->charset_output = $conf->file->character_set_client; // If charset output is forced |
35
|
|
|
if ($dir) |
36
|
|
|
$this->dir = array($dir); |
37
|
|
|
else |
38
|
|
|
$this->dir = $conf->file->dol_document_root; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Return active language code for current user |
43
|
|
|
* It's an accessor for this->defaultlang |
44
|
|
|
* |
45
|
|
|
* @param int $mode 0=Long language code, 1=Short language code (en, fr, es, ...) |
46
|
|
|
* @return string Language code used (en_US, en_AU, fr_FR, ...) |
47
|
|
|
*/ |
48
|
|
|
function getDefaultLang($mode = 0) |
49
|
|
|
{ |
50
|
|
|
if (empty($mode)) |
51
|
|
|
return $this->defaultlang; |
52
|
|
|
else |
53
|
|
|
return substr($this->defaultlang, 0, 2); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set accessor for this->defaultlang |
58
|
|
|
* |
59
|
|
|
* @param string $srclang Language to use. If '' or 'auto', we use browser lang. |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
function setDefaultLang($srclang = 'en_US') |
63
|
|
|
{ |
64
|
|
|
global $conf; |
65
|
|
|
|
66
|
|
|
//dol_syslog(get_class($this)."::setDefaultLang srclang=".$srclang,LOG_DEBUG); |
67
|
|
|
// If a module ask to force a priority on langs directories (to use its own lang files) |
68
|
|
|
if (!empty($conf->global->MAIN_FORCELANGDIR)) { |
69
|
|
|
$more = array(); |
70
|
|
|
$i = 0; |
71
|
|
|
foreach ($conf->file->dol_document_root as $dir) { |
72
|
|
|
$newdir = $dir . $conf->global->MAIN_FORCELANGDIR; // For example $conf->global->MAIN_FORCELANGDIR is '/mymodule' meaning we search files into '/mymodule/langs/xx_XX' |
73
|
|
|
if (!in_array($newdir, $this->dir)) { |
74
|
|
|
$more['module_' . $i] = $newdir; |
75
|
|
|
$i++; // We add the forced dir into the array $more. Just after, we add entries into $more to list of lang dir $this->dir. |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
$this->dir = array_merge($more, $this->dir); // Forced dir ($more) are before standard dirs ($this->dir) |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->origlang = $srclang; |
82
|
|
|
|
83
|
|
|
if (empty($srclang) || $srclang == 'auto') { |
84
|
|
|
$langpref = empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? '' : $_SERVER['HTTP_ACCEPT_LANGUAGE']; |
85
|
|
|
$langpref = preg_replace("/;([^,]*)/i", "", $langpref); |
86
|
|
|
$langpref = str_replace("-", "_", $langpref); |
87
|
|
|
$langlist = preg_split("/[;,]/", $langpref); |
88
|
|
|
$codetouse = $langlist[0]; |
89
|
|
|
} else |
90
|
|
|
$codetouse = $srclang; |
91
|
|
|
|
92
|
|
|
// We redefine $srclang |
93
|
|
|
$langpart = explode("_", $codetouse); |
94
|
|
|
//print "Short code before _ : ".$langpart[0].' / Short code after _ : '.$langpart[1].'<br>'; |
95
|
|
|
if (!empty($langpart[1])) { // If it's for a codetouse that is a long code xx_YY |
96
|
|
|
// Array force long code from first part, even if long code is defined |
97
|
|
|
$longforshort = array('ar' => 'ar_SA'); |
98
|
|
|
$longforshortexcep = array('ar_EG'); |
99
|
|
|
if (isset($longforshort[strtolower($langpart[0])]) && !in_array($codetouse, $longforshortexcep)) |
100
|
|
|
$srclang = $longforshort[strtolower($langpart[0])]; |
101
|
|
|
else if (!is_numeric($langpart[1])) { // Second part YY may be a numeric with some Chrome browser |
102
|
|
|
$srclang = strtolower($langpart[0]) . "_" . strtoupper($langpart[1]); |
103
|
|
|
$longforlong = array('no_nb' => 'nb_NO'); |
104
|
|
|
if (isset($longforlong[strtolower($srclang)])) |
105
|
|
|
$srclang = $longforlong[strtolower($srclang)]; |
106
|
|
|
} else |
107
|
|
|
$srclang = strtolower($langpart[0]) . "_" . strtoupper($langpart[0]); |
108
|
|
|
} else { // If it's for a codetouse that is a short code xx |
109
|
|
|
// Array to convert short lang code into long code. |
110
|
|
|
$longforshort = array('ar' => 'ar_SA', 'el' => 'el_GR', 'ca' => 'ca_ES', 'en' => 'en_US', 'nb' => 'nb_NO', 'no' => 'nb_NO'); |
111
|
|
|
if (isset($longforshort[strtolower($langpart[0])])) |
112
|
|
|
$srclang = $longforshort[strtolower($langpart[0])]; |
113
|
|
|
else if (!empty($langpart[0])) |
114
|
|
|
$srclang = strtolower($langpart[0]) . "_" . strtoupper($langpart[0]); |
115
|
|
|
else |
116
|
|
|
$srclang = 'en_US'; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->defaultlang = $srclang; |
120
|
|
|
//print 'this->defaultlang='.$this->defaultlang; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Load translation files. |
125
|
|
|
* |
126
|
|
|
* @param array $domains Array of lang files to load |
127
|
|
|
* @return int <0 if KO, 0 if already loaded or loading not required, >0 if OK |
128
|
|
|
*/ |
129
|
|
|
function loadLangs($domains) |
130
|
|
|
{ |
131
|
|
|
foreach ($domains as $domain) { |
132
|
|
|
$this->load($domain); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Load translation key-value for a particular file, into a memory array. |
138
|
|
|
* If data for file already loaded, do nothing. |
139
|
|
|
* All data in translation array are stored in UTF-8 format. |
140
|
|
|
* tab_loaded is completed with $domain key. |
141
|
|
|
* rule "we keep first entry found with we keep last entry found" so it is probably not what you want to do. |
142
|
|
|
* |
143
|
|
|
* Value for hash are: 1:Loaded from disk, 2:Not found, 3:Loaded from cache |
144
|
|
|
* |
145
|
|
|
* @param string $domain File name to load (.lang file). Must be "file" or "file@module" for module language files: |
146
|
|
|
* If $domain is "file@module" instead of "file" then we look for module lang file |
147
|
|
|
* in htdocs/custom/modules/mymodule/langs/code_CODE/file.lang |
148
|
|
|
* then in htdocs/module/langs/code_CODE/file.lang instead of htdocs/langs/code_CODE/file.lang |
149
|
|
|
* @param integer $alt 0 (try xx_ZZ then 1), 1 (try xx_XX then 2), 2 (try en_US) |
150
|
|
|
* @param int $stopafterdirection Stop when the DIRECTION tag is found (optimize speed) |
151
|
|
|
* @param int $forcelangdir To force a different lang directory |
152
|
|
|
* @param int $loadfromfileonly 1=Do not load overwritten translation from file or old conf. |
153
|
|
|
* @return int <0 if KO, 0 if already loaded or loading not required, >0 if OK |
154
|
|
|
* @see loadLangs |
155
|
|
|
*/ |
156
|
|
|
function load($domain, $alt = 0, $stopafterdirection = 0, $forcelangdir = '', $loadfromfileonly = 0) |
157
|
|
|
{ |
158
|
|
|
global $conf, $db; |
159
|
|
|
|
160
|
|
|
//dol_syslog("Translate::Load Start domain=".$domain." alt=".$alt." forcelangdir=".$forcelangdir." this->defaultlang=".$this->defaultlang); |
161
|
|
|
// Check parameters |
162
|
|
|
if (empty($domain)) { |
163
|
|
|
dol_print_error('', get_class($this) . "::Load ErrorWrongParameters"); |
164
|
|
|
return -1; |
165
|
|
|
} |
166
|
|
|
if ($this->defaultlang == 'none_NONE') |
167
|
|
|
return 0; // Special language code to not translate keys |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
// Load $this->tab_translate[] from database |
171
|
|
|
if (empty($loadfromfileonly) && count($this->tab_translate) == 0) |
172
|
|
|
$this->loadFromDatabase($db); // No translation was never loaded yet, so we load database. |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
$newdomain = $domain; |
176
|
|
|
$modulename = ''; |
177
|
|
|
|
178
|
|
|
// Search if a module directory name is provided into lang file name |
179
|
|
|
if (preg_match('/^([^@]+)@([^@]+)$/i', $domain, $regs)) { |
180
|
|
|
$newdomain = $regs[1]; |
181
|
|
|
$modulename = $regs[2]; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// Check cache |
185
|
|
|
if (!empty($this->_tab_loaded[$newdomain])) { // File already loaded for this domain |
186
|
|
|
//dol_syslog("Translate::Load already loaded for newdomain=".$newdomain); |
187
|
|
|
return 0; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$fileread = 0; |
191
|
|
|
$langofdir = (empty($forcelangdir) ? $this->defaultlang : $forcelangdir); |
192
|
|
|
|
193
|
|
|
// Redefine alt |
194
|
|
|
$langarray = explode('_', $langofdir); |
195
|
|
|
if ($alt < 1 && isset($langarray[1]) && (strtolower($langarray[0]) == strtolower($langarray[1]) || in_array(strtolower($langofdir), array('el_gr')))) |
196
|
|
|
$alt = 1; |
197
|
|
|
if ($alt < 2 && strtolower($langofdir) == 'en_us') |
198
|
|
|
$alt = 2; |
199
|
|
|
|
200
|
|
|
if (empty($langofdir)) { // This may occurs when load is called without setting the language and without providing a value for forcelangdir |
201
|
|
|
dol_syslog("Error: " . get_class($this) . "::Load was called but language was not set yet with langs->setDefaultLang(). Nothing will be loaded.", LOG_WARNING); |
202
|
|
|
return -1; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
foreach ($this->dir as $keydir => $searchdir) { |
206
|
|
|
// Directory of translation files |
207
|
|
|
$file_lang = $searchdir . ($modulename ? '/' . $modulename : '') . "/langs/" . $langofdir . "/" . $newdomain . ".lang"; |
208
|
|
|
$file_lang_osencoded = AlDolUtils::dol_osencode($file_lang); |
209
|
|
|
|
210
|
|
|
$filelangexists = is_file($file_lang_osencoded); |
211
|
|
|
|
212
|
|
|
//dol_syslog(get_class($this).'::Load Try to read for alt='.$alt.' langofdir='.$langofdir.' domain='.$domain.' newdomain='.$newdomain.' modulename='.$modulename.' file_lang='.$file_lang." => filelangexists=".$filelangexists); |
213
|
|
|
//print 'Try to read for alt='.$alt.' langofdir='.$langofdir.' domain='.$domain.' newdomain='.$newdomain.' modulename='.$modulename.' this->_tab_loaded[newdomain]='.$this->_tab_loaded[$newdomain].' file_lang='.$file_lang." => filelangexists=".$filelangexists."\n"; |
214
|
|
|
|
215
|
|
|
if ($filelangexists) { |
216
|
|
|
// TODO Move cache read out of loop on dirs or at least filelangexists |
217
|
|
|
$found = false; |
218
|
|
|
|
219
|
|
|
// Enable caching of lang file in memory (not by default) |
220
|
|
|
$usecachekey = ''; |
221
|
|
|
// Using a memcached server |
222
|
|
|
if (!empty($conf->memcached->enabled) && !empty($conf->global->MEMCACHED_SERVER)) { |
223
|
|
|
$usecachekey = $newdomain . '_' . $langofdir . '_' . md5($file_lang); // Should not contains special chars |
224
|
|
|
} // Using cache with shmop. Speed gain: 40ms - Memory overusage: 200ko (Size of session cache file) |
225
|
|
|
else if (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) { |
226
|
|
|
$usecachekey = $newdomain; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
if ($usecachekey) { |
230
|
|
|
//dol_syslog('Translate::Load we will cache result into usecachekey '.$usecachekey); |
231
|
|
|
//global $aaa; $aaa+=1; |
232
|
|
|
//print $aaa." ".$usecachekey."\n"; |
233
|
|
|
require_once DOL_BASE_PATH . '/core/lib/memory.lib.php'; |
234
|
|
|
$tmparray = dol_getcache($usecachekey); |
235
|
|
|
if (is_array($tmparray) && count($tmparray)) { |
236
|
|
|
$this->tab_translate += $tmparray; // Faster than array_merge($tmparray,$this->tab_translate). Note: If a value already exists into tab_translate, value into tmparaay is not added. |
237
|
|
|
//print $newdomain."\n"; |
238
|
|
|
//var_dump($this->tab_translate); |
239
|
|
|
if ($alt == 2) |
240
|
|
|
$fileread = 1; |
241
|
|
|
$found = true; // Found in dolibarr PHP cache |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
if (!$found) { |
246
|
|
|
if ($fp = @fopen($file_lang, "rt")) { |
247
|
|
|
if ($usecachekey) |
248
|
|
|
$tabtranslatedomain = array(); // To save lang content in cache |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Read each lines until a '=' (with any combination of spaces around it) |
252
|
|
|
* and split the rest until a line feed. |
253
|
|
|
* This is more efficient than fgets + explode + trim by a factor of ~2. |
254
|
|
|
*/ |
255
|
|
|
while ($line = fscanf($fp, "%[^= ]%*[ =]%[^\n]")) { |
256
|
|
|
if (isset($line[1])) { |
257
|
|
|
list($key, $value) = $line; |
258
|
|
|
//if ($domain == 'orders') print "Domain=$domain, found a string for $tab[0] with value $tab[1]. Currently in cache ".$this->tab_translate[$key]."<br>"; |
259
|
|
|
//if ($key == 'Order') print "Domain=$domain, found a string for key=$key=$tab[0] with value $tab[1]. Currently in cache ".$this->tab_translate[$key]."<br>"; |
260
|
|
|
if (empty($this->tab_translate[$key])) { // If translation was already found, we must not continue, even if MAIN_FORCELANGDIR is set (MAIN_FORCELANGDIR is to replace lang dir, not to overwrite entries) |
261
|
|
|
$value = preg_replace('/\\n/', "\n", $value); // Parse and render carriage returns |
262
|
|
|
if ($key == 'DIRECTION') { // This is to declare direction of language |
263
|
|
|
if ($alt < 2 || empty($this->tab_translate[$key])) { // We load direction only for primary files or if not yet loaded |
264
|
|
|
$this->tab_translate[$key] = $value; |
265
|
|
|
if ($stopafterdirection) { |
266
|
|
|
break; // We do not save tab if we stop after DIRECTION |
267
|
|
|
} elseif ($usecachekey) { |
268
|
|
|
$tabtranslatedomain[$key] = $value; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
} elseif ($key[0] == '#') { |
272
|
|
|
continue; |
273
|
|
|
} else { |
274
|
|
|
$this->tab_translate[$key] = $value; |
275
|
|
|
//if ($domain == 'orders') print "$tab[0] value $value<br>"; |
276
|
|
|
if ($usecachekey) { |
277
|
|
|
$tabtranslatedomain[$key] = $value; |
278
|
|
|
} // To save lang content in cache |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
fclose($fp); |
284
|
|
|
$fileread = 1; |
285
|
|
|
|
286
|
|
|
// TODO Move cache write out of loop on dirs |
287
|
|
|
// To save lang content for usecachekey into cache |
288
|
|
|
if ($usecachekey && count($tabtranslatedomain)) { |
289
|
|
|
$ressetcache = dol_setcache($usecachekey, $tabtranslatedomain); |
290
|
|
|
if ($ressetcache < 0) { |
291
|
|
|
$error = 'Failed to set cache for usecachekey=' . $usecachekey . ' result=' . $ressetcache; |
292
|
|
|
dol_syslog($error, LOG_ERR); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
if (empty($conf->global->MAIN_FORCELANGDIR)) |
297
|
|
|
break; // Break loop on each root dir. If a module has forced dir, we do not stop loop. |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
// Now we complete with next file (fr_CA->fr_FR, es_MX->ex_ES, ...) |
304
|
|
|
if ($alt == 0) { |
305
|
|
|
// This function MUST NOT contains call to syslog |
306
|
|
|
//dol_syslog("Translate::Load loading alternate translation file (to complete ".$this->defaultlang."/".$newdomain.".lang file)", LOG_DEBUG); |
307
|
|
|
$langofdir = strtolower($langarray[0]) . '_' . strtoupper($langarray[0]); |
308
|
|
|
if ($langofdir == 'el_EL') |
309
|
|
|
$langofdir = 'el_GR'; // main parent for el_CY is not 'el_EL' but 'el_GR' |
310
|
|
|
if ($langofdir == 'ar_AR') |
311
|
|
|
$langofdir = 'ar_SA'; // main parent for ar_EG is not 'ar_AR' but 'ar_SA' |
312
|
|
|
$this->load($domain, $alt + 1, $stopafterdirection, $langofdir); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
// Now we complete with reference file (en_US) |
316
|
|
|
if ($alt == 1) { |
317
|
|
|
// This function MUST NOT contains call to syslog |
318
|
|
|
//dol_syslog("Translate::Load loading alternate translation file (to complete ".$this->defaultlang."/".$newdomain.".lang file)", LOG_DEBUG); |
319
|
|
|
$langofdir = 'en_US'; |
320
|
|
|
$this->load($domain, $alt + 1, $stopafterdirection, $langofdir); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
// We are in the pass of the reference file. No more files to scan to complete. |
324
|
|
|
if ($alt == 2) { |
325
|
|
|
if ($fileread) |
326
|
|
|
$this->_tab_loaded[$newdomain] = 1; // Set domain file as found so loaded |
327
|
|
|
|
328
|
|
|
if (empty($this->_tab_loaded[$newdomain])) |
329
|
|
|
$this->_tab_loaded[$newdomain] = 2; // Set this file as not found |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
// This part is deprecated and replaced with table llx_overwrite_trans |
333
|
|
|
// Kept for backward compatibility. |
334
|
|
|
if (empty($loadfromfileonly)) { |
335
|
|
|
$overwritekey = 'MAIN_OVERWRITE_TRANS_' . $this->defaultlang; |
336
|
|
|
if (!empty($conf->global->$overwritekey)) { // Overwrite translation with key1:newstring1,key2:newstring2 |
337
|
|
|
// Overwrite translation with param MAIN_OVERWRITE_TRANS_xx_XX |
338
|
|
|
$tmparray = explode(',', $conf->global->$overwritekey); |
339
|
|
|
foreach ($tmparray as $tmp) { |
340
|
|
|
$tmparray2 = explode(':', $tmp); |
341
|
|
|
if (!empty($tmparray2[1])) |
342
|
|
|
$this->tab_translate[$tmparray2[0]] = $tmparray2[1]; |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
// Check to be sure that SeparatorDecimal differs from SeparatorThousand |
348
|
|
|
if (!empty($this->tab_translate["SeparatorDecimal"]) && !empty($this->tab_translate["SeparatorThousand"]) && $this->tab_translate["SeparatorDecimal"] == $this->tab_translate["SeparatorThousand"]) |
349
|
|
|
$this->tab_translate["SeparatorThousand"] = ''; |
350
|
|
|
|
351
|
|
|
return 1; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Load translation key-value from database into a memory array. |
356
|
|
|
* If data already loaded, do nothing. |
357
|
|
|
* All data in translation array are stored in UTF-8 format. |
358
|
|
|
* tab_loaded is completed with $domain key. |
359
|
|
|
* rule "we keep first entry found with we keep last entry found" so it is probably not what you want to do. |
360
|
|
|
* |
361
|
|
|
* Value for hash are: 1:Loaded from disk, 2:Not found, 3:Loaded from cache |
362
|
|
|
* |
363
|
|
|
* @param Database $db Database handler |
|
|
|
|
364
|
|
|
* @return int <0 if KO, 0 if already loaded or loading not required, >0 if OK |
365
|
|
|
*/ |
366
|
|
|
function loadFromDatabase($db) |
367
|
|
|
{ |
368
|
|
|
global $conf; |
369
|
|
|
|
370
|
|
|
$domain = 'database'; |
371
|
|
|
|
372
|
|
|
// Check parameters |
373
|
|
|
if (empty($db)) |
374
|
|
|
return 0; // Database handler can't be used |
375
|
|
|
|
376
|
|
|
|
377
|
|
|
//dol_syslog("Translate::Load Start domain=".$domain." alt=".$alt." forcelangdir=".$forcelangdir." this->defaultlang=".$this->defaultlang); |
378
|
|
|
|
379
|
|
|
$newdomain = $domain; |
380
|
|
|
|
381
|
|
|
// Check cache |
382
|
|
|
if (!empty($this->_tab_loaded[$newdomain])) { // File already loaded for this domain 'database' |
383
|
|
|
//dol_syslog("Translate::Load already loaded for newdomain=".$newdomain); |
384
|
|
|
return 0; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
$this->_tab_loaded[$newdomain] = 1; // We want to be sure this function is called once only for domain 'database' |
388
|
|
|
|
389
|
|
|
$fileread = 0; |
390
|
|
|
$langofdir = $this->defaultlang; |
391
|
|
|
|
392
|
|
|
if (empty($langofdir)) { // This may occurs when load is called without setting the language and without providing a value for forcelangdir |
393
|
|
|
dol_syslog("Error: " . get_class($this) . "::Load was called but language was not set yet with langs->setDefaultLang(). Nothing will be loaded.", LOG_WARNING); |
394
|
|
|
return -1; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
// TODO Move cache read out of loop on dirs or at least filelangexists |
398
|
|
|
$found = false; |
399
|
|
|
|
400
|
|
|
// Enable caching of lang file in memory (not by default) |
401
|
|
|
$usecachekey = ''; |
402
|
|
|
// Using a memcached server |
403
|
|
|
if (!empty($conf->memcached->enabled) && !empty($conf->global->MEMCACHED_SERVER)) { |
404
|
|
|
$usecachekey = $newdomain . '_' . $langofdir; // Should not contains special chars |
405
|
|
|
} // Using cache with shmop. Speed gain: 40ms - Memory overusage: 200ko (Size of session cache file) |
406
|
|
|
else if (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) { |
407
|
|
|
$usecachekey = $newdomain; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
if ($usecachekey) { |
411
|
|
|
//dol_syslog('Translate::Load we will cache result into usecachekey '.$usecachekey); |
412
|
|
|
//global $aaa; $aaa+=1; |
413
|
|
|
//print $aaa." ".$usecachekey."\n"; |
414
|
|
|
require_once DOL_BASE_PATH . '/core/lib/memory.lib.php'; |
415
|
|
|
$tmparray = dol_getcache($usecachekey); |
416
|
|
|
if (is_array($tmparray) && count($tmparray)) { |
417
|
|
|
$this->tab_translate += $tmparray; // Faster than array_merge($tmparray,$this->tab_translate). Note: If a valuer already exists into tab_translate, value into tmparaay is not added. |
418
|
|
|
//print $newdomain."\n"; |
419
|
|
|
//var_dump($this->tab_translate); |
420
|
|
|
$fileread = 1; |
421
|
|
|
$found = true; // Found in dolibarr PHP cache |
422
|
|
|
} |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
if (!$found && !empty($conf->global->MAIN_ENABLE_OVERWRITE_TRANSLATION)) { |
426
|
|
|
// Overwrite translation with database read |
427
|
|
|
$sql = "SELECT transkey, transvalue FROM " . MAIN_DB_PREFIX . "overwrite_trans where lang='" . $db->escape($this->defaultlang) . "'"; |
428
|
|
|
$resql = $db->query($sql); |
429
|
|
|
|
430
|
|
|
if ($resql) { |
431
|
|
|
$num = $db->num_rows($resql); |
432
|
|
|
if ($num) { |
433
|
|
|
if ($usecachekey) |
434
|
|
|
$tabtranslatedomain = array(); // To save lang content in cache |
435
|
|
|
|
436
|
|
|
$i = 0; |
437
|
|
|
while ($i < $num) { // Ex: Need 225ms for all fgets on all lang file for Third party page. Same speed than file_get_contents |
438
|
|
|
$obj = $db->fetch_object($resql); |
439
|
|
|
|
440
|
|
|
$key = $obj->transkey; |
441
|
|
|
$value = $obj->transvalue; |
442
|
|
|
|
443
|
|
|
//print "Domain=$domain, found a string for $tab[0] with value $tab[1]<br>"; |
444
|
|
|
if (empty($this->tab_translate[$key])) { // If translation was already found, we must not continue, even if MAIN_FORCELANGDIR is set (MAIN_FORCELANGDIR is to replace lang dir, not to overwrite entries) |
445
|
|
|
$value = trim(preg_replace('/\\n/', "\n", $value)); |
446
|
|
|
|
447
|
|
|
$this->tab_translate[$key] = $value; |
448
|
|
|
if ($usecachekey) |
449
|
|
|
$tabtranslatedomain[$key] = $value; // To save lang content in cache |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
$i++; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
$fileread = 1; |
456
|
|
|
|
457
|
|
|
// TODO Move cache write out of loop on dirs |
458
|
|
|
// To save lang content for usecachekey into cache |
459
|
|
|
if ($usecachekey && count($tabtranslatedomain)) { |
460
|
|
|
$ressetcache = dol_setcache($usecachekey, $tabtranslatedomain); |
461
|
|
|
if ($ressetcache < 0) { |
462
|
|
|
$error = 'Failed to set cache for usecachekey=' . $usecachekey . ' result=' . $ressetcache; |
463
|
|
|
dol_syslog($error, LOG_ERR); |
464
|
|
|
} |
465
|
|
|
} |
466
|
|
|
} |
467
|
|
|
} else { |
468
|
|
|
dol_print_error($db); |
469
|
|
|
} |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
if ($fileread) |
473
|
|
|
$this->_tab_loaded[$newdomain] = 1; // Set domain file as loaded |
474
|
|
|
|
475
|
|
|
if (empty($this->_tab_loaded[$newdomain])) |
476
|
|
|
$this->_tab_loaded[$newdomain] = 2; // Marque ce cas comme non trouve (no lines found for language) |
477
|
|
|
|
478
|
|
|
return 1; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* Return translation of a key depending on country |
483
|
|
|
* |
484
|
|
|
* @param string $str string root to translate |
485
|
|
|
* @param string $countrycode country code (FR, ...) |
486
|
|
|
* @return string translated string |
487
|
|
|
*/ |
488
|
|
|
function transcountry($str, $countrycode) |
489
|
|
|
{ |
490
|
|
|
if ($this->tab_translate["$str$countrycode"]) |
491
|
|
|
return $this->trans("$str$countrycode"); |
492
|
|
|
else |
493
|
|
|
return $this->trans($str); |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* Return text translated of text received as parameter (and encode it into HTML) |
498
|
|
|
* Si il n'y a pas de correspondance pour ce texte, on cherche dans fichier alternatif |
499
|
|
|
* et si toujours pas trouve, il est retourne tel quel |
500
|
|
|
* Les parametres de cette methode peuvent contenir de balises HTML. |
501
|
|
|
* |
502
|
|
|
* @param string $key Key to translate |
503
|
|
|
* @param string $param1 chaine de param1 |
504
|
|
|
* @param string $param2 chaine de param2 |
505
|
|
|
* @param string $param3 chaine de param3 |
506
|
|
|
* @param string $param4 chaine de param4 |
507
|
|
|
* @param int $maxsize Max length of text |
508
|
|
|
* @return string Translated string (encoded into HTML entities and UTF8) |
509
|
|
|
*/ |
510
|
|
|
function trans($key, $param1 = '', $param2 = '', $param3 = '', $param4 = '', $maxsize = 0) |
511
|
|
|
{ |
512
|
|
|
global $conf; |
513
|
|
|
|
514
|
|
|
if (!empty($this->tab_translate[$key])) { // Translation is available |
515
|
|
|
$str = $this->tab_translate[$key]; |
516
|
|
|
|
517
|
|
|
// Make some string replacement after translation |
518
|
|
|
$replacekey = 'MAIN_REPLACE_TRANS_' . $this->defaultlang; |
519
|
|
|
if (!empty($conf->global->$replacekey)) { // Replacement translation variable with string1:newstring1;string2:newstring2 |
520
|
|
|
$tmparray = explode(';', $conf->global->$replacekey); |
521
|
|
|
foreach ($tmparray as $tmp) { |
522
|
|
|
$tmparray2 = explode(':', $tmp); |
523
|
|
|
$str = preg_replace('/' . preg_quote($tmparray2[0]) . '/', $tmparray2[1], $str); |
524
|
|
|
} |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
if (!preg_match('/^Format/', $key)) { |
528
|
|
|
//print $str; |
529
|
|
|
$str = sprintf($str, $param1, $param2, $param3, $param4); // Replace %s and %d except for FormatXXX strings. |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
if ($maxsize) |
533
|
|
|
$str = dol_trunc($str, $maxsize); |
534
|
|
|
|
535
|
|
|
// We replace some HTML tags by __xx__ to avoid having them encoded by htmlentities |
536
|
|
|
$str = str_replace(array('<', '>', '"',), array('__lt__', '__gt__', '__quot__'), $str); |
537
|
|
|
|
538
|
|
|
// Crypt string into HTML |
539
|
|
|
$str = htmlentities($str, ENT_COMPAT, $this->charset_output); // Do not convert simple quotes in translation (strings in html are enmbraced by "). Use dol_escape_htmltag around text in HTML content |
540
|
|
|
// Restore HTML tags |
541
|
|
|
$str = str_replace(array('__lt__', '__gt__', '__quot__'), array('<', '>', '"',), $str); |
542
|
|
|
|
543
|
|
|
return $str; |
544
|
|
|
} else { // Translation is not available |
545
|
|
|
//if ($key[0] == '$') { return dol_eval($key,1); } |
546
|
|
|
return $this->getTradFromKey($key); |
547
|
|
|
} |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
/** |
551
|
|
|
* Return translated value of key for special keys ("Currency...", "Civility...", ...). |
552
|
|
|
* Search in lang file, then into database. Key must be any complete entry into lang file: CurrencyEUR, ... |
553
|
|
|
* If not found, return key. |
554
|
|
|
* The string return is not formated (translated with transnoentitiesnoconv) |
555
|
|
|
* NOTE: To avoid infinite loop (getLabelFromKey->transnoentities->getTradFromKey), if you modify this function, |
556
|
|
|
* check that getLabelFromKey is not called with same value than input. |
557
|
|
|
* |
558
|
|
|
* @param string $key Key to translate |
559
|
|
|
* @return string Translated string (translated with transnoentitiesnoconv) |
560
|
|
|
*/ |
561
|
|
|
private function getTradFromKey($key) |
562
|
|
|
{ |
563
|
|
|
global $conf, $db; |
564
|
|
|
|
565
|
|
|
if (!is_string($key)) |
566
|
|
|
return 'ErrorBadValueForParamNotAString'; // Avoid multiple errors with code not using function correctly. |
567
|
|
|
|
568
|
|
|
$newstr = $key; |
569
|
|
|
if (preg_match('/^Civility([0-9A-Z]+)$/i', $key, $reg)) { |
570
|
|
|
$newstr = $this->getLabelFromKey($db, $reg[1], 'c_civility', 'code', 'label'); |
571
|
|
|
} elseif (preg_match('/^Currency([A-Z][A-Z][A-Z])$/i', $key, $reg)) { |
572
|
|
|
$newstr = $this->getLabelFromKey($db, $reg[1], 'c_currencies', 'code_iso', 'label'); |
573
|
|
|
} elseif (preg_match('/^SendingMethod([0-9A-Z]+)$/i', $key, $reg)) { |
574
|
|
|
$newstr = $this->getLabelFromKey($db, $reg[1], 'c_shipment_mode', 'code', 'libelle'); |
575
|
|
|
} elseif (preg_match('/^PaymentTypeShort([0-9A-Z]+)$/i', $key, $reg)) { |
576
|
|
|
$newstr = $this->getLabelFromKey($db, $reg[1], 'c_paiement', 'code', 'libelle', '', 1); |
577
|
|
|
} elseif (preg_match('/^OppStatus([0-9A-Z]+)$/i', $key, $reg)) { |
578
|
|
|
$newstr = $this->getLabelFromKey($db, $reg[1], 'c_lead_status', 'code', 'label'); |
579
|
|
|
} elseif (preg_match('/^OrderSource([0-9A-Z]+)$/i', $key, $reg)) { |
580
|
|
|
// TODO OrderSourceX must be replaced with content of table llx_c_input_reason or llx_c_input_method |
581
|
|
|
//$newstr=$this->getLabelFromKey($db,$reg[1],'c_ordersource','code','label'); |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
/* Disabled. There is too many cases where translation of $newstr is not defined is normal (like when output with setEventMessage an already translated string) |
585
|
|
|
if (! empty($conf->global->MAIN_FEATURES_LEVEL) && $conf->global->MAIN_FEATURES_LEVEL >= 2) |
586
|
|
|
{ |
587
|
|
|
dol_syslog(__METHOD__." MAIN_FEATURES_LEVEL=DEVELOP: missing translation for key '".$newstr."' in ".$_SERVER["PHP_SELF"], LOG_DEBUG); |
588
|
|
|
} */ |
589
|
|
|
|
590
|
|
|
return $newstr; |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
/** |
594
|
|
|
* Return a label for a key. |
595
|
|
|
* Search into translation array, then into cache, then if still not found, search into database. |
596
|
|
|
* Store key-label found into cache variable $this->cache_labels to save SQL requests to get labels. |
597
|
|
|
* |
598
|
|
|
* @param DoliDB $db Database handler |
|
|
|
|
599
|
|
|
* @param string $key Translation key to get label (key in language file) |
600
|
|
|
* @param string $tablename Table name without prefix |
601
|
|
|
* @param string $fieldkey Field for key |
602
|
|
|
* @param string $fieldlabel Field for label |
603
|
|
|
* @param string $keyforselect Use another value than the translation key for the where into select |
604
|
|
|
* @param int $filteronentity Use a filter on entity |
605
|
|
|
* @return string Label in UTF8 (but without entities) |
606
|
|
|
* @see dol_getIdFromCode |
607
|
|
|
*/ |
608
|
|
|
function getLabelFromKey($db, $key, $tablename, $fieldkey, $fieldlabel, $keyforselect = '', $filteronentity = 0) |
609
|
|
|
{ |
610
|
|
|
// If key empty |
611
|
|
|
if ($key == '') |
612
|
|
|
return ''; |
613
|
|
|
|
614
|
|
|
//print 'param: '.$key.'-'.$keydatabase.'-'.$this->trans($key); exit; |
615
|
|
|
// Check if a translation is available (this can call getTradFromKey) |
616
|
|
|
$tmp = $this->transnoentitiesnoconv($key); |
617
|
|
|
if ($tmp != $key && $tmp != 'ErrorBadValueForParamNotAString') { |
618
|
|
|
return $tmp; // Found in language array |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
// Check in cache |
622
|
|
|
if (isset($this->cache_labels[$tablename][$key])) { // Can be defined to 0 or '' |
623
|
|
|
return $this->cache_labels[$tablename][$key]; // Found in cache |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
$sql = "SELECT " . $fieldlabel . " as label"; |
627
|
|
|
$sql .= " FROM " . MAIN_DB_PREFIX . $tablename; |
628
|
|
|
$sql .= " WHERE " . $fieldkey . " = '" . $db->escape($keyforselect ? $keyforselect : $key) . "'"; |
629
|
|
|
if ($filteronentity) |
630
|
|
|
$sql .= " AND entity IN (" . getEntity($tablename) . ')'; |
631
|
|
|
dol_syslog(get_class($this) . '::getLabelFromKey', LOG_DEBUG); |
632
|
|
|
$resql = $db->query($sql); |
633
|
|
|
if ($resql) { |
634
|
|
|
$obj = $db->fetch_object($resql); |
635
|
|
|
if ($obj) |
636
|
|
|
$this->cache_labels[$tablename][$key] = $obj->label; |
637
|
|
|
else |
638
|
|
|
$this->cache_labels[$tablename][$key] = $key; |
639
|
|
|
|
640
|
|
|
$db->free($resql); |
641
|
|
|
return $this->cache_labels[$tablename][$key]; |
642
|
|
|
} else { |
643
|
|
|
$this->error = $db->lasterror(); |
644
|
|
|
return -1; |
645
|
|
|
} |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
/** |
649
|
|
|
* Return translated value of a text string |
650
|
|
|
* Si il n'y a pas de correspondance pour ce texte, on cherche dans fichier alternatif |
651
|
|
|
* et si toujours pas trouve, il est retourne tel quel. |
652
|
|
|
* No convert to encoding charset of lang object is done. |
653
|
|
|
* Parameters of this method must not contains any HTML tags. |
654
|
|
|
* |
655
|
|
|
* @param string $key Key to translate |
656
|
|
|
* @param string $param1 chaine de param1 |
657
|
|
|
* @param string $param2 chaine de param2 |
658
|
|
|
* @param string $param3 chaine de param3 |
659
|
|
|
* @param string $param4 chaine de param4 |
660
|
|
|
* @param string $param5 chaine de param5 |
661
|
|
|
* @return string Translated string |
662
|
|
|
*/ |
663
|
|
|
function transnoentitiesnoconv($key, $param1 = '', $param2 = '', $param3 = '', $param4 = '', $param5 = '') |
664
|
|
|
{ |
665
|
|
|
global $conf; |
666
|
|
|
|
667
|
|
|
if (!empty($this->tab_translate[$key])) { // Translation is available |
668
|
|
|
$str = $this->tab_translate[$key]; |
669
|
|
|
|
670
|
|
|
// Make some string replacement after translation |
671
|
|
|
$replacekey = 'MAIN_REPLACE_TRANS_' . $this->defaultlang; |
672
|
|
|
if (!empty($conf->global->$replacekey)) { // Replacement translation variable with string1:newstring1;string2:newstring2 |
673
|
|
|
$tmparray = explode(';', $conf->global->$replacekey); |
674
|
|
|
foreach ($tmparray as $tmp) { |
675
|
|
|
$tmparray2 = explode(':', $tmp); |
676
|
|
|
$str = preg_replace('/' . preg_quote($tmparray2[0]) . '/', $tmparray2[1], $str); |
677
|
|
|
} |
678
|
|
|
} |
679
|
|
|
|
680
|
|
|
if (!preg_match('/^Format/', $key)) { |
681
|
|
|
//print $str; |
682
|
|
|
$str = sprintf($str, $param1, $param2, $param3, $param4, $param5); // Replace %s and %d except for FormatXXX strings. |
683
|
|
|
} |
684
|
|
|
|
685
|
|
|
return $str; |
686
|
|
|
} else { |
687
|
|
|
if ($key[0] == '$') { |
688
|
|
|
return dol_eval($key, 1); |
689
|
|
|
} |
690
|
|
|
return $this->getTradFromKey($key); |
691
|
|
|
} |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
/** |
695
|
|
|
* Retourne la version traduite du texte passe en parametre complete du code pays |
696
|
|
|
* |
697
|
|
|
* @param string $str string root to translate |
698
|
|
|
* @param string $countrycode country code (FR, ...) |
699
|
|
|
* @return string translated string |
700
|
|
|
*/ |
701
|
|
|
function transcountrynoentities($str, $countrycode) |
702
|
|
|
{ |
703
|
|
|
if ($this->tab_translate["$str$countrycode"]) |
704
|
|
|
return $this->transnoentities("$str$countrycode"); |
705
|
|
|
else |
706
|
|
|
return $this->transnoentities($str); |
707
|
|
|
} |
708
|
|
|
|
709
|
|
|
/** |
710
|
|
|
* Return translated value of a text string |
711
|
|
|
* Si il n'y a pas de correspondance pour ce texte, on cherche dans fichier alternatif |
712
|
|
|
* et si toujours pas trouve, il est retourne tel quel. |
713
|
|
|
* Parameters of this method must not contains any HTML tags. |
714
|
|
|
* |
715
|
|
|
* @param string $key Key to translate |
716
|
|
|
* @param string $param1 chaine de param1 |
717
|
|
|
* @param string $param2 chaine de param2 |
718
|
|
|
* @param string $param3 chaine de param3 |
719
|
|
|
* @param string $param4 chaine de param4 |
720
|
|
|
* @param string $param5 chaine de param5 |
721
|
|
|
* @return string Translated string (encoded into UTF8) |
722
|
|
|
*/ |
723
|
|
|
function transnoentities($key, $param1 = '', $param2 = '', $param3 = '', $param4 = '', $param5 = '') |
724
|
|
|
{ |
725
|
|
|
return $this->convToOutputCharset($this->transnoentitiesnoconv($key, $param1, $param2, $param3, $param4, $param5)); |
726
|
|
|
} |
727
|
|
|
|
728
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
729
|
|
|
|
730
|
|
|
/** |
731
|
|
|
* Convert a string into output charset (this->charset_output that should be defined to conf->file->character_set_client) |
732
|
|
|
* |
733
|
|
|
* @param string $str String to convert |
734
|
|
|
* @param string $pagecodefrom Page code of src string |
735
|
|
|
* @return string Converted string |
736
|
|
|
*/ |
737
|
|
|
function convToOutputCharset($str, $pagecodefrom = 'UTF-8') |
738
|
|
|
{ |
739
|
|
|
if ($pagecodefrom == 'ISO-8859-1' && $this->charset_output == 'UTF-8') |
740
|
|
|
$str = utf8_encode($str); |
741
|
|
|
if ($pagecodefrom == 'UTF-8' && $this->charset_output == 'ISO-8859-1') |
742
|
|
|
$str = utf8_decode(str_replace('€', chr(128), $str)); |
743
|
|
|
return $str; |
744
|
|
|
} |
745
|
|
|
|
746
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
747
|
|
|
|
748
|
|
|
/** |
749
|
|
|
* Return list of all available languages |
750
|
|
|
* |
751
|
|
|
* @param string $langdir Directory to scan |
752
|
|
|
* @param integer $maxlength Max length for each value in combo box (will be truncated) |
753
|
|
|
* @param int $usecode 1=Show code instead of country name for language variant, 2=Show only code |
754
|
|
|
* @return array List of languages |
755
|
|
|
*/ |
756
|
|
|
function get_available_languages($langdir = DOL_DOCUMENT_ROOT, $maxlength = 0, $usecode = 0) |
757
|
|
|
{ |
758
|
|
|
// phpcs:enable |
759
|
|
|
global $conf; |
760
|
|
|
|
761
|
|
|
if ($langdir == DOL_DOCUMENT_ROOT) { |
762
|
|
|
$langdir = DOL_BASE_PATH; |
763
|
|
|
} |
764
|
|
|
|
765
|
|
|
// We scan directory langs to detect available languages |
766
|
|
|
$handle = opendir($langdir . "/langs"); |
767
|
|
|
$langs_available = array(); |
768
|
|
|
while ($dir = trim(readdir($handle))) { |
769
|
|
|
if (preg_match('/^[a-z]+_[A-Z]+/i', $dir)) { |
770
|
|
|
$this->load("languages"); |
771
|
|
|
|
772
|
|
|
if ($usecode == 2) { |
773
|
|
|
$langs_available[$dir] = $dir; |
774
|
|
|
} |
775
|
|
|
if ($usecode == 1 || !empty($conf->global->MAIN_SHOW_LANGUAGE_CODE)) { |
776
|
|
|
$langs_available[$dir] = $dir . ': ' . dol_trunc($this->trans('Language_' . $dir), $maxlength); |
777
|
|
|
} else { |
778
|
|
|
$langs_available[$dir] = $this->trans('Language_' . $dir); |
779
|
|
|
} |
780
|
|
|
} |
781
|
|
|
} |
782
|
|
|
return $langs_available; |
783
|
|
|
} |
784
|
|
|
|
785
|
|
|
/** |
786
|
|
|
* Return if a filename $filename exists for current language (or alternate language) |
787
|
|
|
* |
788
|
|
|
* @param string $filename Language filename to search |
789
|
|
|
* @param integer $searchalt Search also alernate language file |
790
|
|
|
* @return boolean true if exists and readable |
791
|
|
|
*/ |
792
|
|
|
function file_exists($filename, $searchalt = 0) |
793
|
|
|
{ |
794
|
|
|
// phpcs:enable |
795
|
|
|
// Test si fichier dans repertoire de la langue |
796
|
|
|
foreach ($this->dir as $searchdir) { |
797
|
|
|
if (is_readable(dol_osencode($searchdir . "/langs/" . $this->defaultlang . "/" . $filename))) |
798
|
|
|
return true; |
799
|
|
|
|
800
|
|
|
if ($searchalt) { |
801
|
|
|
// Test si fichier dans repertoire de la langue alternative |
802
|
|
|
if ($this->defaultlang != "en_US") |
803
|
|
|
$filenamealt = $searchdir . "/langs/en_US/" . $filename; |
804
|
|
|
//else $filenamealt = $searchdir."/langs/fr_FR/".$filename; |
805
|
|
|
if (is_readable(dol_osencode($filenamealt))) |
806
|
|
|
return true; |
807
|
|
|
} |
808
|
|
|
} |
809
|
|
|
|
810
|
|
|
return false; |
811
|
|
|
} |
812
|
|
|
|
813
|
|
|
/** |
814
|
|
|
* Return full text translated to language label for a key. Store key-label in a cache. |
815
|
|
|
* This function need module "numberwords" to be installed. If not it will return |
816
|
|
|
* same number (this module is not provided by default as it use non GPL source code). |
817
|
|
|
* |
818
|
|
|
* @param int $number Number to encode in full text |
819
|
|
|
* @param int $isamount 1=It's an amount, 0=it's just a number |
820
|
|
|
* @return string Label translated in UTF8 (but without entities) |
821
|
|
|
* 10 if setDefaultLang was en_US => ten |
822
|
|
|
* 123 if setDefaultLang was fr_FR => cent vingt trois |
823
|
|
|
*/ |
824
|
|
|
function getLabelFromNumber($number, $isamount = 0) |
825
|
|
|
{ |
826
|
|
|
global $conf; |
827
|
|
|
|
828
|
|
|
$newnumber = $number; |
829
|
|
|
|
830
|
|
|
$dirsubstitutions = array_merge(array(), $conf->modules_parts['substitutions']); |
831
|
|
|
foreach ($dirsubstitutions as $reldir) { |
832
|
|
|
$dir = dol_buildpath($reldir, 0); |
833
|
|
|
$newdir = dol_osencode($dir); |
834
|
|
|
|
835
|
|
|
// Check if directory exists |
836
|
|
|
if (!is_dir($newdir)) |
837
|
|
|
continue; // We must not use dol_is_dir here, function may not be loaded |
838
|
|
|
|
839
|
|
|
$fonc = 'numberwords'; |
840
|
|
|
if (file_exists($newdir . '/functions_' . $fonc . '.lib.php')) { |
841
|
|
|
include_once $newdir . '/functions_' . $fonc . '.lib.php'; |
842
|
|
|
$newnumber = numberwords_getLabelFromNumber($this, $number, $isamount); |
|
|
|
|
843
|
|
|
break; |
844
|
|
|
} |
845
|
|
|
} |
846
|
|
|
|
847
|
|
|
return $newnumber; |
848
|
|
|
} |
849
|
|
|
|
850
|
|
|
/** |
851
|
|
|
* Return a currency code into its symbol |
852
|
|
|
* |
853
|
|
|
* @param string $currency_code Currency Code |
854
|
|
|
* @param string $amount If not '', show currency + amount according to langs ($10, 10€). |
855
|
|
|
* @return string Amount + Currency symbol encoded into UTF8 |
856
|
|
|
* @deprecated Use method price to output a price |
857
|
|
|
* @see price() |
858
|
|
|
*/ |
859
|
|
|
function getCurrencyAmount($currency_code, $amount) |
860
|
|
|
{ |
861
|
|
|
$symbol = $this->getCurrencySymbol($currency_code); |
862
|
|
|
|
863
|
|
|
if (in_array($currency_code, array('USD'))) |
864
|
|
|
return $symbol . $amount; |
865
|
|
|
else |
866
|
|
|
return $amount . $symbol; |
867
|
|
|
} |
868
|
|
|
|
869
|
|
|
/** |
870
|
|
|
* Return a currency code into its symbol. |
871
|
|
|
* If mb_convert_encoding is not available, return currency code. |
872
|
|
|
* |
873
|
|
|
* @param string $currency_code Currency code |
874
|
|
|
* @param integer $forceloadall 1=Force to load all currencies into cache. We know we need to use all of them. By default read and cache only required currency. |
875
|
|
|
* @return string Currency symbol encoded into UTF8 |
876
|
|
|
*/ |
877
|
|
|
function getCurrencySymbol($currency_code, $forceloadall = 0) |
878
|
|
|
{ |
879
|
|
|
$currency_sign = ''; // By default return iso code |
880
|
|
|
|
881
|
|
|
if (function_exists("mb_convert_encoding")) { |
882
|
|
|
$this->loadCacheCurrencies($forceloadall ? '' : $currency_code); |
883
|
|
|
|
884
|
|
|
if (isset($this->cache_currencies[$currency_code]) && !empty($this->cache_currencies[$currency_code]['unicode']) && is_array($this->cache_currencies[$currency_code]['unicode'])) { |
885
|
|
|
foreach ($this->cache_currencies[$currency_code]['unicode'] as $unicode) { |
886
|
|
|
$currency_sign .= mb_convert_encoding("&#{$unicode};", "UTF-8", 'HTML-ENTITIES'); |
887
|
|
|
} |
888
|
|
|
} |
889
|
|
|
} |
890
|
|
|
|
891
|
|
|
return ($currency_sign ? $currency_sign : $currency_code); |
892
|
|
|
} |
893
|
|
|
|
894
|
|
|
/** |
895
|
|
|
* Load into the cache this->cache_currencies, all currencies |
896
|
|
|
* |
897
|
|
|
* @param string $currency_code Get only currency. Get all if ''. |
898
|
|
|
* @return int Nb of loaded lines, 0 if already loaded, <0 if KO |
899
|
|
|
*/ |
900
|
|
|
public function loadCacheCurrencies($currency_code) |
901
|
|
|
{ |
902
|
|
|
global $db; |
903
|
|
|
|
904
|
|
|
if ($this->cache_currencies_all_loaded) |
905
|
|
|
return 0; // Cache already loaded for all |
906
|
|
|
if (!empty($currency_code) && isset($this->cache_currencies[$currency_code])) |
907
|
|
|
return 0; // Cache already loaded for the currency |
908
|
|
|
|
909
|
|
|
$sql = "SELECT code_iso, label, unicode"; |
910
|
|
|
$sql .= " FROM " . MAIN_DB_PREFIX . "c_currencies"; |
911
|
|
|
$sql .= " WHERE active = 1"; |
912
|
|
|
if (!empty($currency_code)) |
913
|
|
|
$sql .= " AND code_iso = '" . $db->escape($currency_code) . "'"; |
914
|
|
|
//$sql.= " ORDER BY code_iso ASC"; // Not required, a sort is done later |
915
|
|
|
|
916
|
|
|
dol_syslog(get_class($this) . '::loadCacheCurrencies', LOG_DEBUG); |
917
|
|
|
$resql = $db->query($sql); |
918
|
|
|
if ($resql) { |
919
|
|
|
$this->load("dict"); |
920
|
|
|
$label = array(); |
921
|
|
|
if (!empty($currency_code)) |
922
|
|
|
foreach ($this->cache_currencies as $key => $val) |
923
|
|
|
$label[$key] = $val['label']; // Label in already loaded cache |
924
|
|
|
|
925
|
|
|
$num = $db->num_rows($resql); |
926
|
|
|
$i = 0; |
927
|
|
|
while ($i < $num) { |
928
|
|
|
$obj = $db->fetch_object($resql); |
929
|
|
|
|
930
|
|
|
// Si traduction existe, on l'utilise, sinon on prend le libelle par defaut |
931
|
|
|
$this->cache_currencies[$obj->code_iso]['label'] = ($obj->code_iso && $this->trans("Currency" . $obj->code_iso) != "Currency" . $obj->code_iso ? $this->trans("Currency" . $obj->code_iso) : ($obj->label != '-' ? $obj->label : '')); |
932
|
|
|
$this->cache_currencies[$obj->code_iso]['unicode'] = (array)json_decode($obj->unicode, true); |
933
|
|
|
$label[$obj->code_iso] = $this->cache_currencies[$obj->code_iso]['label']; |
934
|
|
|
$i++; |
935
|
|
|
} |
936
|
|
|
if (empty($currency_code)) |
937
|
|
|
$this->cache_currencies_all_loaded = true; |
938
|
|
|
//print count($label).' '.count($this->cache_currencies); |
939
|
|
|
// Resort cache |
940
|
|
|
array_multisort($label, SORT_ASC, $this->cache_currencies); |
941
|
|
|
//var_dump($this->cache_currencies); $this->cache_currencies is now sorted onto label |
942
|
|
|
return $num; |
943
|
|
|
} else { |
944
|
|
|
dol_print_error($db); |
945
|
|
|
return -1; |
946
|
|
|
} |
947
|
|
|
} |
948
|
|
|
|
949
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
950
|
|
|
|
951
|
|
|
/** |
952
|
|
|
* Return an array with content of all loaded translation keys (found into this->tab_translate) so |
953
|
|
|
* we get a substitution array we can use for substitutions (for mail or ODT generation for example) |
954
|
|
|
* |
955
|
|
|
* @return array Array of translation keys lang_key => string_translation_loaded |
956
|
|
|
*/ |
957
|
|
|
function get_translations_for_substitutions() |
958
|
|
|
{ |
959
|
|
|
// phpcs:enable |
960
|
|
|
$substitutionarray = array(); |
961
|
|
|
|
962
|
|
|
foreach ($this->tab_translate as $code => $label) { |
963
|
|
|
$substitutionarray['lang_' . $code] = $label; |
964
|
|
|
$substitutionarray['__(' . $code . ')__'] = $label; |
965
|
|
|
} |
966
|
|
|
|
967
|
|
|
return $substitutionarray; |
968
|
|
|
} |
969
|
|
|
} |
970
|
|
|
|