52 public $charset_output =
'UTF-8';
58 public $tab_translate = array();
63 private $_tab_loaded = array();
68 public $cache_labels = array();
73 public $cache_currencies = array();
79 private $cache_currencies_all_loaded =
false;
95 public $errors = array();
106 if (!empty(
$conf->file->character_set_client)) {
107 $this->charset_output =
$conf->file->character_set_client;
110 $this->dir = array($dir);
112 $this->dir =
$conf->file->dol_document_root;
133 foreach (
$conf->file->dol_document_root as $dir) {
135 if (!in_array($newdir, $this->dir)) {
136 $more[
'module_' . $i] = $newdir;
140 $this->dir = array_merge($more, $this->dir);
143 $this->origlang = $srclang;
145 if (empty($srclang) || $srclang ==
'auto') {
147 $langpref = empty($_SERVER[
'HTTP_ACCEPT_LANGUAGE']) ?
'' : $_SERVER[
'HTTP_ACCEPT_LANGUAGE'];
148 $langpref = preg_replace(
"/;([^,]*)/i",
"", $langpref);
149 $langpref = str_replace(
"-",
"_", $langpref);
150 $langlist = preg_split(
"/[;,]/", $langpref);
151 $codetouse = preg_replace(
'/[^_a-zA-Z]/',
'', $langlist[0]);
153 $codetouse = $srclang;
157 $langpart = explode(
"_", $codetouse);
159 if (!empty($langpart[1])) {
161 $longforshort = array(
'ar' =>
'ar_SA');
162 $longforshortexcep = array(
'ar_EG');
163 if (isset($longforshort[strtolower($langpart[0])]) && !in_array($codetouse, $longforshortexcep)) {
164 $srclang = $longforshort[strtolower($langpart[0])];
165 } elseif (!is_numeric($langpart[1])) {
166 $srclang = strtolower($langpart[0]) .
"_" . strtoupper($langpart[1]);
167 $longforlong = array(
'no_nb' =>
'nb_NO');
168 if (isset($longforlong[strtolower($srclang)])) {
169 $srclang = $longforlong[strtolower($srclang)];
172 $srclang = strtolower($langpart[0]) .
"_" . strtoupper($langpart[0]);
176 $longforshort = array(
177 'am' =>
'am_ET',
'ar' =>
'ar_SA',
'bn' =>
'bn_DB',
'el' =>
'el_GR',
'ca' =>
'ca_ES',
'cs' =>
'cs_CZ',
'en' =>
'en_US',
'fa' =>
'fa_IR',
178 'gl' =>
'gl_ES',
'he' =>
'he_IL',
'hi' =>
'hi_IN',
'ja' =>
'ja_JP',
179 'ka' =>
'ka_GE',
'km' =>
'km_KH',
'kn' =>
'kn_IN',
'ko' =>
'ko_KR',
'lo' =>
'lo_LA',
'nb' =>
'nb_NO',
'no' =>
'nb_NO',
'ne' =>
'ne_NP',
180 'sl' =>
'sl_SI',
'sq' =>
'sq_AL',
'sr' =>
'sr_RS',
'sv' =>
'sv_SE',
'uk' =>
'uk_UA',
'vi' =>
'vi_VN',
'zh' =>
'zh_CN'
182 if (isset($longforshort[strtolower($langpart[0])])) {
183 $srclang = $longforshort[strtolower($langpart[0])];
184 } elseif (!empty($langpart[0])) {
185 $srclang = strtolower($langpart[0]) .
"_" . strtoupper($langpart[0]);
191 $this->defaultlang = $srclang;
192 $this->shortlang = substr($srclang, 0, 2);
207 return $this->defaultlang;
209 return substr($this->defaultlang, 0, 2);
223 foreach ($domains as $domain) {
224 $result = $this->
load($domain);
227 } elseif ($result < 0) {
257 public function load($domain, $alt = 0, $stopafterdirection = 0, $forcelangdir =
'', $loadfromfileonly = 0, $forceloadifalreadynotfound = 0, &$tabtranslatedomain = [], $langkey =
'')
264 if (empty($domain)) {
265 dol_print_error(
null, get_class($this) .
"::Load ErrorWrongParameters");
268 if ($this->defaultlang ===
'none_NONE') {
274 if (empty($loadfromfileonly) && count($this->tab_translate) == 0) {
279 $newdomain = $domain;
284 if (preg_match(
'/^([^@]+)@([^@]+)$/i', $domain, $regs)) {
285 $newdomain = (string) $regs[1];
286 $modulename = (string) $regs[2];
291 !empty($this->_tab_loaded[$newdomain])
292 && ($this->_tab_loaded[$newdomain] != 2 || empty($forceloadifalreadynotfound))
299 $langofdir = (empty($forcelangdir) ? $this->defaultlang : $forcelangdir);
300 $langkey = (empty($langkey) ? $langofdir : $langkey);
303 $langarray = explode(
'_', $langofdir);
304 if ($alt < 1 && isset($langarray[1]) && (strtolower($langarray[0]) == strtolower($langarray[1]) || in_array(strtolower($langofdir), array(
'el_gr')))) {
307 if ($alt < 2 && strtolower($langofdir) ==
'en_us') {
311 if (empty($langofdir)) {
312 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);
317 foreach ($this->dir as $searchdir) {
319 $file_lang = $searchdir . ($modulename ?
'/' . $modulename :
'') .
"/langs/" . $langofdir .
"/" . $newdomain .
".lang";
323 $filelangexists = @is_file($file_lang_osencoded);
328 if ($filelangexists) {
336 $usecachekey = $newdomain .
'_' . $langkey .
'_' . md5($file_lang);
339 $usecachekey = $newdomain;
344 require_once DOL_DOCUMENT_ROOT .
'/core/lib/memory.lib.php';
346 if (is_array($tmparray) && count($tmparray)) {
347 $this->tab_translate += $tmparray;
356 if ($fp = @fopen($file_lang,
"rt")) {
364 while ($line = fscanf($fp,
"%[^= ]%*[ =]%[^\n\r]")) {
365 if (isset($line[1])) {
366 list($key, $value) = $line;
369 if (empty($this->tab_translate[$key])) {
370 if ($key ==
'DIRECTION') {
371 if ($alt < 2 || empty($this->tab_translate[$key])) {
372 $this->tab_translate[$key] = $value;
373 if ($stopafterdirection) {
375 } elseif ($usecachekey) {
376 $tabtranslatedomain[$key] = $value;
379 } elseif ($key[0] ==
'#') {
383 $this->tab_translate[$key] = str_replace(array(
'\\n',
'\\\\s'), array(
"\n",
'\s'), $value);
385 $tabtranslatedomain[$key] = $value;
406 $langofdir = strtolower($langarray[0]) .
'_' . strtoupper($langarray[0]);
407 if ($langofdir ==
'el_EL') {
408 $langofdir =
'el_GR';
410 if ($langofdir ==
'ar_AR') {
411 $langofdir =
'ar_SA';
413 $this->
load($domain, $alt + 1, $stopafterdirection, $langofdir, 0, 0, $tabtranslatedomain, $langkey);
420 $langofdir =
'en_US';
421 $this->
load($domain, $alt + 1, $stopafterdirection, $langofdir, 0, 0, $tabtranslatedomain, $langkey);
427 $this->_tab_loaded[$newdomain] = 1;
430 if (empty($this->_tab_loaded[$newdomain])) {
431 $this->_tab_loaded[$newdomain] = 2;
437 if (empty($loadfromfileonly)) {
438 $overwritekey =
'MAIN_OVERWRITE_TRANS_' . $this->defaultlang;
442 foreach ($tmparray as $tmp) {
443 $tmparray2 = explode(
':', $tmp);
444 if (!empty($tmparray2[1])) {
445 $this->tab_translate[$tmparray2[0]] = $tmparray2[1];
452 if ($usecachekey && count($tabtranslatedomain)) {
453 $ressetcache =
dol_setcache($usecachekey, $tabtranslatedomain);
454 if ($ressetcache < 0) {
455 $error =
'Failed to set cache for usecachekey=' . $usecachekey .
' result=' . $ressetcache;
462 !empty($this->tab_translate[
"SeparatorDecimal"]) && !empty($this->tab_translate[
"SeparatorThousand"])
463 && $this->tab_translate[
"SeparatorDecimal"] == $this->tab_translate[
"SeparatorThousand"]
465 $this->tab_translate[
"SeparatorThousand"] =
'';
487 $domain =
'database';
496 $newdomain = $domain;
499 if (!empty($this->_tab_loaded[$newdomain])) {
504 $this->_tab_loaded[$newdomain] = 2;
507 $langofdir = $this->defaultlang;
509 if (empty($langofdir)) {
510 dol_syslog(
"Error: " . get_class($this) .
"::loadFromDatabase was called but language was not set yet with langs->setDefaultLang(). Nothing will be loaded.", LOG_WARNING);
521 $usecachekey = $newdomain .
'_' . $langofdir;
524 $usecachekey = $newdomain;
531 require_once DOL_DOCUMENT_ROOT .
'/core/lib/memory.lib.php';
533 if (is_array($tmparray) && count($tmparray)) {
534 $this->tab_translate += $tmparray;
544 $sql =
"SELECT transkey, transvalue FROM ".$db->prefix().
"overwrite_trans where (lang='".$db->escape($this->defaultlang).
"' OR lang IS NULL)";
545 $sql .=
" AND entity IN (0, ".getEntity(
'overwrite_trans').
")";
546 $sql .= $db->order(
"lang",
"DESC");
548 $resql = $db->query($sql);
551 $num = $db->num_rows($resql);
553 $tabtranslatedomain = array();
557 $obj = $db->fetch_object($resql);
559 $key = $obj->transkey;
560 $value = $obj->transvalue;
563 if (empty($this->tab_translate[$key])) {
565 $this->tab_translate[$key] = str_replace(array(
'\\n',
'\\\\s'), array(
"\n",
'\s'), $value);
568 $tabtranslatedomain[$key] = $value;
579 if ($usecachekey && count($tabtranslatedomain)) {
580 $ressetcache =
dol_setcache($usecachekey, $tabtranslatedomain);
581 if ($ressetcache < 0) {
582 $error =
'Failed to set cache for usecachekey=' . $usecachekey .
' result=' . $ressetcache;
593 $this->_tab_loaded[$newdomain] = 1;
607 return $this->_tab_loaded[$domain];
625 if (!is_string($key)) {
627 return 'ErrorBadValueForParamNotAString';
632 if (preg_match(
'/^Civility([0-9A-Z]+)$/i', $key, $reg)) {
633 $newstr = $this->
getLabelFromKey($db, $reg[1],
'c_civility',
'code',
'label');
634 } elseif (preg_match(
'/^Currency([A-Z][A-Z][A-Z])$/i', $key, $reg)) {
635 $newstr = $this->
getLabelFromKey($db, $reg[1],
'c_currencies',
'code_iso',
'label');
636 } elseif (preg_match(
'/^SendingMethod([0-9A-Z]+)$/i', $key, $reg)) {
637 $newstr = $this->
getLabelFromKey($db, $reg[1],
'c_shipment_mode',
'code',
'libelle');
638 } elseif (preg_match(
'/^PaymentType(?:Short)?([0-9A-Z]+)$/i', $key, $reg)) {
639 $newstr = $this->
getLabelFromKey($db, $reg[1],
'c_paiement',
'code',
'libelle',
'', 1);
640 } elseif (preg_match(
'/^OppStatus([0-9A-Z]+)$/i', $key, $reg)) {
641 $newstr = $this->
getLabelFromKey($db, $reg[1],
'c_lead_status',
'code',
'label');
642 } elseif (preg_match(
'/^OrderSource([0-9A-Z]+)$/i', $key, $reg)) {
670 public function trans($key, $param1 =
'', $param2 =
'', $param3 =
'', $param4 =
'', $maxsize = 0)
672 if (!empty($this->tab_translate[$key])) {
673 $str = $this->tab_translate[$key];
676 $replacekey =
'MAIN_REPLACE_TRANS_' . $this->defaultlang;
679 foreach ($tmparray as $tmp) {
680 $tmparray2 = explode(
':', $tmp);
681 $str = preg_replace(
'/' . preg_quote($tmparray2[0]) .
'/', $tmparray2[1], $str);
688 array(
'"',
'<b>',
'</b>',
'<u>',
'</u>',
'<i',
'</i>',
'<center>',
'</center>',
'<strong>',
'</strong>',
'<a ',
'</a>',
'<br>',
'<span',
'</span>',
'< ',
'>'),
689 array(
'__quot__',
'__tagb__',
'__tagbend__',
'__tagu__',
'__taguend__',
'__tagi__',
'__tagiend__',
'__tagcenter__',
'__tagcenterend__',
'__tagb__',
'__tagbend__',
'__taga__',
'__tagaend__',
'__tagbr__',
'__tagspan__',
'__tagspanend__',
'__ltspace__',
'__gt__'),
693 if (strpos($key,
'Format') !== 0) {
696 $str = sprintf($str, $param1, $param2, $param3, $param4);
703 $str = htmlentities($str, ENT_COMPAT, $this->charset_output);
707 array(
'__quot__',
'__tagb__',
'__tagbend__',
'__tagu__',
'__taguend__',
'__tagi__',
'__tagiend__',
'__tagcenter__',
'__tagcenterend__',
'__taga__',
'__tagaend__',
'__tagbr__',
'__tagspan__',
'__tagspanend__',
'__ltspace__',
'__gt__'),
708 array(
'"',
'<b>',
'</b>',
'<u>',
'</u>',
'<i',
'</i>',
'<center>',
'</center>',
'<a ',
'</a>',
'<br>',
'<span',
'</span>',
'< ',
'>'),
714 $str = str_replace(array(
'%27',
'''),
'', $str);
741 public function transnoentities($key, $param1 =
'', $param2 =
'', $param3 =
'', $param4 =
'', $param5 =
'')
766 if (!empty($this->tab_translate[$key])) {
767 $str = $this->tab_translate[$key];
770 $replacekey =
'MAIN_REPLACE_TRANS_' . $this->defaultlang;
773 foreach ($tmparray as $tmp) {
774 $tmparray2 = explode(
':', $tmp);
775 $str = preg_replace(
'/' . preg_quote($tmparray2[0]) .
'/', $tmparray2[1], $str);
779 if (!preg_match(
'/^Format/', $key)) {
782 $str = sprintf($str, $param1, $param2, $param3, $param4, $param5);
787 $str = str_replace(array(
'%27',
'''),
'', $str);
806 $strLocaleKey = $str.$countrycode;
807 if (!empty($this->tab_translate[$strLocaleKey])) {
808 return $this->
trans($strLocaleKey);
810 return $this->
trans($str);
825 $strLocaleKey = $str.$countrycode;
826 if (!empty($this->tab_translate[$strLocaleKey])) {
844 if (empty($pagecodeto)) {
845 $pagecodeto = $this->charset_output;
848 if ($pagecodefrom ==
'ISO-8859-1' && $pagecodeto ==
'UTF-8') {
849 $str = mb_convert_encoding($str,
'UTF-8',
'ISO-8859-1');
851 if ($pagecodefrom ==
'UTF-8' && $pagecodeto ==
'ISO-8859-1') {
852 $str = mb_convert_encoding(str_replace(
'€', chr(128), $str),
'ISO-8859-1');
872 $this->
load(
"languages");
875 $handle = opendir($langdir .
"/langs");
876 $langs_available = array();
877 while ($dir = trim(readdir($handle))) {
879 if (preg_match(
'/^([a-z]+)_([A-Z]+)/i', $dir, $regs)) {
882 $arrayofspecialmainlanguages = array(
911 if (strtolower($regs[1]) != strtolower($regs[2]) && !in_array($dir, $arrayofspecialmainlanguages)) {
921 $langs_available[$dir] = $dir;
925 $langs_available[$dir] = $dir .
': ' .
dol_trunc($this->
trans(
'Language_' . $dir), $maxlength);
927 $langs_available[$dir] = $this->
trans(
'Language_' . $dir);
930 $langs_available[$dir] = str_replace(
' (United States)',
'', $langs_available[$dir]);
934 return $langs_available;
950 foreach ($this->dir as $searchdir) {
951 if (is_readable(
dol_osencode($searchdir .
"/langs/" . $this->defaultlang .
"/" . $filename))) {
958 if ($this->defaultlang !=
"en_US") {
959 $filenamealt = $searchdir .
"/langs/en_US/" . $filename;
962 if ($filenamealt !==
null && is_readable(
dol_osencode($filenamealt))) {
987 $newnumber = $number;
989 $dirsubstitutions = array_merge(array(),
$conf->modules_parts[
'substitutions']);
990 foreach ($dirsubstitutions as $reldir) {
995 if (!is_dir($newdir)) {
999 $fonc =
'numberwords';
1000 if (
file_exists($newdir .
'/functions_' . $fonc .
'.lib.php')) {
1001 include_once $newdir .
'/functions_' . $fonc .
'.lib.php';
1002 if (function_exists(
'numberwords_getLabelFromNumber')) {
1003 $newnumber = numberwords_getLabelFromNumber($this, $number, $isamount);
1028 public function getLabelFromKey($db, $key, $tablename, $fieldkey, $fieldlabel, $keyforselect =
'', $filteronentity = 0)
1035 if (preg_match(
'/[^0-9A-Z_]/i', $tablename) || preg_match(
'/[^0-9A-Z_]/i', $fieldkey) || preg_match(
'/[^0-9A-Z_]/i', $fieldlabel)) {
1036 $this->error =
'Bad value for parameter tablename, fieldkey or fieldlabel';
1044 if ($tmp != $key && $tmp !=
'ErrorBadValueForParamNotAString') {
1049 if (isset($this->cache_labels[$tablename][$key])) {
1050 return $this->cache_labels[$tablename][$key];
1054 $sql =
"SELECT " . $fieldlabel .
" as label";
1055 $sql .=
" FROM " . $db->prefix() . $tablename;
1056 $sql .=
" WHERE " . $fieldkey .
" = '" . $db->escape($keyforselect ? $keyforselect : $key) .
"'";
1057 if ($filteronentity) {
1058 $sql .=
" AND entity IN (" .
getEntity($tablename) .
')';
1060 dol_syslog(get_class($this) .
'::getLabelFromKey', LOG_DEBUG);
1061 $resql = $db->query($sql);
1063 $obj = $db->fetch_object($resql);
1065 $this->cache_labels[$tablename][$key] = (string) $obj->label;
1067 $this->cache_labels[$tablename][$key] = $key;
1071 return $this->cache_labels[$tablename][$key];
1073 $this->error = $db->lasterror();
1092 if (in_array($currency_code, array(
'USD'))) {
1093 return $symbol . $amount;
1095 return $amount . $symbol;
1109 $currency_sign =
'';
1111 if (function_exists(
"mb_convert_encoding")) {
1114 if (isset($this->cache_currencies[$currency_code]) && !empty($this->cache_currencies[$currency_code][
'unicode']) && is_array($this->cache_currencies[$currency_code][
'unicode'])) {
1115 foreach ($this->cache_currencies[$currency_code][
'unicode'] as $unicode) {
1116 $currency_sign .= mb_convert_encoding(
"&#" . $unicode .
";",
"UTF-8",
'HTML-ENTITIES');
1121 return ($currency_sign ? $currency_sign : $currency_code);
1134 if ($this->cache_currencies_all_loaded) {
1137 if (!empty($currency_code) && isset($this->cache_currencies[$currency_code])) {
1141 $sql =
"SELECT code_iso, label, unicode";
1142 $sql .=
" FROM " . $db->prefix() .
"c_currencies";
1143 $sql .=
" WHERE active = 1";
1144 if (!empty($currency_code)) {
1145 $sql .=
" AND code_iso = '" . $db->escape($currency_code) .
"'";
1149 dol_syslog(get_class($this) .
'::loadCacheCurrencies', LOG_DEBUG);
1150 $resql = $db->query($sql);
1152 $this->
load(
"dict");
1154 if (!empty($currency_code)) {
1155 foreach ($this->cache_currencies as $key => $val) {
1156 $label[$key] = $val[
'label'];
1160 $num = $db->num_rows($resql);
1163 $obj = $db->fetch_object($resql);
1166 $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 :
''));
1167 $this->cache_currencies[$obj->code_iso][
'unicode'] = (array) json_decode((empty($obj->unicode) ?
'' : $obj->unicode),
true);
1168 $label[$obj->code_iso] = $this->cache_currencies[$obj->code_iso][
'label'];
1172 if (empty($currency_code)) {
1173 $this->cache_currencies_all_loaded =
true;
1178 array_multisort($label, SORT_ASC, $this->cache_currencies);
1197 $substitutionarray = array();
1199 foreach ($this->tab_translate as $code => $label) {
1200 $substitutionarray[
'lang_' . $code] = $label;
1201 $substitutionarray[
'__(' . $code .
')__'] = $label;
1204 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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
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.