PHP Class Elgg\I18n\Translator

Since: 1.10.0
Mostrar archivo Open project: elgg/elgg Class Usage Examples

Public Methods

Method Description
__construct ( ) Initializes new translator
addTranslation ( string $country_code, array $language_array ) : boolean Add a translation.
detectLanguage ( ) : string Detect the current system/user language or false.
getAllLanguageCodes ( ) : array Returns an array of language codes.
getCurrentLanguage ( ) : string Get the current system/user language or "en".
getInstalledTranslations ( ) : array Return an array of installed translations as an associative array "two letter code" => "native language name".
getLanguageCompleteness ( string $language ) : integer Return the level of completeness for a given language code (compared to english)
getMissingLanguageKeys ( string $language ) : mixed Return the translation keys missing from a given language, or those that are identical to the english version.
languageKeyExists ( string $key, string $language = 'en' ) : boolean Check if a given language key exists
loadTranslations ( string $language = null ) Load both core and plugin translations
normalizeLanguageCode ( string $code ) : string Normalize a language code (e.g. from Transifex)
registerPluginTranslations ( string $path ) : boolean Registers translations in a directory assuming the standard plugin layout.
registerTranslations ( string $path, boolean $load_all = false, string $language = null ) : boolean When given a full path, finds translation files and loads them
reloadAllTranslations ( ) : void Reload all translations from all registered paths.
translate ( string $message_key, array $args = [], string $language = "" ) : string Given a message key, returns an appropriately translated full-text string

Private Methods

Method Description
ensureTranslationsLoaded ( string $language ) : void Make sure translations are loaded
loadPluginTranslations ( string $language ) : void Load plugin translations for a language

Method Details

__construct() public method

Initializes new translator
public __construct ( )

addTranslation() public method

Translations are arrays in the Zend Translation array format, eg: $english = array('message1' => 'message1', 'message2' => 'message2'); $german = array('message1' => 'Nachricht1','message2' => 'Nachricht2');
public addTranslation ( string $country_code, array $language_array ) : boolean
$country_code string Standard country code (eg 'en', 'nl', 'es')
$language_array array Formatted array of strings
return boolean Depending on success

detectLanguage() public method

Detect the current system/user language or false.
public detectLanguage ( ) : string
return string The language code (eg "en") or false if not set

getAllLanguageCodes() public static method

Returns an array of language codes.
public static getAllLanguageCodes ( ) : array
return array

getCurrentLanguage() public method

Get the current system/user language or "en".
public getCurrentLanguage ( ) : string
return string The language code for the site/user or "en" if not set

getInstalledTranslations() public method

Return an array of installed translations as an associative array "two letter code" => "native language name".
public getInstalledTranslations ( ) : array
return array

getLanguageCompleteness() public method

Return the level of completeness for a given language code (compared to english)
public getLanguageCompleteness ( string $language ) : integer
$language string Language
return integer

getMissingLanguageKeys() public method

Return the translation keys missing from a given language, or those that are identical to the english version.
public getMissingLanguageKeys ( string $language ) : mixed
$language string The language
return mixed

languageKeyExists() public method

Check if a given language key exists
Since: 1.11
public languageKeyExists ( string $key, string $language = 'en' ) : boolean
$key string The translation key
$language string The specific language to check
return boolean

loadTranslations() public method

By default this loads only English and the language of the logged in user. The optional $language argument can be used to load translations on-demand in case we need to translate something to a language not loaded by default for the current request.
public loadTranslations ( string $language = null )
$language string Language code

normalizeLanguageCode() public static method

Normalize a language code (e.g. from Transifex)
public static normalizeLanguageCode ( string $code ) : string
$code string Language code
return string

registerPluginTranslations() public method

Registers translations in a directory assuming the standard plugin layout.
public registerPluginTranslations ( string $path ) : boolean
$path string Without the trailing slash.
return boolean Success

registerTranslations() public method

When given a full path, finds translation files and loads them
public registerTranslations ( string $path, boolean $load_all = false, string $language = null ) : boolean
$path string Full path
$load_all boolean If true all languages are loaded, if false only the current language + en are loaded
$language string Language code
return boolean success

reloadAllTranslations() public method

This is only called by functions which need to know all possible translations.
public reloadAllTranslations ( ) : void
return void

translate() public method

Given a message key, returns an appropriately translated full-text string
public translate ( string $message_key, array $args = [], string $language = "" ) : string
$message_key string The short message code
$args array An array of arguments to pass through vsprintf().
$language string Optionally, the standard language code (defaults to site/user default, then English)
return string Either the translated string, the English string, or the original language string.