Elgg  Version master
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Elgg\I18n\Translator Class Reference

Translator. More...

Inheritance diagram for Elgg\I18n\Translator:
Elgg\I18n\NullTranslator

Public Member Functions

 __construct (protected Config $config, protected LocaleService $locale)
 Constructor. More...
 
 getLoadedTranslations ()
 Get a map of all loaded translations. More...
 
 translate (string $message_key, array $args=[], string $language= '')
 Given a message key, returns an appropriately translated full-text string. More...
 
 addTranslation (string $country_code, array $language_array, bool $ensure_translations_loaded=true)
 Add a translation. More...
 
 getCurrentLanguage ()
 Get the current system/user language or 'en'. More...
 
 setCurrentLanguage (string $language=null)
 Sets current system language. More...
 
 detectLanguage ()
 Detect the current system/user language or false. More...
 
 bootTranslations ()
 Ensures all needed translations are loaded. More...
 
 loadTranslations (string $language)
 Load both core and plugin translations. More...
 
 registerTranslations (string $path, bool $load_all=false, string $language=null)
 When given a full path, finds translation files and loads them. More...
 
 reloadAllTranslations ()
 Reload all translations from all registered paths. More...
 
 getInstalledTranslations (bool $calculate_completeness=false)
 Return an array of installed translations as an associative array "two letter code" => "native language name". More...
 
 getLanguageCompleteness (string $language)
 Return the level of completeness for a given language code (compared to english) More...
 
 getMissingLanguageKeys (string $language)
 Return the translation keys missing from a given language, or those that are identical to the english version. More...
 
 languageKeyExists (string $key, string $language= 'en')
 Check if a given language key exists. More...
 
 getAvailableLanguages ()
 Returns an array of all available language keys. More...
 
 getAllowedLanguages ()
 Returns an array of allowed languages as configured by the site admin. More...
 
 registerLanguagePath (string $path)
 Registers a path for potential translation files. More...
 
 getLanguagePaths ()
 Returns a unique array with locations of translation files. More...
 

Protected Member Functions

 includeLanguageFile (string $path)
 Load cached or include a language file by its path. More...
 
 ensureTranslationsLoaded (string $language)
 Make sure translations are loaded. More...
 

Protected Attributes

array $translations = []
 
string $defaultPath
 
string $current_language = null
 
array $allowed_languages
 
array $language_paths = []
 
bool $was_reloaded = false
 

Detailed Description

Translator.

Use elgg()->translator

Since
1.10.0

Definition at line 16 of file Translator.php.

Constructor & Destructor Documentation

Elgg\I18n\Translator::__construct ( protected Config  $config,
protected LocaleService  $locale 
)

Constructor.

Parameters
Config$configElgg config
LocaleService$localelocale service

Definition at line 52 of file Translator.php.

Member Function Documentation

Elgg\I18n\Translator::addTranslation ( string  $country_code,
array  $language_array,
bool  $ensure_translations_loaded = true 
)

Add a translation.

Translations are arrays in the Zend Translation array format, eg:

$english = array('message1' => 'message1', 'message2' => 'message2'); $german = array('message1' => 'Nachricht1','message2' => 'Nachricht2');

Parameters
string$country_codeStandard country code (eg 'en', 'nl', 'es')
array$language_arrayFormatted array of strings
bool$ensure_translations_loadedEnsures translations are loaded before adding the language array (default: true)
Returns
bool Depending on success

Definition at line 162 of file Translator.php.

Elgg\I18n\Translator::bootTranslations ( )

Ensures all needed translations are loaded.

This loads only English and the language of the logged in user.

Returns
void

Definition at line 269 of file Translator.php.

Elgg\I18n\Translator::detectLanguage ( )

Detect the current system/user language or false.

Returns
string The language code (eg 'en')

Definition at line 219 of file Translator.php.

Elgg\I18n\Translator::ensureTranslationsLoaded ( string  $language)
protected

Make sure translations are loaded.

Parameters
string$languageLanguage
Returns
void

Definition at line 636 of file Translator.php.

Elgg\I18n\Translator::getAllowedLanguages ( )

Returns an array of allowed languages as configured by the site admin.

Returns
string[]
Since
3.3

Definition at line 569 of file Translator.php.

