36 public $charset_output =
'UTF-8';
38 public $tab_translate = array();
39 private $_tab_loaded = array();
41 public $cache_labels = array();
42 public $cache_currencies = array();
43 private $cache_currencies_all_loaded =
false;
46 public $errors = array();
57 if (!empty($conf->file->character_set_client)) {
58 $this->charset_output = $conf->file->character_set_client;
61 $this->dir = array($dir);
63 $this->dir = $conf->file->dol_document_root;
81 if (!empty($conf->global->MAIN_FORCELANGDIR)) {
84 foreach ($conf->file->dol_document_root as $dir) {
85 $newdir = $dir . $conf->global->MAIN_FORCELANGDIR;
86 if (!in_array($newdir, $this->dir)) {
87 $more[
'module_' . $i] = $newdir;
91 $this->dir = array_merge($more, $this->dir);
94 $this->origlang = $srclang;
96 if (empty($srclang) || $srclang ==
'auto') {
98 $langpref = empty($_SERVER[
'HTTP_ACCEPT_LANGUAGE']) ?
'' : $_SERVER[
'HTTP_ACCEPT_LANGUAGE'];
99 $langpref = preg_replace(
"/;([^,]*)/i",
"", $langpref);
100 $langpref = str_replace(
"-",
"_", $langpref);
101 $langlist = preg_split(
"/[;,]/", $langpref);
102 $codetouse = preg_replace(
'/[^_a-zA-Z]/',
'', $langlist[0]);
104 $codetouse = $srclang;
108 $langpart = explode(
"_", $codetouse);
110 if (!empty($langpart[1])) {
112 $longforshort = array(
'ar' =>
'ar_SA');
113 $longforshortexcep = array(
'ar_EG');
114 if (isset($longforshort[strtolower($langpart[0])]) && !in_array($codetouse, $longforshortexcep)) {
115 $srclang = $longforshort[strtolower($langpart[0])];
116 } elseif (!is_numeric($langpart[1])) {
117 $srclang = strtolower($langpart[0]) .
"_" . strtoupper($langpart[1]);
118 $longforlong = array(
'no_nb' =>
'nb_NO');
119 if (isset($longforlong[strtolower($srclang)])) {
120 $srclang = $longforlong[strtolower($srclang)];
123 $srclang = strtolower($langpart[0]) .
"_" . strtoupper($langpart[0]);
127 $longforshort = array(
128 'am' =>
'am_ET',
'ar' =>
'ar_SA',
'bn' =>
'bn_DB',
'el' =>
'el_GR',
'ca' =>
'ca_ES',
'cs' =>
'cs_CZ',
'en' =>
'en_US',
'fa' =>
'fa_IR',
129 'gl' =>
'gl_ES',
'he' =>
'he_IL',
'hi' =>
'hi_IN',
'ja' =>
'ja_JP',
130 'ka' =>
'ka_GE',
'km' =>
'km_KH',
'kn' =>
'kn_IN',
'ko' =>
'ko_KR',
'lo' =>
'lo_LA',
'nb' =>
'nb_NO',
'no' =>
'nb_NO',
'ne' =>
'ne_NP',
131 'sl' =>
'sl_SI',
'sq' =>
'sq_AL',
'sr' =>
'sr_RS',
'sv' =>
'sv_SE',
'uk' =>
'uk_UA',
'vi' =>
'vi_VN',
'zh' =>
'zh_CN'
133 if (isset($longforshort[strtolower($langpart[0])])) {
134 $srclang = $longforshort[strtolower($langpart[0])];
135 } elseif (!empty($langpart[0])) {
136 $srclang = strtolower($langpart[0]) .
"_" . strtoupper($langpart[0]);
142 $this->defaultlang = $srclang;
143 $this->shortlang = substr($srclang, 0, 2);
158 return $this->defaultlang;
160 return substr($this->defaultlang, 0, 2);
174 foreach ($domains as $domain) {
175 $result = $this->
load($domain);
178 } elseif ($result < 0) {
206 public function load($domain, $alt = 0, $stopafterdirection = 0, $forcelangdir =
'', $loadfromfileonly = 0, $forceloadifalreadynotfound = 0)
213 if (empty($domain)) {
217 if ($this->defaultlang ===
'none_NONE') {
223 if (empty($loadfromfileonly) && count($this->tab_translate) == 0) {
228 $newdomain = $domain;
233 if (preg_match(
'/^([^@]+)@([^@]+)$/i', $domain, $regs)) {
234 $newdomain = $regs[1];
235 $modulename = $regs[2];
240 !empty($this->_tab_loaded[$newdomain])
241 && ($this->_tab_loaded[$newdomain] != 2 || empty($forceloadifalreadynotfound))
248 $langofdir = (empty($forcelangdir) ? $this->defaultlang : $forcelangdir);
251 $langarray = explode(
'_', $langofdir);
252 if ($alt < 1 && isset($langarray[1]) && (strtolower($langarray[0]) == strtolower($langarray[1]) || in_array(strtolower($langofdir), array(
'el_gr')))) {
255 if ($alt < 2 && strtolower($langofdir) ==
'en_us') {
259 if (empty($langofdir)) {
260 dol_syslog(
"Error: " . get_class($this) .
"::load was called for domain=" . $domain .
" but language was not set yet with langs->setDefaultLang(). Nothing will be loaded.", LOG_WARNING);
264 foreach ($this->dir as $searchdir) {
266 $file_lang = $searchdir . ($modulename ?
'/' . $modulename :
'') .
"/langs/" . $langofdir .
"/" . $newdomain .
".lang";
269 $filelangexists = is_file($file_lang_osencoded);
274 if ($filelangexists) {
281 if (
isModEnabled(
'memcached') && !empty($conf->global->MEMCACHED_SERVER)) {
282 $usecachekey = $newdomain .
'_' . $langofdir .
'_' . md5($file_lang);
283 } elseif (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) {
285 $usecachekey = $newdomain;
292 require_once DOL_DOCUMENT_ROOT .
'/core/lib/memory.lib.php';
294 if (is_array($tmparray) && count($tmparray)) {
295 $this->tab_translate += $tmparray;
306 if ($fp = @fopen($file_lang,
"rt")) {
308 $tabtranslatedomain = array();
316 while ($line = fscanf($fp,
"%[^= ]%*[ =]%[^\n\r]")) {
317 if (isset($line[1])) {
318 list($key, $value) = $line;
321 if (empty($this->tab_translate[$key])) {
322 if ($key ==
'DIRECTION') {
323 if ($alt < 2 || empty($this->tab_translate[$key])) {
324 $this->tab_translate[$key] = $value;
325 if ($stopafterdirection) {
327 } elseif ($usecachekey) {
328 $tabtranslatedomain[$key] = $value;
331 } elseif ($key[0] ==
'#') {
335 $this->tab_translate[$key] = str_replace(array(
'\\n',
'\\\\s'), array(
"\n",
'\s'), $value);
337 $tabtranslatedomain[$key] = $value;
348 if ($usecachekey && count($tabtranslatedomain)) {
349 $ressetcache =
dol_setcache($usecachekey, $tabtranslatedomain);
350 if ($ressetcache < 0) {
351 $error =
'Failed to set cache for usecachekey=' . $usecachekey .
' result=' . $ressetcache;
356 if (empty($conf->global->MAIN_FORCELANGDIR)) {
368 $langofdir = strtolower($langarray[0]) .
'_' . strtoupper($langarray[0]);
369 if ($langofdir ==
'el_EL') {
370 $langofdir =
'el_GR';
372 if ($langofdir ==
'ar_AR') {
373 $langofdir =
'ar_SA';
375 $this->
load($domain, $alt + 1, $stopafterdirection, $langofdir);
382 $langofdir =
'en_US';
383 $this->
load($domain, $alt + 1, $stopafterdirection, $langofdir);
389 $this->_tab_loaded[$newdomain] = 1;
392 if (empty($this->_tab_loaded[$newdomain])) {
393 $this->_tab_loaded[$newdomain] = 2;
399 if (empty($loadfromfileonly)) {
400 $overwritekey =
'MAIN_OVERWRITE_TRANS_' . $this->defaultlang;
401 if (!empty($conf->global->$overwritekey)) {
403 $tmparray = explode(
',', $conf->global->$overwritekey);
404 foreach ($tmparray as $tmp) {
405 $tmparray2 = explode(
':', $tmp);
406 if (!empty($tmparray2[1])) {
407 $this->tab_translate[$tmparray2[0]] = $tmparray2[1];
415 !empty($this->tab_translate[
"SeparatorDecimal"]) && !empty($this->tab_translate[
"SeparatorThousand"])
416 && $this->tab_translate[
"SeparatorDecimal"] == $this->tab_translate[
"SeparatorThousand"]
418 $this->tab_translate[
"SeparatorThousand"] =
'';
440 $domain =
'database';
449 $newdomain = $domain;
452 if (!empty($this->_tab_loaded[$newdomain])) {
457 $this->_tab_loaded[$newdomain] = 1;
460 $langofdir = $this->defaultlang;
462 if (empty($langofdir)) {
463 dol_syslog(
"Error: " . get_class($this) .
"::loadFromDatabase was called but language was not set yet with langs->setDefaultLang(). Nothing will be loaded.", LOG_WARNING);
473 if (
isModEnabled(
'memcached') && !empty($conf->global->MEMCACHED_SERVER)) {
474 $usecachekey = $newdomain .
'_' . $langofdir;
475 } elseif (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) {
477 $usecachekey = $newdomain;
484 require_once DOL_DOCUMENT_ROOT .
'/core/lib/memory.lib.php';
486 if (is_array($tmparray) && count($tmparray)) {
487 $this->tab_translate += $tmparray;
495 if (!$found && !empty($conf->global->MAIN_ENABLE_OVERWRITE_TRANSLATION)) {
497 $sql =
"SELECT transkey, transvalue FROM " . $db->prefix() .
"overwrite_trans where lang='" . $db->escape($this->defaultlang) .
"' OR lang IS NULL";
498 $sql .=
" AND entity IN (0, " .
getEntity(
'overwrite_trans') .
")";
499 $sql .= $db->order(
"lang",
"DESC");
500 $resql = $db->query(
$sql);
503 $num = $db->num_rows($resql);
506 $tabtranslatedomain = array();
511 $obj = $db->fetch_object($resql);
513 $key = $obj->transkey;
514 $value = $obj->transvalue;
517 if (empty($this->tab_translate[$key])) {
519 $this->tab_translate[$key] = str_replace(array(
'\\n',
'\\\\s'), array(
"\n",
'\s'), $value);
522 $tabtranslatedomain[$key] = $value;
533 if ($usecachekey && count($tabtranslatedomain)) {
534 $ressetcache =
dol_setcache($usecachekey, $tabtranslatedomain);
535 if ($ressetcache < 0) {
536 $error =
'Failed to set cache for usecachekey=' . $usecachekey .
' result=' . $ressetcache;
547 $this->_tab_loaded[$newdomain] = 1;
550 if (empty($this->_tab_loaded[$newdomain])) {
551 $this->_tab_loaded[$newdomain] = 2;
565 return $this->_tab_loaded[$domain];
583 if (!is_string($key)) {
585 return 'ErrorBadValueForParamNotAString';
590 if (preg_match(
'/^Civility([0-9A-Z]+)$/i', $key, $reg)) {
591 $newstr = $this->
getLabelFromKey($db, $reg[1],
'c_civility',
'code',
'label');
592 } elseif (preg_match(
'/^Currency([A-Z][A-Z][A-Z])$/i', $key, $reg)) {
593 $newstr = $this->
getLabelFromKey($db, $reg[1],
'c_currencies',
'code_iso',
'label');
594 } elseif (preg_match(
'/^SendingMethod([0-9A-Z]+)$/i', $key, $reg)) {
595 $newstr = $this->
getLabelFromKey($db, $reg[1],
'c_shipment_mode',
'code',
'libelle');
596 } elseif (preg_match(
'/^PaymentType(?:Short)?([0-9A-Z]+)$/i', $key, $reg)) {
597 $newstr = $this->
getLabelFromKey($db, $reg[1],
'c_paiement',
'code',
'libelle',
'', 1);
598 } elseif (preg_match(
'/^OppStatus([0-9A-Z]+)$/i', $key, $reg)) {
599 $newstr = $this->
getLabelFromKey($db, $reg[1],
'c_lead_status',
'code',
'label');
600 } elseif (preg_match(
'/^OrderSource([0-9A-Z]+)$/i', $key, $reg)) {
628 public function trans($key, $param1 =
'', $param2 =
'', $param3 =
'', $param4 =
'', $maxsize = 0)
632 if (!empty($this->tab_translate[$key])) {
633 $str = $this->tab_translate[$key];
636 $replacekey =
'MAIN_REPLACE_TRANS_' . $this->defaultlang;
637 if (!empty($conf->global->$replacekey)) {
638 $tmparray = explode(
';', $conf->global->$replacekey);
639 foreach ($tmparray as $tmp) {
640 $tmparray2 = explode(
':', $tmp);
641 $str = preg_replace(
'/' . preg_quote($tmparray2[0]) .
'/', $tmparray2[1], $str);
648 array(
'"',
'<b>',
'</b>',
'<u>',
'</u>',
'<i',
'</i>',
'<center>',
'</center>',
'<strong>',
'</strong>',
'<a ',
'</a>',
'<br>',
'<span',
'</span>',
'< ',
'>'),
649 array(
'__quot__',
'__tagb__',
'__tagbend__',
'__tagu__',
'__taguend__',
'__tagi__',
'__tagiend__',
'__tagcenter__',
'__tagcenterend__',
'__tagb__',
'__tagbend__',
'__taga__',
'__tagaend__',
'__tagbr__',
'__tagspan__',
'__tagspanend__',
'__ltspace__',
'__gt__'),
653 if (strpos($key,
'Format') !== 0) {
655 $str = sprintf($str, $param1, $param2, $param3, $param4);
662 $str = htmlentities($str, ENT_COMPAT, $this->charset_output);
666 array(
'__quot__',
'__tagb__',
'__tagbend__',
'__tagu__',
'__taguend__',
'__tagi__',
'__tagiend__',
'__tagcenter__',
'__tagcenterend__',
'__taga__',
'__tagaend__',
'__tagbr__',
'__tagspan__',
'__tagspanend__',
'__ltspace__',
'__gt__'),
667 array(
'"',
'<b>',
'</b>',
'<u>',
'</u>',
'<i',
'</i>',
'<center>',
'</center>',
'<a ',
'</a>',
'<br>',
'<span',
'</span>',
'< ',
'>'),
697 public function transnoentities($key, $param1 =
'', $param2 =
'', $param3 =
'', $param4 =
'', $param5 =
'')
722 if (!empty($this->tab_translate[$key])) {
723 $str = $this->tab_translate[$key];
726 $replacekey =
'MAIN_REPLACE_TRANS_' . $this->defaultlang;
727 if (!empty($conf->global->$replacekey)) {
728 $tmparray = explode(
';', $conf->global->$replacekey);
729 foreach ($tmparray as $tmp) {
730 $tmparray2 = explode(
':', $tmp);
731 $str = preg_replace(
'/' . preg_quote($tmparray2[0]) .
'/', $tmparray2[1], $str);
735 if (!preg_match(
'/^Format/', $key)) {
737 $str = sprintf($str, $param1, $param2, $param3, $param4, $param5);
760 if (!empty($this->tab_translate[
"$str$countrycode"])) {
761 return $this->
trans(
"$str$countrycode");
763 return $this->
trans($str);
778 if (!empty($this->tab_translate[
"$str$countrycode"])) {
795 if ($pagecodefrom ==
'ISO-8859-1' && $this->charset_output ==
'UTF-8') {
796 $str = utf8_encode($str);
798 if ($pagecodefrom ==
'UTF-8' && $this->charset_output ==
'ISO-8859-1') {
799 $str = utf8_decode(str_replace(
'€', chr(128), $str));
821 $this->
load(
"languages");
824 $handle = opendir($langdir .
"/langs");
825 $langs_available = array();
826 while ($dir = trim(readdir($handle))) {
828 if (preg_match(
'/^([a-z]+)_([A-Z]+)/i', $dir, $regs)) {
831 $arrayofspecialmainlanguages = array(
860 if (strtolower($regs[1]) != strtolower($regs[2]) && !in_array($dir, $arrayofspecialmainlanguages)) {
865 if (!empty($conf->global->MAIN_LANGUAGES_ALLOWED) && !in_array($dir, explode(
',', $conf->global->MAIN_LANGUAGES_ALLOWED))) {
870 $langs_available[$dir] = $dir;
873 if ($usecode == 1 || !empty($conf->global->MAIN_SHOW_LANGUAGE_CODE)) {
874 $langs_available[$dir] = $dir .
': ' .
dol_trunc($this->
trans(
'Language_' . $dir), $maxlength);
876 $langs_available[$dir] = $this->
trans(
'Language_' . $dir);
879 $langs_available[$dir] = str_replace(
' (United States)',
'', $langs_available[$dir]);
883 return $langs_available;
899 foreach ($this->dir as $searchdir) {
900 if (is_readable(
dol_osencode($searchdir .
"/langs/" . $this->defaultlang .
"/" . $filename))) {
906 if ($this->defaultlang !=
"en_US") {
907 $filenamealt = $searchdir .
"/langs/en_US/" . $filename;
935 $newnumber = $number;
937 $dirsubstitutions = array_merge(array(), $conf->modules_parts[
'substitutions']);
938 foreach ($dirsubstitutions as $reldir) {
943 if (!is_dir($newdir)) {
947 $fonc =
'numberwords';
948 if (
file_exists($newdir .
'/functions_' . $fonc .
'.lib.php')) {
949 include_once $newdir .
'/functions_' . $fonc .
'.lib.php';
950 if (function_exists(
'numberwords_getLabelFromNumber')) {
951 $newnumber = numberwords_getLabelFromNumber($this, $number, $isamount);
976 public function getLabelFromKey($db, $key, $tablename, $fieldkey, $fieldlabel, $keyforselect =
'', $filteronentity = 0)
983 if (preg_match(
'/[^0-9A-Z_]/i', $tablename) || preg_match(
'/[^0-9A-Z_]/i', $fieldkey) || preg_match(
'/[^0-9A-Z_]/i', $fieldlabel)) {
984 $this->error =
'Bad value for parameter tablename, fieldkey or fieldlabel';
992 if ($tmp != $key && $tmp !=
'ErrorBadValueForParamNotAString') {
997 if (isset($this->cache_labels[$tablename][$key])) {
998 return $this->cache_labels[$tablename][$key];
1002 $sql =
"SELECT " . $fieldlabel .
" as label";
1003 $sql .=
" FROM " . $db->prefix() . $tablename;
1004 $sql .=
" WHERE " . $fieldkey .
" = '" . $db->escape($keyforselect ? $keyforselect : $key) .
"'";
1005 if ($filteronentity) {
1008 dol_syslog(get_class($this) .
'::getLabelFromKey', LOG_DEBUG);
1009 $resql = $db->query(
$sql);
1011 $obj = $db->fetch_object($resql);
1013 $this->cache_labels[$tablename][$key] = $obj->label;
1015 $this->cache_labels[$tablename][$key] = $key;
1019 return $this->cache_labels[$tablename][$key];
1021 $this->error = $db->lasterror();
1040 if (in_array($currency_code, array(
'USD'))) {
1041 return $symbol . $amount;
1043 return $amount . $symbol;
1057 $currency_sign =
'';
1059 if (function_exists(
"mb_convert_encoding")) {
1062 if (isset($this->cache_currencies[$currency_code]) && !empty($this->cache_currencies[$currency_code][
'unicode']) && is_array($this->cache_currencies[$currency_code][
'unicode'])) {
1063 foreach ($this->cache_currencies[$currency_code][
'unicode'] as $unicode) {
1064 $currency_sign .= mb_convert_encoding(
"&#" . $unicode .
";",
"UTF-8",
'HTML-ENTITIES');
1069 return ($currency_sign ? $currency_sign : $currency_code);
1082 if ($this->cache_currencies_all_loaded) {
1085 if (!empty($currency_code) && isset($this->cache_currencies[$currency_code])) {
1089 $sql =
"SELECT code_iso, label, unicode";
1090 $sql .=
" FROM " . $db->prefix() .
"c_currencies";
1091 $sql .=
" WHERE active = 1";
1092 if (!empty($currency_code)) {
1093 $sql .=
" AND code_iso = '" . $db->escape($currency_code) .
"'";
1097 dol_syslog(get_class($this) .
'::loadCacheCurrencies', LOG_DEBUG);
1098 $resql = $db->query(
$sql);
1100 $this->
load(
"dict");
1102 if (!empty($currency_code)) {
1103 foreach ($this->cache_currencies as $key => $val) {
1104 $label[$key] = $val[
'label'];
1108 $num = $db->num_rows($resql);
1111 $obj = $db->fetch_object($resql);
1114 $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 :
''));
1115 $this->cache_currencies[$obj->code_iso][
'unicode'] = (array) json_decode((empty($obj->unicode) ?
'' : $obj->unicode),
true);
1116 $label[$obj->code_iso] = $this->cache_currencies[$obj->code_iso][
'label'];
1120 if (empty($currency_code)) {
1121 $this->cache_currencies_all_loaded =
true;
1126 array_multisort($label, SORT_ASC, $this->cache_currencies);
1145 $substitutionarray = array();
1147 foreach ($this->tab_translate as $code => $label) {
1148 $substitutionarray[
'lang_' . $code] = $label;
1149 $substitutionarray[
'__(' . $code .
')__'] = $label;
1152 return $substitutionarray;