Class: LanguageManager

Class for working with localization of texts.

Implements

Authors

Oleg Schildt

Package

Source code

 

Properties
protected

Internal variable for storing the paths of the additional localization files.

static protected

Internal variable for storing the current context.

protected

Internal variable for storing the cookie path.

static protected

Internal array for storing the list of country name translations.

static protected

Internal variable for storing the current language.

protected

Internal variable for storing the state whether the dictionary is loaded or not.

static protected

Internal array for storing the list of language name translations.

protected

Internal variable for storing the path with the localization files.

static protected

Internal array for storing the list of supported languages.

static protected

Internal array for storing the list of text translations.

protected

Internal variable for storing the state whether the APCU should be used.

protected

Internal variable for storing the state whether the last selected language can be stored to cookie.

protected

Internal variable for storing the fallback language.

protected

Internal variable for storing the state whether the E_USER_NOTICE is triggered in the case of missing translations.

Methods
public

Adds additional localization files to the dictionary.

public

This function should detect the current language based on cookies, browser languages etc.

public

Adds additional translations to the dictionary.

public

Returns the current context.

public

Tries to find the country code by the given name.

public

Provides the list of countries for the given language in the form "code" => "translation".

public

Provides the text translation for the country name by the code for the given language.

public

Returns the current language.

public

Returns the fallback language. If set and a translation is missing on a language, the translation on this language will be used.

public

Tries to find the language code by the given name.

public

Provides the list of languages for the given language in the form "code" => "translation".

public

Provides the text translation for the language name by the code for the given language.

public

Returns the list of supported languages.

public

Checks whether the text translation for the text ID for the given language exists.

public

Initializes the language manager with parameters.

public

This is function for loading the translations from the source JSON file.

public

Sets the current context.

public

Sets the current language.

public

Provides the text translation for the text ID for the given language.

public

Provides the text translation for the text ID for the given language if the translation exists.

public

Checks whether the country code is valid (has translation).

public

Checks whether the language code is valid (has translation).


Property: LanguageManager::$additional_localization_files

protected string|array $additional_localization_files = [];

Internal variable for storing the paths of the additional localization files.

Type

string|array

Authors

Oleg Schildt

Source code


Property: LanguageManager::$context

protected static string $context = "default";

Internal variable for storing the current context.

Type

string

Authors

Oleg Schildt

Source code

See also


Property: LanguageManager::$cookie_path

protected string $cookie_path = "/";

Internal variable for storing the cookie path.

Type

string

Authors

Oleg Schildt

Source code


Property: LanguageManager::$countries

protected static array $countries = [];

Internal array for storing the list of country name translations.

Type

array

Authors

Oleg Schildt

Source code


Property: LanguageManager::$current_language

protected static array $current_language = [];

Internal variable for storing the current language.

Type

array

Authors

Oleg Schildt

Source code


Property: LanguageManager::$dictionary_loaded

protected bool $dictionary_loaded = false;

Internal variable for storing the state whether the dictionary is loaded or not.

Type

bool

Authors

Oleg Schildt

Source code


Property: LanguageManager::$languages

protected static array $languages = [];

Internal array for storing the list of language name translations.

Type

array

Authors

Oleg Schildt

Source code


Property: LanguageManager::$localization_path

protected string $localization_path = "";

Internal variable for storing the path with the localization files.

Type

string

Authors

Oleg Schildt

Source code


Property: LanguageManager::$supported_languages

protected static array $supported_languages = [];

Internal array for storing the list of supported languages.

Type

array

Authors

Oleg Schildt

Source code


Property: LanguageManager::$texts

protected static array $texts = [];

Internal array for storing the list of text translations.

Type

array

Authors

Oleg Schildt

Source code


Property: LanguageManager::$use_apcu

protected bool $use_apcu = false;

Internal variable for storing the state whether the APCU should be used.

Type

bool

Authors

Oleg Schildt

Source code

Property: LanguageManager::$use_cookie

protected bool $use_cookie = false;

Internal variable for storing the state whether the last selected language can be stored to cookie.

Type

bool

Authors

Oleg Schildt

Source code


Property: LanguageManager::$use_fallback_language

protected string $use_fallback_language = "";

Internal variable for storing the fallback language.

Details

If set and a translation is missing on a language, the translation on this language will be used.

Type

string

Authors

Oleg Schildt

Source code


Property: LanguageManager::$warn_missing

protected bool $warn_missing = true;

Internal variable for storing the state whether the E_USER_NOTICE is triggered in the case of missing translations.

Type

bool

Authors

Oleg Schildt

Source code


Method: LanguageManager::addLocalizationFile()