Elgg\I18n\Translator::getAvailableLanguages ( )

Returns an array of all available language keys.

Triggers an event to allow plugins to add/remove languages

Returns
array
Since
3.0

Definition at line 530 of file Translator.php.

Elgg\I18n\Translator::getCurrentLanguage ( )

Get the current system/user language or 'en'.

Returns
string

Definition at line 189 of file Translator.php.

Elgg\I18n\Translator::getInstalledTranslations ( bool  $calculate_completeness = false)

Return an array of installed translations as an associative array "two letter code" => "native language name".

Parameters
boolean$calculate_completenessSet to true if you want a completeness postfix added to the language text
Returns
array

Definition at line 423 of file Translator.php.

Elgg\I18n\Translator::getLanguageCompleteness ( string  $language)

Return the level of completeness for a given language code (compared to english)

Parameters
string$languageLanguage
Returns
float

Definition at line 459 of file Translator.php.

Elgg\I18n\Translator::getLanguagePaths ( )

Returns a unique array with locations of translation files.

Returns
array

Definition at line 625 of file Translator.php.

Elgg\I18n\Translator::getLoadedTranslations ( )

Get a map of all loaded translations.

Returns
array

Definition at line 63 of file Translator.php.

Elgg\I18n\Translator::getMissingLanguageKeys ( string  $language)

Return the translation keys missing from a given language, or those that are identical to the english version.

Parameters
string$languageThe language
Returns
array

Definition at line 486 of file Translator.php.

Elgg\I18n\Translator::includeLanguageFile ( string  $path)
protected

Load cached or include a language file by its path.

Parameters
string$pathPath to file
Returns
bool

Definition at line 377 of file Translator.php.

Elgg\I18n\Translator::languageKeyExists ( string  $key,
string  $language = 'en' 
)

Check if a given language key exists.

Parameters
string$keyThe translation key
string$languageThe specific language to check
Returns
bool
Since
1.11

Definition at line 510 of file Translator.php.

Elgg\I18n\Translator::loadTranslations ( string  $language)

Load both core and plugin translations.

The $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.

Parameters
string$languageLanguage code
Returns
void

Definition at line 290 of file Translator.php.

Elgg\I18n\Translator::registerLanguagePath ( string  $path)

Registers a path for potential translation files.

Parameters
string$pathpath to a folder that contains translation files
Returns
void

Definition at line 606 of file Translator.php.

Elgg\I18n\Translator::registerTranslations ( string  $path,
bool  $load_all = false,
string  $language = null 
)

When given a full path, finds translation files and loads them.

Parameters
string$pathFull path
bool$load_allIf true all languages are loaded, if false only the current language + en are loaded
string$languageLanguage code
Returns
bool success

Definition at line 317 of file Translator.php.

Elgg\I18n\Translator::reloadAllTranslations ( )

Reload all translations from all registered paths.

This is only called by functions which need to know all possible translations.

Returns
void

Definition at line 399 of file Translator.php.

Elgg\I18n\Translator::setCurrentLanguage ( string  $language = null)

Sets current system language.

Parameters
string$languageLanguage code
Returns
void

Definition at line 208 of file Translator.php.

Elgg\I18n\Translator::translate ( string  $message_key,
array  $args = [],
string  $language = '' 
)

Given a message key, returns an appropriately translated full-text string.

Parameters
string$message_keyThe short message code
array$argsAn array of arguments to pass through vsprintf().
string$languageOptionally, the standard language code (defaults to site/user default, then English)
Returns
string Either the translated string, the English string or the original language string.

Definition at line 77 of file Translator.php.

Member Data Documentation

array Elgg\I18n\Translator::$allowed_languages
protected

Definition at line 26 of file Translator.php.

string Elgg\I18n\Translator::$current_language = null
protected

Definition at line 24 of file Translator.php.

string Elgg\I18n\Translator::$defaultPath
protected

Definition at line 22 of file Translator.php.

array Elgg\I18n\Translator::$language_paths = []
protected

Definition at line 42 of file Translator.php.

array Elgg\I18n\Translator::$translations = []
protected

Definition at line 20 of file Translator.php.

bool Elgg\I18n\Translator::$was_reloaded = false
protected

Definition at line 44 of file Translator.php.


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