51 public $charset_output =
'UTF-8';
57 public $tab_translate = array();
62 private $_tab_loaded = array();
67 public $cache_labels = array();
72 public $cache_currencies = array();
78 private $cache_currencies_all_loaded =
false;
94 public $errors = array();
105 if (!empty($conf->file->character_set_client)) {
106 $this->charset_output = $conf->file->character_set_client;
109 $this->dir = array($dir);
111 $this->dir = $conf->file->dol_document_root;
132 foreach ($conf->file->dol_document_root as $dir) {
134 if (!in_array($newdir, $this->dir)) {
135 $more[
'module_' . $i] = $newdir;
139 $this->dir = array_merge($more, $this->dir);
142 $this->origlang = $srclang;
144 if (empty($srclang) || $srclang ==
'auto') {
146 $langpref = empty($_SERVER[
'HTTP_ACCEPT_LANGUAGE']) ?
'' : $_SERVER[
'HTTP_ACCEPT_LANGUAGE'];
147 $langpref = preg_replace(
"/;([^,]*)/i",
"", $langpref);
148 $langpref = str_replace(
"-",
"_", $langpref);
149 $langlist = preg_split(
"/[;,]/", $langpref);
150 $codetouse = preg_replace(
'/[^_a-zA-Z]/',
'', $langlist[0]);
152 $codetouse = $srclang;
156 $langpart = explode(
"_", $codetouse);
158 if (!empty($langpart[1])) {
160 $longforshort = array(
'ar' =>
'ar_SA');
161 $longforshortexcep = array(
'ar_EG');
162 if (isset($longforshort[strtolower($langpart[0])]) && !in_array($codetouse, $longforshortexcep)) {
163 $srclang = $longforshort[strtolower($langpart[0])];
164 } elseif (!is_numeric($langpart[1])) {
165 $srclang = strtolower($langpart[0]) .
"_" . strtoupper($langpart[1]);
166 $longforlong = array(
'no_nb' =>
'nb_NO');
167 if (isset($longforlong[strtolower($srclang)])) {
168 $srclang = $longforlong[strtolower($srclang)];
171 $srclang = strtolower($langpart[0]) .
"_" . strtoupper($langpart[0]);
175 $longforshort = array(
176 'am' =>
'am_ET',
'ar' =>
'ar_SA',
'bn' =>
'bn_DB',
'el' =>
'el_GR',
'ca' =>
'ca_ES',
'cs' =>
'cs_CZ',
'en' =>
'en_US',
'fa' =>
'fa_IR',
177 'gl' =>
'gl_ES',
'he' =>
'he_IL',
'hi' =>
'hi_IN',
'ja' =>
'ja_JP',
178 'ka' =>
'ka_GE',
'km' =>
'km_KH',
'kn' =>
'kn_IN',
'ko' =>
'ko_KR',
'lo' =>
'lo_LA',
'nb' =>
'nb_NO',
'no' =>
'nb_NO',
'ne' =>
'ne_NP',
179 'sl' =>
'sl_SI',
'sq' =>
'sq_AL',
'sr' =>
'sr_RS',
'sv' =>
'sv_SE',
'uk' =>
'uk_UA',
'vi' =>
'vi_VN',
'zh' =>
'zh_CN'
181 if (isset($longforshort[strtolower($langpart[0])])) {
182 $srclang = $longforshort[strtolower($langpart[0])];
183 } elseif (!empty($langpart[0])) {
184 $srclang = strtolower($langpart[0]) .
"_" . strtoupper($langpart[0]);
190 $this->defaultlang = $srclang;
191 $this->shortlang = substr($srclang, 0, 2);
206 return $this->defaultlang;
208 return substr($this->defaultlang, 0, 2);
222 foreach ($domains as $domain) {
223 $result = $this->
load($domain);
226 } elseif ($result < 0) {
256 public function load($domain, $alt = 0, $stopafterdirection = 0, $forcelangdir =
'', $loadfromfileonly = 0, $forceloadifalreadynotfound = 0, &$tabtranslatedomain = [], $langkey =
'')
263 if (empty($domain)) {
264 dol_print_error(
null, get_class($this) .
"::Load ErrorWrongParameters");
267 if ($this->defaultlang ===
'none_NONE') {
273 if (empty($loadfromfileonly) && count($this->tab_translate) == 0) {
278 $newdomain = $domain;
283 if (preg_match(
'/^([^@]+)@([^@]+)$/i', $domain, $regs)) {
284 $newdomain = (string) $regs[1];
285 $modulename = (string) $regs[2];
290 !empty($this->_tab_loaded[$newdomain])
291 && ($this->_tab_loaded[$newdomain] != 2 || empty($forceloadifalreadynotfound))
298 $langofdir = (empty($forcelangdir) ? $this->defaultlang : $forcelangdir);
299 $langkey = (empty($langkey) ? $langofdir : $langkey);
302 $langarray = explode(
'_', $langofdir);
303 if ($alt < 1 && isset($langarray[1]) && (strtolower($langarray[0]) == strtolower($langarray[1]) || in_array(strtolower($langofdir), array(
'el_gr')))) {
306 if ($alt < 2 && strtolower($langofdir) ==
'en_us') {
310 if (empty($langofdir)) {
311 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);
316 foreach ($this->dir as $searchdir) {
318 $file_lang = $searchdir . ($modulename ?
'/' . $modulename :
'') .
"/langs/" . $langofdir .
"/" . $newdomain .
".lang";
322 $filelangexists = @is_file($file_lang_osencoded);
327 if ($filelangexists) {
335 $usecachekey = $newdomain .
'_' . $langkey .
'_' . md5($file_lang);
338 $usecachekey = $newdomain;
343 require_once DOL_DOCUMENT_ROOT .
'/core/lib/memory.lib.php';
345 if (is_array($tmparray) && count($tmparray)) {
346 $this->tab_translate += $tmparray;
355 if ($fp = @fopen($file_lang,
"rt")) {
363 while ($line = fscanf($fp,
"%[^= ]%*[ =]%[^\n\r]")) {
364 if (isset($line[1])) {
365 list($key, $value) = $line;
368 if (empty($this->tab_translate[$key])) {
369 if ($key ==
'DIRECTION') {
370 if ($alt < 2 || empty($this->tab_translate[$key])) {
371 $this->tab_translate[$key] = $value;
372 if ($stopafterdirection) {
374 } elseif ($usecachekey) {
375 $tabtranslatedomain[$key] = $value;
378 } elseif ($key[0] ==
'#') {
382 $this->tab_translate[$key] = str_replace(array(
'\\n',
'\\\\s'), array(
"\n",
'\s'), $value);
384 $tabtranslatedomain[$key] = $value;
405 $langofdir = strtolower($langarray[0]) .
'_' . strtoupper($langarray[0]);
406 if ($langofdir ==
'el_EL') {
407 $langofdir =
'el_GR';
409 if ($langofdir ==
'ar_AR') {
410 $langofdir =
'ar_SA';
412 $this->
load($domain, $alt + 1, $stopafterdirection, $langofdir, 0, 0, $tabtranslatedomain, $langkey);
419 $langofdir =
'en_US';
420 $this->
load($domain, $alt + 1, $stopafterdirection, $langofdir, 0, 0, $tabtranslatedomain, $langkey);
426 $this->_tab_loaded[$newdomain] = 1;
429 if (empty($this->_tab_loaded[$newdomain])) {
430 $this->_tab_loaded[$newdomain] = 2;
436 if (empty($loadfromfileonly)) {
437 $overwritekey =
'MAIN_OVERWRITE_TRANS_' . $this->defaultlang;
441 foreach ($tmparray as $tmp) {
442 $tmparray2 = explode(
':', $tmp);
443 if (!empty($tmparray2[1])) {
444 $this->tab_translate[$tmparray2[0]] = $tmparray2[1];
451 if ($usecachekey && count($tabtranslatedomain)) {
452 $ressetcache =
dol_setcache($usecachekey, $tabtranslatedomain);
453 if ($ressetcache < 0) {
454 $error =
'Failed to set cache for usecachekey=' . $usecachekey .
' result=' . $ressetcache;
461 !empty($this->tab_translate[
"SeparatorDecimal"]) && !empty($this->tab_translate[
"SeparatorThousand"])
462 && $this->tab_translate[
"SeparatorDecimal"] == $this->tab_translate[
"SeparatorThousand"]
464 $this->tab_translate[
"SeparatorThousand"] =
'';
486 $domain =
'database';
495 $newdomain = $domain;
498 if (!empty($this->_tab_loaded[$newdomain])) {
503 $this->_tab_loaded[$newdomain] = 2;
506 $langofdir = $this->defaultlang;
508 if (empty($langofdir)) {
509 dol_syslog(
"Error: " . get_class($this) .
"::loadFromDatabase was called but language was not set yet with langs->setDefaultLang(). Nothing will be loaded.", LOG_WARNING);
520 $usecachekey = $newdomain .
'_' . $langofdir;
523 $usecachekey = $newdomain;
530 require_once DOL_DOCUMENT_ROOT .
'/core/lib/memory.lib.php';
532 if (is_array($tmparray) && count($tmparray)) {
533 $this->tab_translate += $tmparray;
543 $sql =
"SELECT transkey, transvalue FROM ".$db->prefix().
"overwrite_trans where (lang='".$db->escape($this->defaultlang).
"' OR lang IS NULL)";
544 $sql .=
" AND entity IN (0, ".getEntity(
'overwrite_trans').
")";
545 $sql .= $db->order(
"lang",
"DESC");
547 $resql = $db->query($sql);
550 $num = $db->num_rows($resql);
552 $tabtranslatedomain = array();
556 $obj = $db->fetch_object($resql);
558 $key = $obj->transkey;
559 $value = $obj->transvalue;
562 if (empty($this->tab_translate[$key])) {
564 $this->tab_translate[$key] = str_replace(array(
'\\n',
'\\\\s'), array(
"\n",
'\s'), $value);
567 $tabtranslatedomain[$key] = $value;
578 if ($usecachekey && count($tabtranslatedomain)) {
579 $ressetcache =
dol_setcache($usecachekey, $tabtranslatedomain);
580 if ($ressetcache < 0) {
581 $error =
'Failed to set cache for usecachekey=' . $usecachekey .
' result=' . $ressetcache;
592 $this->_tab_loaded[$newdomain] = 1;
606 return $this->_tab_loaded[$domain];
624 if (!is_string($key)) {
626 return 'ErrorBadValueForParamNotAString';
631 if (preg_match(
'/^Civility([0-9A-Z]+)$/i', $key, $reg)) {
632 $newstr = $this->
getLabelFromKey($db, $reg[1],
'c_civility',
'code',
'label');
633 } elseif (preg_match(
'/^Currency([A-Z][A-Z][A-Z])$/i', $key, $reg)) {
634 $newstr = $this->
getLabelFromKey($db, $reg[1],
'c_currencies',
'code_iso',
'label');
635 } elseif (preg_match(
'/^SendingMethod([0-9A-Z]+)$/i', $key, $reg)) {
636 $newstr = $this->
getLabelFromKey($db, $reg[1],
'c_shipment_mode',
'code',
'libelle');
637 } elseif (preg_match(
'/^PaymentType(?:Short)?([0-9A-Z]+)$/i', $key, $reg)) {
638 $newstr = $this->
getLabelFromKey($db, $reg[1],
'c_paiement',
'code',
'libelle',
'', 1);
639 } elseif (preg_match(
'/^OppStatus([0-9A-Z]+)$/i', $key, $reg)) {
640 $newstr = $this->
getLabelFromKey($db, $reg[1],
'c_lead_status',
'code',
'label');
641 } elseif (preg_match(
'/^OrderSource([0-9A-Z]+)$/i', $key, $reg)) {
669 public function trans($key, $param1 =
'', $param2 =
'', $param3 =
'', $param4 =
'', $maxsize = 0)
671 if (!empty($this->tab_translate[$key])) {
672 $str = $this->tab_translate[$key];
675 $replacekey =
'MAIN_REPLACE_TRANS_' . $this->defaultlang;
678 foreach ($tmparray as $tmp) {
679 $tmparray2 = explode(
':', $tmp);
680 $str = preg_replace(
'/' . preg_quote($tmparray2[0]) .
'/', $tmparray2[1], $str);
687 array(
'"',
'<b>',
'</b>',
'<u>',
'</u>',
'<i',
'</i>',
'<center>',
'</center>',
'<strong>',
'</strong>',
'<a ',
'</a>',
'<br>',
'<span',
'</span>',
'< ',
'>'),
688 array(
'__quot__',
'__tagb__',
'__tagbend__',
'__tagu__',
'__taguend__',
'__tagi__',
'__tagiend__',
'__tagcenter__',
'__tagcenterend__',
'__tagb__',
'__tagbend__',
'__taga__',
'__tagaend__',
'__tagbr__',
'__tagspan__',
'__tagspanend__',
'__ltspace__',
'__gt__'),
692 if (strpos($key,
'Format') !== 0) {
695 $str = sprintf($str, $param1, $param2, $param3, $param4);
702 $str = htmlentities($str, ENT_COMPAT, $this->charset_output);
706 array(
'__quot__',
'__tagb__',
'__tagbend__',
'__tagu__',
'__taguend__',
'__tagi__',
'__tagiend__',
'__tagcenter__',
'__tagcenterend__',
'__taga__',
'__tagaend__',
'__tagbr__',
'__tagspan__',
'__tagspanend__',
'__ltspace__',
'__gt__'),
707 array(
'"',
'<b>',
'</b>',
'<u>',
'</u>',
'<i',
'</i>',
'<center>',
'</center>',
'<a ',
'</a>',
'<br>',
'<span',
'</span>',
'< ',
'>'),
713 $str = str_replace(array(
'%27',
'''),
'', $str);
740 public function transnoentities($key, $param1 =
'', $param2 =
'', $param3 =
'', $param4 =
'', $param5 =
'')
765 if (!empty($this->tab_translate[$key])) {
766 $str = $this->tab_translate[$key];
769 $replacekey =
'MAIN_REPLACE_TRANS_' . $this->defaultlang;
772 foreach ($tmparray as $tmp) {
773 $tmparray2 = explode(
':', $tmp);
774 $str = preg_replace(
'/' . preg_quote($tmparray2[0]) .
'/', $tmparray2[1], $str);
778 if (!preg_match(
'/^Format/', $key)) {
781 $str = sprintf($str, $param1, $param2, $param3, $param4, $param5);
786 $str = str_replace(array(
'%27',
'''),
'', $str);
805 $strLocaleKey = $str.$countrycode;
806 if (!empty($this->tab_translate[$strLocaleKey])) {
807 return $this->
trans($strLocaleKey);
809 return $this->
trans($str);
824 $strLocaleKey = $str.$countrycode;
825 if (!empty($this->tab_translate[$strLocaleKey])) {
843 if (empty($pagecodeto)) {
844 $pagecodeto = $this->charset_output;
847 if ($pagecodefrom ==
'ISO-8859-1' && $pagecodeto ==
'UTF-8') {
848 $str = mb_convert_encoding($str,
'UTF-8',
'ISO-8859-1');
850 if ($pagecodefrom ==
'UTF-8' && $pagecodeto ==
'ISO-8859-1') {
851 $str = mb_convert_encoding(str_replace(
'€', chr(128), $str),
'ISO-8859-1');
871 $this->
load(
"languages");
874 $handle = opendir($langdir .
"/langs");
875 $langs_available = array();
876 while ($dir = trim(readdir($handle))) {
878 if (preg_match(
'/^([a-z]+)_([A-Z]+)/i', $dir, $regs)) {
881 $arrayofspecialmainlanguages = array(
910 if (strtolower($regs[1]) != strtolower($regs[2]) && !in_array($dir, $arrayofspecialmainlanguages)) {
920 $langs_available[$dir] = $dir;
924 $langs_available[$dir] = $dir .
': ' .
dol_trunc($this->
trans(
'Language_' . $dir), $maxlength);
926 $langs_available[$dir] = $this->
trans(
'Language_' . $dir);
929 $langs_available[$dir] = str_replace(
' (United States)',
'', $langs_available[$dir]);
933 return $langs_available;
949 foreach ($this->dir as $searchdir) {
950 if (is_readable(
dol_osencode($searchdir .
"/langs/" . $this->defaultlang .
"/" . $filename))) {
957 if ($this->defaultlang !=
"en_US") {
958 $filenamealt = $searchdir .
"/langs/en_US/" . $filename;
961 if ($filenamealt !==
null && is_readable(
dol_osencode($filenamealt))) {
986 $newnumber = $number;
988 $dirsubstitutions = array_merge(array(), $conf->modules_parts[
'substitutions']);
989 foreach ($dirsubstitutions as $reldir) {
994 if (!is_dir($newdir)) {
998 $fonc =
'numberwords';
999 if (
file_exists($newdir .
'/functions_' . $fonc .
'.lib.php')) {
1000 include_once $newdir .
'/functions_' . $fonc .
'.lib.php';
1001 if (function_exists(
'numberwords_getLabelFromNumber')) {
1002 $newnumber = numberwords_getLabelFromNumber($this, $number, $isamount);
1027 public function getLabelFromKey($db, $key, $tablename, $fieldkey, $fieldlabel, $keyforselect =
'', $filteronentity = 0)
1034 if (preg_match(
'/[^0-9A-Z_]/i', $tablename) || preg_match(
'/[^0-9A-Z_]/i', $fieldkey) || preg_match(
'/[^0-9A-Z_]/i', $fieldlabel)) {
1035 $this->error =
'Bad value for parameter tablename, fieldkey or fieldlabel';
1043 if ($tmp != $key && $tmp !=
'ErrorBadValueForParamNotAString') {
1048 if (isset($this->cache_labels[$tablename][$key])) {
1049 return $this->cache_labels[$tablename][$key];
1053 $sql =
"SELECT " . $fieldlabel .
" as label";
1054 $sql .=
" FROM " . $db->prefix() . $tablename;
1055 $sql .=
" WHERE " . $fieldkey .
" = '" . $db->escape($keyforselect ? $keyforselect : $key) .
"'";
1056 if ($filteronentity) {
1057 $sql .=
" AND entity IN (" .
getEntity($tablename) .
')';
1059 dol_syslog(get_class($this) .
'::getLabelFromKey', LOG_DEBUG);
1060 $resql = $db->query($sql);
1062 $obj = $db->fetch_object($resql);
1064 $this->cache_labels[$tablename][$key] = (string) $obj->label;
1066 $this->cache_labels[$tablename][$key] = $key;
1070 return $this->cache_labels[$tablename][$key];
1072 $this->error = $db->lasterror();
1091 if (in_array($currency_code, array(
'USD'))) {
1092 return $symbol . $amount;
1094 return $amount . $symbol;
1108 $currency_sign =
'';
1110 if (function_exists(
"mb_convert_encoding")) {
1113 if (isset($this->cache_currencies[$currency_code]) && !empty($this->cache_currencies[$currency_code][
'unicode']) && is_array($this->cache_currencies[$currency_code][
'unicode'])) {
1114 foreach ($this->cache_currencies[$currency_code][
'unicode'] as $unicode) {
1115 $currency_sign .= mb_convert_encoding(
"&#" . $unicode .
";",
"UTF-8",
'HTML-ENTITIES');
1120 return ($currency_sign ? $currency_sign : $currency_code);
1133 if ($this->cache_currencies_all_loaded) {
1136 if (!empty($currency_code) && isset($this->cache_currencies[$currency_code])) {
1140 $sql =
"SELECT code_iso, label, unicode";
1141 $sql .=
" FROM " . $db->prefix() .
"c_currencies";
1142 $sql .=
" WHERE active = 1";
1143 if (!empty($currency_code)) {
1144 $sql .=
" AND code_iso = '" . $db->escape($currency_code) .
"'";
1148 dol_syslog(get_class($this) .
'::loadCacheCurrencies', LOG_DEBUG);
1149 $resql = $db->query($sql);
1151 $this->
load(
"dict");
1153 if (!empty($currency_code)) {
1154 foreach ($this->cache_currencies as $key => $val) {
1155 $label[$key] = $val[
'label'];
1159 $num = $db->num_rows($resql);
1162 $obj = $db->fetch_object($resql);
1165 $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 :
''));
1166 $this->cache_currencies[$obj->code_iso][
'unicode'] = (array) json_decode((empty($obj->unicode) ?
'' : $obj->unicode),
true);
1167 $label[$obj->code_iso] = $this->cache_currencies[$obj->code_iso][
'label'];
1171 if (empty($currency_code)) {
1172 $this->cache_currencies_all_loaded =
true;
1177 array_multisort($label, SORT_ASC, $this->cache_currencies);
1196 $substitutionarray = array();
1198 foreach ($this->tab_translate as $code => $label) {
1199 $substitutionarray[
'lang_' . $code] = $label;
1200 $substitutionarray[
'__(' . $code .
')__'] = $label;
1203 return $substitutionarray;
Class to manage translations.
transnoentities($key, $param1='', $param2='', $param3='', $param4='', $param5='')
Return translated value of a text string If there is no match for this text, we look in alternative f...
transnoentitiesnoconv($key, $param1='', $param2='', $param3='', $param4='', $param5='')
Return translated value of a text string If there is no match for this text, we look in alternative f...
getTradFromKey($key)
Return translated value of key for special keys ("Currency...", "Civility...", ......
getLabelFromNumber($number, $isamount='')
Return full text translated to language label for a key.
getCurrencyAmount($currency_code, $amount)
Return a currency code into its symbol.
get_translations_for_substitutions()
Return an array with content of all loaded translation keys (found into this->tab_translate) so we ge...
getLabelFromKey($db, $key, $tablename, $fieldkey, $fieldlabel, $keyforselect='', $filteronentity=0)
Return a label for a key.
load($domain, $alt=0, $stopafterdirection=0, $forcelangdir='', $loadfromfileonly=0, $forceloadifalreadynotfound=0, &$tabtranslatedomain=[], $langkey='')
Load translation key-value for a particular file, into a memory array.
get_available_languages($langdir=DOL_DOCUMENT_ROOT, $maxlength=0, $usecode=0, $mainlangonly=0)
Return list of all available languages.
loadCacheCurrencies($currency_code)
Load into the cache this->cache_currencies, all currencies.
convToOutputCharset($str, $pagecodefrom='UTF-8', $pagecodeto='')
Convert a string into output charset (this->charset_output that should be defined to conf->file->char...
setDefaultLang($srclang='en_US')
Set accessor for this->defaultlang.
file_exists($filename, $searchalt=0)
Return if a filename $filename exists for current language (or alternate language)
transcountry($str, $countrycode)
Return translation of a key depending on country.
isLoaded($domain)
Get information with result of loading data for domain.
trans($key, $param1='', $param2='', $param3='', $param4='', $maxsize=0)
Return text translated of text received as parameter (and encode it into HTML) If there is no match f...
transcountrynoentities($str, $countrycode)
Retourne la version traduite du texte passe en parameter complete du code pays.
getCurrencySymbol($currency_code, $forceloadall=0)
Return a currency code into its symbol.
loadLangs($domains)
Load translation files.
loadFromDatabase($db)
Load translation key-value from database into a memory array.
getDefaultLang($mode=0)
Return active language code for current user It's an accessor for this->defaultlang.
__construct($dir, $conf)
Constructor.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
dol_setcache($memoryid, $data, $expire=0)
Save data into a memory area shared by all users, all sessions on server.
dol_getcache($memoryid)
Read a memory area shared by all users, all sessions on server.