public function addLocalizationFile(string $localization_file): void;

Adds additional localization files to the dictionary.

Parameters

Name Pass type Value type Default value Description
$localization_file by value string

The path of the additional localization file.

Returns

void

Throws

\Exception

It might throw an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code


Method: LanguageManager::detectLanguage()

public function detectLanguage(): void;

This function should detect the current language based on cookies, browser languages etc.

Details

Priority:

  1. explicitly set by the request parameter language.
  2. header 'Content-Language'.
  3. last language in the cookie.
  4. browser default language.
  5. the first one from the supported list.
  6. English.

Some applications may consist of two parts - administration console and public site. A usual example is a CMS system.

For example, you are using administration console in English and editing the public site for German and French. When you open the public site for preview in German or French, you want it to be open in the corresponding language, but the administration console should remain in English.

With the help of $context, you are able to maintain different languages for different parts of your application. If you do not need the $context, just do not specify it.

Returns

void

Overrides

Authors

Oleg Schildt

Source code


Method: LanguageManager::extendDictionary()

public function extendDictionary(array $dictionary): void;

Adds additional translations to the dictionary.

Parameters

Name Pass type Value type Default value Description
$dictionary by value array

The array with additional translations.

Returns

void

Throws

\Exception

It might throw an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code


Method: LanguageManager::getContext()

public function getContext(): string;

Returns the current context.

Details

Some applications may consist of two parts - administration console and public site. A usual example is a CMS system.

For example, you are using administration console in English and editing the public site for German and French. When you open the public site for preview in German or French, you want it to be open in the corresponding language, but the administration console should remain in English.

With the help of $context, you are able to maintain different languages for different parts of your application. If you do not need the $context, just do not specify it.

Returns

string

Returns the current context.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: LanguageManager::getCountryCode()

public function getCountryCode(string $country_name): string;

Tries to find the country code by the given name.

Parameters

Name Pass type Value type Default value Description
$country_name by value string

The name of the country in any supported language.

Returns

string

Returns the country code if it could be found, otherwise an empty string.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: LanguageManager::getCountryList()

public function getCountryList(array &$country_list, string $lng = "", array $display_first = []): bool;

Provides the list of countries for the given language in the form "code" => "translation".

Parameters

Name Pass type Value type Default value Description
$country_list by reference array

Target array where the country list should be loaded.

$lng by value string ""

The language. If it is not specified, the default language is used.

$display_first by value array []

List of the country codes to be displayed first in the order, they appear in the list.

Returns

bool

Returns true if the country list is successfully retrieved, otherwise false.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: LanguageManager::getCountryName()

public function getCountryName(string $code, string $lng = ""): string;

Provides the text translation for the country name by the code for the given language.

Parameters

Name Pass type Value type Default value Description
$code by value string

Country ISO code (uppercase, e.g. US, DE, FR).

$lng by value string ""

The language. If it is not specified, the default language is used.

Returns

string

Returns the translation text for the country name or the $code if no translation is found.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: LanguageManager::getCurrentLanguage()

public function getCurrentLanguage(): string;

Returns the current language.

Returns

string

Returns the current language ISO code.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: LanguageManager::getFallbackLanguage()

public function getFallbackLanguage(): string;

Returns the fallback language. If set and a translation is missing on a language, the translation on this language will be used.

Returns

string

Returns the fallback language.

Overrides

Authors

Oleg Schildt

Source code


Method: LanguageManager::getLanguageCode()

public function getLanguageCode(string $lang_name): string;

Tries to find the language code by the given name.

Parameters

Name Pass type Value type Default value Description
$lang_name by value string

The name of the language in any supported language.

Returns

string

Returns the language code if it could be found, otherwise an empty string.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: LanguageManager::getLanguageList()

public function getLanguageList(array &$language_list, string $lng = "", array $display_first = []): bool;

Provides the list of languages for the given language in the form "code" => "translation".

Parameters

Name Pass type Value type Default value Description
$language_list by reference array

Target array where the language list should be loaded.

$lng by value string ""

The language. If it is not specified, the default language is used.

$display_first by value array []

List of the language codes to be displayed first in the order, they appear in the list.

Returns

bool

Returns true if the language list is successfully retrieved, otherwise false.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: LanguageManager::getLanguageName()

public function getLanguageName(string $code, string $lng = ""): string;

Provides the text translation for the language name by the code for the given language.

Parameters

Name Pass type Value type Default value Description
$code by value string

Language ISO code (lowercase, e.g. en, de, fr).

$lng by value string ""

The language. If it is not specified, the default language is used.

Returns

string

Returns the translation text for the language name or the $code if no translation is found.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: LanguageManager::getSupportedLanguages()

