dolibarr  19.0.0-dev
Public Member Functions | Private Member Functions | List of all members
Translate Class Reference

Class to manage translations. More...

Public Member Functions

 __construct ($dir, $conf)
 Constructor. More...
 
 setDefaultLang ($srclang='en_US')
 Set accessor for this->defaultlang. More...
 
 getDefaultLang ($mode=0)
 Return active language code for current user It's an accessor for this->defaultlang. More...
 
 loadLangs ($domains)
 Load translation files. More...
 
 load ($domain, $alt=0, $stopafterdirection=0, $forcelangdir='', $loadfromfileonly=0, $forceloadifalreadynotfound=0)
 Load translation key-value for a particular file, into a memory array. More...
 
 loadFromDatabase ($db)
 Load translation key-value from database into a memory array. More...
 
 isLoaded ($domain)
 Get information with result of loading data for domain. More...
 
 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 for this text, we look in alternative file and if still not found, it is returned as it is. More...
 
 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 file and if still not found it is returned as is. More...
 
 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 file and if still not found, it is returned as is. More...
 
 transcountry ($str, $countrycode)
 Return translation of a key depending on country. More...
 
 transcountrynoentities ($str, $countrycode)
 Retourne la version traduite du texte passe en parametre complete du code pays. More...
 
 convToOutputCharset ($str, $pagecodefrom='UTF-8')
 Convert a string into output charset (this->charset_output that should be defined to conf->file->character_set_client) More...
 
 get_available_languages ($langdir=DOL_DOCUMENT_ROOT, $maxlength=0, $usecode=0, $mainlangonly=0)
 Return list of all available languages. More...
 
 file_exists ($filename, $searchalt=0)
 Return if a filename $filename exists for current language (or alternate language) More...
 
 getLabelFromNumber ($number, $isamount='')
 Return full text translated to language label for a key. More...
 
 getLabelFromKey ($db, $key, $tablename, $fieldkey, $fieldlabel, $keyforselect='', $filteronentity=0)
 Return a label for a key. More...
 
 getCurrencyAmount ($currency_code, $amount)
 Return a currency code into its symbol. More...
 
 getCurrencySymbol ($currency_code, $forceloadall=0)
 Return a currency code into its symbol. More...
 
 loadCacheCurrencies ($currency_code)
 Load into the cache this->cache_currencies, all currencies. More...
 
 get_translations_for_substitutions ()
 Return an array with content of all loaded translation keys (found into this->tab_translate) so we get a substitution array we can use for substitutions (for mail or ODT generation for example) More...
 

Private Member Functions

 getTradFromKey ($key)
 Return translated value of key for special keys ("Currency...", "Civility...", ...). More...
 

Detailed Description

Class to manage translations.

Definition at line 30 of file translate.class.php.

Constructor & Destructor Documentation

◆ __construct()

Translate::__construct (   $dir,
  $conf 
)

Constructor.

