class Translate (View source)

Class to manage translations

Properties

string[] $dir
string $defaultlang
string $shortlang
string $charset_output
array<string, string> $tab_translate
array<string, array<string, string>> $cache_labels
array<string, array{label: string, unicode: string}> $cache_currencies
string $origlang
$error
string[] $errors

Methods

__construct($dir, Conf $conf)

Constructor

void
setDefaultLang($srclang = 'en_US')

Set accessor for this->defaultlang

string
getDefaultLang($mode = 0)

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

int
loadLangs($domains)

Load translation files.

int
load(string $domain, $alt = 0, $stopafterdirection = 0, $forcelangdir = '', $loadfromfileonly = 0, $forceloadifalreadynotfound = 0, $tabtranslatedomain = [], $langkey = '')

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

int
loadFromDatabase(DoliDB $db)

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

int
isLoaded(string $domain)

Get information with result of loading data for domain

trans(string $key, $param1 = '', $param2 = '', $param3 = '', $param4 = '', int $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.

transnoentities(string $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.

tr(string $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.

transnoentitiesnoconv(string $key, $param1 = '', $param2 = '', $param3 = '', $param4 = '', $param5 = '')

Return translated value of a text string. Alias of tr() for backward compatibility.

transcountry($str, string $countrycode)

Return translation of a key depending on country

transcountrynoentities($str, string $countrycode)

Retourne la version traduite du texte passe en parameter complete du code pays

convToOutputCharset($str, string $pagecodefrom = 'UTF-8', $pagecodeto = '')

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

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

Return list of all available languages

file_exists($filename, int $searchalt = 0)

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

getLabelFromNumber(int|string $number, $isamount = '')

Return full text translated to language label for a key. Store key-label in a cache.

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

Return a label for a key.

getCurrencyAmount($currency_code, string $amount)

Return a currency code into its symbol

getCurrencySymbol(string $currency_code = '', $forceloadall = 0)

Return a currency code into its symbol.

loadCacheCurrencies(string $currency_code)

Load into the cache this->cache_currencies, all currencies

array<string, string>
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)

Details

__construct($dir, Conf $conf)

Constructor

@param string $dir Force directory that contains /langs subdirectory (value is sometimes '..' like into install/ pages or support/ pages). Use '' by default.

Parameters

$dir
Conf $conf

Object with Dolibarr configuration

void setDefaultLang($srclang = 'en_US')

Set accessor for this->defaultlang

@param string $srclang Language to use. If '' or 'auto', we use browser lang.

Parameters

$srclang

Return Value

void

string getDefaultLang($mode = 0)

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

@param int $mode 0=Long language code, 1=Short language code (en, fr, es, ...)

Parameters

$mode

Return Value

string

Language code used (en_US, en_AU, fr_FR, ...)

int loadLangs($domains)

Load translation files.

@param string[] $domains Array of lang files to load

Parameters

$domains

Return Value

int

Return integer <0 if KO, 0 if already loaded or loading not required, >0 if OK

int load(string $domain, $alt = 0, $stopafterdirection = 0, $forcelangdir = '', $loadfromfileonly = 0, $forceloadifalreadynotfound = 0, $tabtranslatedomain = [], $langkey = '')

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 $domain

File 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 @param int $alt 0 (try xx_ZZ then 1), 1 (try xx_XX then 2), 2 (try en_US) @param int $stopafterdirection Stop when the DIRECTION tag is found (optimize speed) @param string $forcelangdir To force a different lang directory @param int $loadfromfileonly 1=Do not load overwritten translation from file or old conf. @param int $forceloadifalreadynotfound Force attempt to reload lang file if it was previously not found @param array<string,string> $tabtranslatedomain Store translations to be stored in cache @param string $langkey To create key for cachekey in recursivity

$alt
$stopafterdirection
$forcelangdir
$loadfromfileonly
$forceloadifalreadynotfound
$tabtranslatedomain
$langkey

Return Value

int

Return integer <0 if KO, 0 if already loaded or loading not required, >0 if OK loadLangs()

int loadFromDatabase(DoliDB $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 $db

Database handler

Return Value

int

Return integer <0 if KO, 0 if already loaded or loading not required, >0 if OK

int isLoaded(string $domain)

Get information with result of loading data for domain

Parameters

string $domain

Domain to check

Return Value

int

0, 1, 2...

trans(string $key, $param1 = '', $param2 = '', $param3 = '', $param4 = '', int $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 $key

Key to translate @param string|float|int $param1 param1 string @param string|float|int $param2 param2 string @param string|float|int $param3 param3 string @param string|float|int $param4 param4 string

$param1
$param2
$param3
$param4
int $maxsize

Max length of text. Warning: Will not work if paramX has HTML content. deprecated. @return string Translated string (encoded into HTML entities and UTF8)

transnoentities(string $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 $key

Key to translate @param string $param1 chaine de param1 @param string $param2 chaine de param2 @param string $param3 chaine de param3 @param string $param4 chaine de param4 @param string $param5 chaine de param5 @return string Translated string (encoded into UTF8)

$param1
$param2
$param3
$param4
$param5

tr(string $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 $key

Key to translate @param string $param1 chaine de param1 @param string $param2 chaine de param2 @param string $param3 chaine de param3 @param string $param4 chaine de param4 @param string $param5 chaine de param5 @return string Translated string

$param1
$param2
$param3
$param4
$param5

transnoentitiesnoconv(string $key, $param1 = '', $param2 = '', $param3 = '', $param4 = '', $param5 = '')

Return translated value of a text string. Alias of tr() for backward compatibility.

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 $key

Key to translate @param string $param1 chaine de param1 @param string $param2 chaine de param2 @param string $param3 chaine de param3 @param string $param4 chaine de param4 @param string $param5 chaine de param5 @return string Translated string

$param1
$param2
$param3
$param4
$param5

transcountry($str, string $countrycode)

Return translation of a key depending on country

@param string $str String root to translate. Example 'TotalHT', 'AmountLT1', 'ProfId1', 'ProfId2', 'LocalTax1IsUsedExample', ...

Parameters

$str
string $countrycode

Country code (FR, ...) @return string Translated string transcountrynoentities(), picto_from_langcode()

transcountrynoentities($str, string $countrycode)

Retourne la version traduite du texte passe en parameter complete du code pays

@param string $str string root to translate

Parameters

$str
string $countrycode

country code (FR, ...) @return string translated string transcountry(), picto_from_langcode()

convToOutputCharset($str, string $pagecodefrom = 'UTF-8', $pagecodeto = '')

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

@param string $str String to convert

Parameters

$str
string $pagecodefrom

Page code of src string @param string $pagecodeto Expected page code of returned string @return string Converted string

$pagecodeto

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

Return list of all available languages

@param  string      $langdir        Directory to scan

Parameters

$langdir
int $maxlength

Max length for each value in combo box (will be truncated) @param int<0,1> $usecode 1=Show code instead of country name for language variant, 2=Show only code @param int<0,1> $mainlangonly 1=Show only main languages ('fr_FR' no' fr_BE', 'es_ES' not 'es_MX', ...) @return array<string,string> List of languages

$usecode
$mainlangonly

file_exists($filename, int $searchalt = 0)

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

@param string $filename Language filename to search

Parameters

$filename
int $searchalt

Search also alternate language file @return bool true if exists and readable

getLabelFromNumber(int|string $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 $number

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

$isamount

getLabelFromKey(DoliDB $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 $db

Database handler @param string $key Translation key to get label (key in language file) @param string $tablename Table name without prefix. This value must always be a hardcoded string and not a value coming from user input. @param string $fieldkey Field for key. This value must always be a hardcoded string and not a value coming from user input. @param string $fieldlabel Field for label. This value must always be a hardcoded string and not a value coming from user input. @param string $keyforselect Use another value than the translation key for the where into select @param int $filteronentity Use a filter on entity @return string|int Label in UTF8 (not HTML entity encoded) or -1 if error dol_getIdFromCode()

$key
$tablename
$fieldkey
$fieldlabel
$keyforselect
$filteronentity

getCurrencyAmount($currency_code, string $amount)

Return a currency code into its symbol

@param string $currency_code Currency Code

Parameters

$currency_code
string $amount

If not '', show currency + amount according to langs ($10, 10€). @return string Amount + Currency symbol encoded into UTF8 @deprecated Use method price to output a price price()

getCurrencySymbol(string $currency_code = '', $forceloadall = 0)

Return a currency code into its symbol.

If mb_convert_encoding is not available, return currency code.

Parameters

string $currency_code

Currency code @param integer $forceloadall 1=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. @return string Currency symbol encoded into UTF8

$forceloadall

loadCacheCurrencies(string $currency_code)

Load into the cache this->cache_currencies, all currencies

Parameters

string $currency_code

Get only currency. Get all if ''. @return int Nb of loaded lines, 0 if already loaded, <0 if KO

array<string, string> 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)

Return Value

array<string, string>

Array of translation keys lang_key => string_translation_loaded