public function getSupportedLanguages(): array;

Returns the list of supported languages.

Returns

array

Returns the list of supported languages.

Overrides

Authors

Oleg Schildt

Source code


Method: LanguageManager::hasTranslation()

public function hasTranslation(string $text_id, string $lng = ""): bool;

Checks whether the text translation for the text ID for the given language exists.

Parameters

Name Pass type Value type Default value Description
$text_id by value string

Text ID

$lng by value string ""

The language. If it is not specified, the default language is used.

Returns

bool

Returns true if the translation exists, otherwise false.

Overrides

Authors

Oleg Schildt

Source code


Method: LanguageManager::init()

public function init(array $parameters): void;

Initializes the language manager with parameters.

Parameters

Name Pass type Value type Default value Description
$parameters by value array

Settings for logging as an associative array in the form key => value:

  • $parameters["localization_path"] - the path where the localization files are stored.

  • $parameters["use_cookie"] - if set to true, the last selected language is stored to cookie.

  • $parameters["use_fallback_language"] - if set and a translation is missing on a language, the translation on this language will be used.

  • $parameters["cookie_path"] - Cookie path.

  • $parameters["use_apcu"] - if installed, apcu can be used to cache the translations in the memory.

  • $parameters["warn_missing"] - If it is set to true, the E_USER_NOTICE is triggered in the case of missing translations.

Returns

void

Throws

\Exception

It might throw an exception in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code


Method: LanguageManager::loadDictionary()

public function loadDictionary(): void;

This is function for loading the translations from the source JSON file.

Returns

void

Throws

\Exception

It might throw the following exceptions in the case of any errors:

  • if the translation file is invalid.

Overrides

Authors

Oleg Schildt

Source code


Method: LanguageManager::setContext()

public function setContext(string $context): void;

Sets the current context.

Details

Some applications may consist of two parts - administration console and public site. A usual example is a CMS system.

For example, you are using administration console in English and editing the public site for German and French. When you open the public site for preview in German or French, you want it to be open in the corresponding language, but the administration console should remain in English.

With the help of $context, you are able to maintain different languages for different parts of your application. If you do not need the $context, just do not specify it.

Parameters

Name Pass type Value type Default value Description
$context by value string

The name of the context.

Returns

void

Overrides

Authors

Oleg Schildt

Source code

See also


Method: LanguageManager::setCurrentLanguage()

public function setCurrentLanguage(string $language): void;

Sets the current language.

Parameters

Name Pass type Value type Default value Description
$language by value string

The language ISO code to be set.

Returns

void

Overrides

Authors

Oleg Schildt

Source code

See also


Method: LanguageManager::text()

public function text(string $text_id, string $lng = "", string $default_text = ""): string;

Provides the text translation for the text ID for the given language.

Parameters

Name Pass type Value type Default value Description
$text_id by value string

Text ID

$lng by value string ""

The language. If it is not specified, the default language is used.

$default_text by value string ""

The default text to be used if there is no translation.

Returns

string

Returns the translation text or the $default_text/$text_id if no translation is found.

Throws

\Exception

It might throw exceptions in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code


Method: LanguageManager::try_text()

public function try_text(string $text_id, string $lng = ""): string;

Provides the text translation for the text ID for the given language if the translation exists.

Details

Otherwise, it returns the text ID and emits no warning.

Parameters

Name Pass type Value type Default value Description
$text_id by value string

Text ID

$lng by value string ""

The language. If it is not specified, the default language is used.

Returns

string

Returns the translation text or the $default_text/$text_id if no translation is found.

Throws

\Exception

It might throw exceptions in the case of any errors.

Overrides

Authors

Oleg Schildt

Source code


Method: LanguageManager::validateCountryCode()

public function validateCountryCode(string $code, string $lng = ""): bool;

Checks whether the country code is valid (has translation).

Parameters

Name Pass type Value type Default value Description
$code by value string

Country ISO code (uppercase, e.g. US, DE, FR).

$lng by value string ""

The language. If it is not specified, the default language is used.

Returns

bool

Returns true if the country code is valid (has translation), otherwise false.

Overrides

Authors

Oleg Schildt

Source code

See also


Method: LanguageManager::validateLanguageCode()

public function validateLanguageCode(string $code, string $lng = ""): bool;

Checks whether the language code is valid (has translation).

Parameters

Name Pass type Value type Default value Description
$code by value string

Language ISO code (lowercase, e.g. en, de, fr).

$lng by value string ""

The language. If it is not specified, the default language is used.

Returns

bool

Returns true if the language code is valid (has translation), otherwise false.

Overrides

Authors

Oleg Schildt

Source code

See also