Parameters
string$dirForce directory that contains /langs subdirectory (value is sometimes '..' like into install/* pages or support/* pages). Use '' by default.
Conf$confObject with Dolibarr configuration

Definition at line 55 of file translate.class.php.

Member Function Documentation

◆ convToOutputCharset()

Translate::convToOutputCharset (   $str,
  $pagecodefrom = 'UTF-8' 
)

Convert a string into output charset (this->charset_output that should be defined to conf->file->character_set_client)

Parameters
string$strString to convert
string$pagecodefromPage code of src string
Returns
string Converted string

Definition at line 793 of file translate.class.php.

◆ file_exists()

Translate::file_exists (   $filename,
  $searchalt = 0 
)

Return if a filename $filename exists for current language (or alternate language)

Parameters
string$filenameLanguage filename to search
integer$searchaltSearch also alernate language file
Returns
boolean true if exists and readable

Definition at line 895 of file translate.class.php.

◆ get_available_languages()

Translate::get_available_languages (   $langdir = DOL_DOCUMENT_ROOT,
  $maxlength = 0,
  $usecode = 0,
  $mainlangonly = 0 
)

Return list of all available languages.

Parameters
string$langdirDirectory to scan
integer$maxlengthMax length for each value in combo box (will be truncated)
int$usecode1=Show code instead of country name for language variant, 2=Show only code
int$mainlangonly1=Show only main languages ('fr_FR' no' fr_BE', 'es_ES' not 'es_MX', ...)
Returns
array List of languages

Definition at line 816 of file translate.class.php.

◆ get_translations_for_substitutions()

Translate::get_translations_for_substitutions ( )

Return an array with content of all loaded translation keys (found into this->tab_translate) so we get a substitution array we can use for substitutions (for mail or ODT generation for example)

Returns
array Array of translation keys lang_key => string_translation_loaded

Definition at line 1142 of file translate.class.php.

◆ getCurrencyAmount()

Translate::getCurrencyAmount (   $currency_code,
  $amount 
)

Return a currency code into its symbol.

Parameters
string$currency_codeCurrency Code
string$amountIf not '', show currency + amount according to langs ($10, 10€).
Returns
string Amount + Currency symbol encoded into UTF8
Deprecated:
Use method price to output a price
See also
price()

Definition at line 1036 of file translate.class.php.

◆ getCurrencySymbol()

Translate::getCurrencySymbol (   $currency_code,
  $forceloadall = 0 
)

Return a currency code into its symbol.

If mb_convert_encoding is not available, return currency code.

Parameters
string$currency_codeCurrency code
integer$forceloadall1=Force to load all currencies into cache. We know we need to use all of them. By default read and cache only the requested currency.
Returns
string Currency symbol encoded into UTF8

Definition at line 1055 of file translate.class.php.

References loadCacheCurrencies().

◆ getDefaultLang()

Translate::getDefaultLang (   $mode = 0)

Return active language code for current user It's an accessor for this->defaultlang.

Parameters
int$mode0=Long language code, 1=Short language code (en, fr, es, ...)
Returns
string Language code used (en_US, en_AU, fr_FR, ...)

Definition at line 155 of file translate.class.php.

◆ getLabelFromKey()

Translate::getLabelFromKey (   $db,
  $key,
  $tablename,
  $fieldkey,
  $fieldlabel,
  $keyforselect = '',
  $filteronentity = 0 
)

Return a label for a key.

Search into translation array, then into cache, then if still not found, search into database. Store key-label found into cache variable $this->cache_labels to save SQL requests to get labels.

Parameters
DoliDB$dbDatabase handler
string$keyTranslation key to get label (key in language file)
string$tablenameTable name without prefix. This value must always be a hardcoded string and not a value coming from user input.
string$fieldkeyField for key. This value must always be a hardcoded string and not a value coming from user input.
string$fieldlabelField for label. This value must always be a hardcoded string and not a value coming from user input.
string$keyforselectUse another value than the translation key for the where into select
int$filteronentityUse a filter on entity
Returns
string Label in UTF8 (but without entities)
See also
dol_getIdFromCode()

Definition at line 976 of file translate.class.php.

◆ getLabelFromNumber()

Translate::getLabelFromNumber (   $number,
  $isamount = '' 
)

Return full text translated to language label for a key.

Store key-label in a cache. This function need module "numberwords" to be installed. If not it will return same number (this module is not provided by default as it use non GPL source code).

Parameters
int | string$numberNumber to encode in full text
string$isamount''=it's just a number, '1'=It's an amount (default currency), 'currencycode'=It's an amount (foreign currency)
Returns
string Label translated in UTF8 (but without entities) 10 if setDefaultLang was en_US => ten 123 if setDefaultLang was fr_FR => cent vingt trois

Definition at line 931 of file translate.class.php.

◆ getTradFromKey()

Translate::getTradFromKey (   $key)
private

Return translated value of key for special keys ("Currency...", "Civility...", ...).

Search in lang file, then into database. Key must be any complete entry into lang file: CurrencyEUR, ... If not found, return key. The string return is not formated (translated with transnoentitiesnoconv). NOTE: To avoid infinite loop (getLabelFromKey->transnoentities->getTradFromKey->getLabelFromKey), if you modify this function, check that getLabelFromKey is never called with the same value than $key.

Parameters
string$keyKey to translate
Returns
string Translated string (translated with transnoentitiesnoconv)

Definition at line 579 of file translate.class.php.

◆ isLoaded()

Translate::isLoaded (   $domain)

Get information with result of loading data for domain.

Parameters
string$domainDomain to check
Returns
int 0, 1, 2...

Definition at line 563 of file translate.class.php.

◆ load()

Translate::load (   $domain,
  $alt = 0,
  $stopafterdirection = 0,
  $forcelangdir = '',
  $loadfromfileonly = 0,
  $forceloadifalreadynotfound = 0 
)

Load translation key-value for a particular file, into a memory array.

If data for file already loaded, do nothing. All data in translation array are stored in UTF-8 format. tab_loaded is completed with $domain key. rule "we keep first entry found with we keep last entry found" so it is probably not what you want to do.

Value for hash are: 1:Loaded from disk, 2:Not found, 3:Loaded from cache

Parameters
string$domainFile name to load (.lang file). Must be "file" or "file@module" for module language files: If $domain is "file@module" instead of "file" then we look for module lang file in htdocs/custom/modules/mymodule/langs/code_CODE/file.lang then in htdocs/module/langs/code_CODE/file.lang instead of htdocs/langs/code_CODE/file.lang
integer$alt0 (try xx_ZZ then 1), 1 (try xx_XX then 2), 2 (try en_US)
int$stopafterdirectionStop when the DIRECTION tag is found (optimize speed)
int$forcelangdirTo force a different lang directory
int$loadfromfileonly1=Do not load overwritten translation from file or old conf.
int$forceloadifalreadynotfoundForce attempt to reload lang file if it was previously not found
Returns
int <0 if KO, 0 if already loaded or loading not required, >0 if OK
See also
loadLangs()

Read each lines until a '=' (with any combination of spaces around it) and split the rest until a line feed. This is more efficient than fgets + explode + trim by a factor of ~2.

Definition at line 206 of file translate.class.php.

◆ loadCacheCurrencies()

Translate::loadCacheCurrencies (   $currency_code)

Load into the cache this->cache_currencies, all currencies.

Parameters
string$currency_codeGet only currency. Get all if ''.
Returns
int Nb of loaded lines, 0 if already loaded, <0 if KO

Definition at line 1078 of file translate.class.php.

Referenced by getCurrencySymbol().

◆ loadFromDatabase()

Translate::loadFromDatabase (   $db)

Load translation key-value from database into a memory array.

If data already loaded, do nothing. All data in translation array are stored in UTF-8 format. tab_loaded is completed with $domain key. rule "we keep first entry found with we keep last entry found" so it is probably not what you want to do.

Value for hash are: 1:Loaded from disk, 2:Not found, 3:Loaded from cache

Parameters
DoliDB$dbDatabase handler
Returns
int <0 if KO, 0 if already loaded or loading not required, >0 if OK

Definition at line 436 of file translate.class.php.

◆ loadLangs()

Translate::loadLangs (   $domains)

Load translation files.

Parameters
array$domainsArray of lang files to load
Returns
int <0 if KO, 0 if already loaded or loading not required, >0 if OK

Definition at line 171 of file translate.class.php.

◆ setDefaultLang()

Translate::setDefaultLang (   $srclang = 'en_US')

Set accessor for this->defaultlang.

Parameters
string$srclangLanguage to use. If '' or 'auto', we use browser lang.
Returns
void

Definition at line 74 of file translate.class.php.

◆ trans()

Translate::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 for this text, we look in alternative file and if still not found, it is returned as it is.

The parameters of this method should not contain HTML tags. If there is, they will be htmlencoded to have no effect.

Parameters
string$keyKey to translate
string$param1param1 string
string$param2param2 string
string$param3param3 string
string$param4param4 string
int$maxsizeMax length of text. Warning: Will not work if paramX has HTML content. deprecated.
Returns
string Translated string (encoded into HTML entities and UTF8)

Definition at line 628 of file translate.class.php.

Referenced by transcountry().

◆ transcountry()

Translate::transcountry (   $str,
  $countrycode 
)

Return translation of a key depending on country.

Parameters
string$strstring root to translate
string$countrycodecountry code (FR, ...)
Returns
string translated string
See also
transcountrynoentities(), picto_from_langcode()

Definition at line 758 of file translate.class.php.

References trans().

◆ transcountrynoentities()

Translate::transcountrynoentities (   $str,
  $countrycode 
)

Retourne la version traduite du texte passe en parametre complete du code pays.

Parameters
string$strstring root to translate
string$countrycodecountry code (FR, ...)
Returns
string translated string
See also
transcountry(), picto_from_langcode()

Definition at line 776 of file translate.class.php.

References transnoentities().

◆ transnoentities()

Translate::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 file and if still not found it is returned as is.

Parameters of this method must not contain any HTML tags.

Parameters
string$keyKey to translate
string$param1chaine de param1
string$param2chaine de param2
string$param3chaine de param3
string$param4chaine de param4
string$param5chaine de param5
Returns
string Translated string (encoded into UTF8)

Definition at line 697 of file translate.class.php.

Referenced by transcountrynoentities().

◆ transnoentitiesnoconv()

Translate::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 file and if still not found, it is returned as is.

No conversion to encoding charset of lang object is done. Parameters of this method must not contains any HTML tags.

Parameters
string$keyKey to translate
string$param1chaine de param1
string$param2chaine de param2
string$param3chaine de param3
string$param4chaine de param4
string$param5chaine de param5
Returns
string Translated string

Definition at line 718 of file translate.class.php.


The documentation for this class was generated from the following file: