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 = $regs[1];
285 $modulename = $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")) {
365 while ($line = fscanf($fp,
"%[^= ]%*[ =]%[^\n\r]")) {
366 if (isset($line[1])) {
367 list($key, $value) = $line;
370 if (empty($this->tab_translate[$key])) {
371 if ($key ==
'DIRECTION') {
372 if ($alt < 2 || empty($this->tab_translate[$key])) {
373 $this->tab_translate[$key] = $value;
374 if ($stopafterdirection) {
376 } elseif ($usecachekey) {
377 $tabtranslatedomain[$key] = $value;
380 } elseif ($key[0] ==
'#') {
384 $this->tab_translate[$key] = str_replace(array(
'\\n',
'\\\\s'), array(
"\n",
'\s'), $value);
386 $tabtranslatedomain[$key] = $value;
407 $langofdir = strtolower($langarray[0]) .
'_' . strtoupper($langarray[0]);
408 if ($langofdir ==
'el_EL') {
409 $langofdir =
'el_GR';
411 if ($langofdir ==
'ar_AR') {
412 $langofdir =
'ar_SA';
414 $this->
load($domain, $alt + 1, $stopafterdirection, $langofdir, 0, 0, $tabtranslatedomain, $langkey);
421 $langofdir =
'en_US';
422 $this->
load($domain, $alt + 1, $stopafterdirection, $langofdir, 0, 0, $tabtranslatedomain, $langkey);
428 $this->_tab_loaded[$newdomain] = 1;
431 if (empty($this->_tab_loaded[$newdomain])) {
432 $this->_tab_loaded[$newdomain] = 2;
438 if (empty($loadfromfileonly)) {
439 $overwritekey =
'MAIN_OVERWRITE_TRANS_' . $this->defaultlang;
440 if (!empty($conf->global->$overwritekey)) {
443 foreach ($tmparray as $tmp) {
444 $tmparray2 = explode(
':', $tmp);
445 if (!empty($tmparray2[1])) {
446 $this->tab_translate[$tmparray2[0]] = $tmparray2[1];
453 if ($usecachekey && count($tabtranslatedomain)) {
454 $ressetcache =
dol_setcache($usecachekey, $tabtranslatedomain);
455 if ($ressetcache < 0) {
456 $error =
'Failed to set cache for usecachekey=' . $usecachekey .
' result=' . $ressetcache;
463 !empty($this->tab_translate[
"SeparatorDecimal"]) && !empty($this->tab_translate[
"SeparatorThousand"])
464 && $this->tab_translate[
"SeparatorDecimal"] == $this->tab_translate[
"SeparatorThousand"]
466 $this->tab_translate[
"SeparatorThousand"] =
'';
488 $domain =
'database';
497 $newdomain = $domain;
500 if (!empty($this->_tab_loaded[$newdomain])) {
505 $this->_tab_loaded[$newdomain] = 1;
508 $langofdir = $this->defaultlang;
510 if (empty($langofdir)) {
511 dol_syslog(
"Error: " . get_class($this) .
"::loadFromDatabase was called but language was not set yet with langs->setDefaultLang(). Nothing will be loaded.", LOG_WARNING);
522 $usecachekey = $newdomain .
'_' . $langofdir;
525 $usecachekey = $newdomain;
532 require_once DOL_DOCUMENT_ROOT .
'/core/lib/memory.lib.php';
534 if (is_array($tmparray) && count($tmparray)) {
535 $this->tab_translate += $tmparray;
545 $sql =
"SELECT transkey, transvalue FROM ".$db->prefix().
"overwrite_trans where (lang='".$db->escape($this->defaultlang).
"' OR lang IS NULL)";
546 $sql .=
" AND entity IN (0, ".getEntity(
'overwrite_trans').
")";
547 $sql .= $db->order(
"lang",
"DESC");
549 $resql = $db->query($sql);
552 $num = $db->num_rows($resql);
555 $tabtranslatedomain = array();
560 $obj = $db->fetch_object($resql);
562 $key = $obj->transkey;
563 $value = $obj->transvalue;
566 if (empty($this->tab_translate[$key])) {
568 $this->tab_translate[$key] = str_replace(array(
'\\n',
'\\\\s'), array(
"\n",
'\s'), $value);
571 $tabtranslatedomain[$key] = $value;
582 if ($usecachekey && count($tabtranslatedomain)) {
583 $ressetcache =
dol_setcache($usecachekey, $tabtranslatedomain);
584 if ($ressetcache < 0) {
585 $error =
'Failed to set cache for usecachekey=' . $usecachekey .
' result=' . $ressetcache;
596 $this->_tab_loaded[$newdomain] = 1;
599 if (empty($this->_tab_loaded[$newdomain])) {
600 $this->_tab_loaded[$newdomain] = 2;
614 return $this->_tab_loaded[$domain];
632 if (!is_string($key)) {
634 return 'ErrorBadValueForParamNotAString';
639 if (preg_match(
'/^Civility([0-9A-Z]+)$/i', $key, $reg)) {
640 $newstr = $this->
getLabelFromKey($db, $reg[1],
'c_civility',
'code',
'label');
641 } elseif (preg_match(
'/^Currency([A-Z][A-Z][A-Z])$/i', $key, $reg)) {
642 $newstr = $this->
getLabelFromKey($db, $reg[1],
'c_currencies',
'code_iso',
'label');
643 } elseif (preg_match(
'/^SendingMethod([0-9A-Z]+)$/i', $key, $reg)) {
644 $newstr = $this->
getLabelFromKey($db, $reg[1],
'c_shipment_mode',
'code',
'libelle');
645 } elseif (preg_match(
'/^PaymentType(?:Short)?([0-9A-Z]+)$/i', $key, $reg)) {
646 $newstr = $this->
getLabelFromKey($db, $reg[1],
'c_paiement',
'code',
'libelle',
'', 1);
647 } elseif (preg_match(
'/^OppStatus([0-9A-Z]+)$/i', $key, $reg)) {
648 $newstr = $this->
getLabelFromKey($db, $reg[1],
'c_lead_status',
'code',
'label');
649 } elseif (preg_match(
'/^OrderSource([0-9A-Z]+)$/i', $key, $reg)) {
677 public function trans($key, $param1 =
'', $param2 =
'', $param3 =
'', $param4 =
'', $maxsize = 0)
681 if (!empty($this->tab_translate[$key])) {
682 $str = $this->tab_translate[$key];
685 $replacekey =
'MAIN_REPLACE_TRANS_' . $this->defaultlang;
686 if (!empty($conf->global->$replacekey)) {
688 foreach ($tmparray as $tmp) {
689 $tmparray2 = explode(
':', $tmp);
690 $str = preg_replace(
'/' . preg_quote($tmparray2[0]) .
'/', $tmparray2[1], $str);
697 array(
'"',
'<b>',
'</b>',
'<u>',
'</u>',
'<i',
'</i>',
'<center>',
'</center>',
'<strong>',
'</strong>',
'<a ',
'</a>',
'<br>',
'<span',
'</span>',
'< ',
'>'),
698 array(
'__quot__',
'__tagb__',
'__tagbend__',
'__tagu__',
'__taguend__',
'__tagi__',
'__tagiend__',
'__tagcenter__',
'__tagcenterend__',
'__tagb__',
'__tagbend__',
'__taga__',
'__tagaend__',
'__tagbr__',
'__tagspan__',
'__tagspanend__',
'__ltspace__',
'__gt__'),
702 if (strpos($key,
'Format') !== 0) {
705 $str = sprintf($str, $param1, $param2, $param3, $param4);
712 $str = htmlentities($str, ENT_COMPAT, $this->charset_output);
716 array(
'__quot__',
'__tagb__',
'__tagbend__',
'__tagu__',
'__taguend__',
'__tagi__',
'__tagiend__',
'__tagcenter__',
'__tagcenterend__',
'__taga__',
'__tagaend__',
'__tagbr__',
'__tagspan__',
'__tagspanend__',
'__ltspace__',
'__gt__'),
717 array(
'"',
'<b>',
'</b>',
'<u>',
'</u>',
'<i',
'</i>',
'<center>',
'</center>',
'<a ',
'</a>',
'<br>',
'<span',
'</span>',
'< ',
'>'),
723 $str = str_replace(array(
'%27',
'''),
'', $str);
750 public function transnoentities($key, $param1 =
'', $param2 =
'', $param3 =
'', $param4 =
'', $param5 =
'')
775 if (!empty($this->tab_translate[$key])) {
776 $str = $this->tab_translate[$key];
779 $replacekey =
'MAIN_REPLACE_TRANS_' . $this->defaultlang;
780 if (!empty($conf->global->$replacekey)) {
782 foreach ($tmparray as $tmp) {
783 $tmparray2 = explode(
':', $tmp);
784 $str = preg_replace(
'/' . preg_quote($tmparray2[0]) .
'/', $tmparray2[1], $str);
788 if (!preg_match(
'/^Format/', $key)) {
791 $str = sprintf($str, $param1, $param2, $param3, $param4, $param5);
796 $str = str_replace(array(
'%27',
'''),
'', $str);
815 $strLocaleKey = $str.$countrycode;
816 if (!empty($this->tab_translate[$strLocaleKey])) {
817 return $this->
trans($strLocaleKey);
819 return $this->
trans($str);
834 $strLocaleKey = $str.$countrycode;
835 if (!empty($this->tab_translate[$strLocaleKey])) {
852 if ($pagecodefrom ==
'ISO-8859-1' && $this->charset_output ==
'UTF-8') {
853 $str = mb_convert_encoding($str,
'UTF-8',
'ISO-8859-1');
855 if ($pagecodefrom ==
'UTF-8' && $this->charset_output ==
'ISO-8859-1') {
856 $str = mb_convert_encoding(str_replace(
'€', chr(128), $str),
'ISO-8859-1');
878 $this->
load(
"languages");
881 $handle = opendir($langdir .
"/langs");
882 $langs_available = array();
883 while ($dir = trim(readdir($handle))) {
885 if (preg_match(
'/^([a-z]+)_([A-Z]+)/i', $dir, $regs)) {
888 $arrayofspecialmainlanguages = array(
917 if (strtolower($regs[1]) != strtolower($regs[2]) && !in_array($dir, $arrayofspecialmainlanguages)) {
927 $langs_available[$dir] = $dir;
931 $langs_available[$dir] = $dir .
': ' .
dol_trunc($this->
trans(
'Language_' . $dir), $maxlength);
933 $langs_available[$dir] = $this->
trans(
'Language_' . $dir);
936 $langs_available[$dir] = str_replace(
' (United States)',
'', $langs_available[$dir]);
940 return $langs_available;
956 foreach ($this->dir as $searchdir) {
957 if (is_readable(
dol_osencode($searchdir .
"/langs/" . $this->defaultlang .
"/" . $filename))) {
963 if ($this->defaultlang !=
"en_US") {
964 $filenamealt = $searchdir .
"/langs/en_US/" . $filename;
992 $newnumber = $number;
994 $dirsubstitutions = array_merge(array(), $conf->modules_parts[
'substitutions']);
995 foreach ($dirsubstitutions as $reldir) {
1000 if (!is_dir($newdir)) {
1004 $fonc =
'numberwords';
1005 if (
file_exists($newdir .
'/functions_' . $fonc .
'.lib.php')) {
1006 include_once $newdir .
'/functions_' . $fonc .
'.lib.php';
1007 if (function_exists(
'numberwords_getLabelFromNumber')) {
1008 $newnumber = numberwords_getLabelFromNumber($this, $number, $isamount);
1033 public function getLabelFromKey($db, $key, $tablename, $fieldkey, $fieldlabel, $keyforselect =
'', $filteronentity = 0)
1040 if (preg_match(
'/[^0-9A-Z_]/i', $tablename) || preg_match(
'/[^0-9A-Z_]/i', $fieldkey) || preg_match(
'/[^0-9A-Z_]/i', $fieldlabel)) {
1041 $this->error =
'Bad value for parameter tablename, fieldkey or fieldlabel';
1049 if ($tmp != $key && $tmp !=
'ErrorBadValueForParamNotAString') {
1054 if (isset($this->cache_labels[$tablename][$key])) {
1055 return $this->cache_labels[$tablename][$key];
1059 $sql =
"SELECT " . $fieldlabel .
" as label";
1060 $sql .=
" FROM " . $db->prefix() . $tablename;
1061 $sql .=
" WHERE " . $fieldkey .
" = '" . $db->escape($keyforselect ? $keyforselect : $key) .
"'";
1062 if ($filteronentity) {
1063 $sql .=
" AND entity IN (" .
getEntity($tablename) .
')';
1065 dol_syslog(get_class($this) .
'::getLabelFromKey', LOG_DEBUG);
1066 $resql = $db->query($sql);
1068 $obj = $db->fetch_object($resql);
1070 $this->cache_labels[$tablename][$key] = $obj->label;
1072 $this->cache_labels[$tablename][$key] = $key;
1076 return $this->cache_labels[$tablename][$key];
1078 $this->error = $db->lasterror();
1097 if (in_array($currency_code, array(
'USD'))) {
1098 return $symbol . $amount;
1100 return $amount . $symbol;
1114 $currency_sign =
'';
1116 if (function_exists(
"mb_convert_encoding")) {
1119 if (isset($this->cache_currencies[$currency_code]) && !empty($this->cache_currencies[$currency_code][
'unicode']) && is_array($this->cache_currencies[$currency_code][
'unicode'])) {
1120 foreach ($this->cache_currencies[$currency_code][
'unicode'] as $unicode) {
1121 $currency_sign .= mb_convert_encoding(
"&#" . $unicode .
";",
"UTF-8",
'HTML-ENTITIES');
1126 return ($currency_sign ? $currency_sign : $currency_code);
1139 if ($this->cache_currencies_all_loaded) {
1142 if (!empty($currency_code) && isset($this->cache_currencies[$currency_code])) {
1146 $sql =
"SELECT code_iso, label, unicode";
1147 $sql .=
" FROM " . $db->prefix() .
"c_currencies";
1148 $sql .=
" WHERE active = 1";
1149 if (!empty($currency_code)) {
1150 $sql .=
" AND code_iso = '" . $db->escape($currency_code) .
"'";
1154 dol_syslog(get_class($this) .
'::loadCacheCurrencies', LOG_DEBUG);
1155 $resql = $db->query($sql);
1157 $this->
load(
"dict");
1159 if (!empty($currency_code)) {
1160 foreach ($this->cache_currencies as $key => $val) {
1161 $label[$key] = $val[
'label'];
1165 $num = $db->num_rows($resql);
1168 $obj = $db->fetch_object($resql);
1171 $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 :
''));
1172 $this->cache_currencies[$obj->code_iso][
'unicode'] = (array) json_decode((empty($obj->unicode) ?
'' : $obj->unicode),
true);
1173 $label[$obj->code_iso] = $this->cache_currencies[$obj->code_iso][
'label'];
1177 if (empty($currency_code)) {
1178 $this->cache_currencies_all_loaded =
true;
1183 array_multisort($label, SORT_ASC, $this->cache_currencies);
1202 $substitutionarray = array();
1204 foreach ($this->tab_translate as $code => $label) {
1205 $substitutionarray[
'lang_' . $code] = $label;
1206 $substitutionarray[
'__(' . $code .
')__'] = $label;
1209 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.
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.
convToOutputCharset($str, $pagecodefrom='UTF-8')
Convert a string into output charset (this->charset_output that should be defined to conf->file->char...
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 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.