class Localization (View source)

Constants

BASE_LOCALE

The "base" locale identifier.

CONTEXT_SYSTEM

The context (resolving to en_US) to be considered as the "neutral" one.

This context must be used for all strings that are system related in concrete5 and should have their own context. Generally, these are the strings that concrete5 saves in the database, such as package, theme and block type names/descriptions.

CONTEXT_SITE

This is the context for the site interface tranlations.

It contains the page locale, determined by the page-specific locale or by the site section it's contained in. When there's no locale associated to the current page, this context is associated to the locale specified in the concrete.locale configuration option.

These are all the translations that the site visitors see on the site. The editor also sees these strings in the same language as the visitor.

CONTEXT_UI

The context containing the locale of the current user (fallsback to the concrete.locale configuration option).

This should be the context used when showing the edit dialogs, the concrete5 menus...

Properties

protected TranslatorAdapterRepositoryInterface $translatorAdapterRepository

The translator adapter repository to be used.

protected array $contextLocales

The locale identifier to be used for every translation context.

protected string|null $activeContext

The currently active translation context.

protected array $activeContextQueue

Tracks the list of active contexts.

Methods

getTranslatorAdapterRepository()

Gets the translator adapter repository.

setTranslatorAdapterRepository(TranslatorAdapterRepositoryInterface $repository)

Sets the translator adapter repository.

string
getActiveContext()

Returns the currently active translation context.

setActiveContext(string $context)

Sets the active translation context.

pushActiveContext(string $newContext)

Change the active translation context, but remember the previous one.

popActiveContext()

Restore the context that was active before calling pushActiveContext.

getTranslatorAdapter($context)

Gets the translator adapter object for the given context from the translator adapter repository.

getActiveTranslatorAdapter()

Gets the translator adapter for the active context.

setContextLocale(string $context, string $locale)

Sets the context locale for the given context as the given locale.

string|null
getContextLocale($context)

Gets the context locale for the given context.

setLocale(string $locale)

Sets the locale for the active context.

string
getLocale()

Gets the locale for the active context.

removeLoadedTranslatorAdapters()

Removes all the loaded translator adapters from the translator adapter repository.

Translator|null
getActiveTranslateObject() deprecated

Gets the translator object for the active context.

static Localization
getInstance()

Gets a singleton instance of this class.

static Translator
getTranslate() deprecated

Gets the translator object for the active context from from the singleton instance of this class.

static 
changeLocale(string $locale)

Sets the locale for the active context for the singleton instance of this class.

static string
activeLocale()

Returns the currently active locale for the currently active context from the singleton instance of this class.

static string
activeLanguage()

Returns the language for the currently active locale for the currently active context from the singleton instance of this class.

static array
getAvailableInterfaceLanguages()

Gets a list of the available site interface languages. Returns an array that where each item is a locale in format xx_XX.

static array
getAvailableInterfaceLanguageDescriptions(string|null $displayLocale = '')

Generates a list of all available languages and returns an array like [ "de_DE" => "Deutsch (Deutschland)", "en_US" => "English (United States)", "fr_FR" => "Francais (France)"] The result will be sorted by the key.

static string
getLanguageDescription(string $locale, string|null $displayLocale = '')

Get the description of a locale consisting of language and region description e.g. "French (France)".

static 
clearCache()

Clear the translations cache.

static 
setupSiteLocalization(Translator $translate = null) deprecated

Load the site language files (must be done after all packages called their setupPackageLocalization).

currentLocaleChanged(string $locale)

To be called every time the current locale changes.

Details

TranslatorAdapterRepositoryInterface getTranslatorAdapterRepository()

Gets the translator adapter repository.

Return Value

TranslatorAdapterRepositoryInterface

Exceptions

Exception

in case the translator adapter repository has not been set, an exception is thrown

setTranslatorAdapterRepository(TranslatorAdapterRepositoryInterface $repository)

Sets the translator adapter repository.

Parameters

TranslatorAdapterRepositoryInterface $repository

string getActiveContext()

Returns the currently active translation context.

Return Value

string

setActiveContext(string $context)

Sets the active translation context.

Parameters

string $context

pushActiveContext(string $newContext)

Change the active translation context, but remember the previous one.

Useful when temporarily setting the translation context to something else than the original.

Parameters

string $newContext

The new translation context to activate (default contexts are defined by the Localization::CONTEXT_... constants).

See also

Localization::popActiveContext

Examples

```php
$loc = \Localization::getInstance();
// Let's assume the current context is 'original_context'

$loc->pushActiveContext('new_context');

// Do what you want in context 'new_context'

$loc->popActiveContext();
// Now the context is 'original_context'
```

popActiveContext()

Restore the context that was active before calling pushActiveContext.

TranslatorAdapterInterface getTranslatorAdapter($context)

Gets the translator adapter object for the given context from the translator adapter repository.

Parameters

$context

Return Value

TranslatorAdapterInterface

Exceptions

Exception

in case trying to fetch an adapter for an unknown context, an exception is thrown

TranslatorAdapterInterface getActiveTranslatorAdapter()

Gets the translator adapter for the active context.

setContextLocale(string $context, string $locale)

Sets the context locale for the given context as the given locale.

Parameters

string $context
string $locale

string|null getContextLocale($context)

Gets the context locale for the given context.

Parameters

$context

Return Value

string|null

setLocale(string $locale)

Sets the locale for the active context.

Parameters

string $locale

string getLocale()

Gets the locale for the active context.

Return Value

string

removeLoadedTranslatorAdapters()

Removes all the loaded translator adapters from the translator adapter repository.

Translator|null getActiveTranslateObject() deprecated

deprecated Use translator adapters instead

Gets the translator object for the active context.

Return Value

Translator|null

static Localization getInstance()

Gets a singleton instance of this class.

Return Value

Localization

static Translator getTranslate() deprecated

deprecated Use translator adapters instead

Gets the translator object for the active context from from the singleton instance of this class.

Return Value

Translator

static changeLocale(string $locale)

Sets the locale for the active context for the singleton instance of this class.

Parameters

string $locale

static string activeLocale()

Returns the currently active locale for the currently active context from the singleton instance of this class.

Return Value

string

Examples

'en_US'

static string activeLanguage()

Returns the language for the currently active locale for the currently active context from the singleton instance of this class.

Return Value

string

Examples

'en'

static array getAvailableInterfaceLanguages()

Gets a list of the available site interface languages. Returns an array that where each item is a locale in format xx_XX.

Return Value

array

static array getAvailableInterfaceLanguageDescriptions(string|null $displayLocale = '')

Generates a list of all available languages and returns an array like [ "de_DE" => "Deutsch (Deutschland)", "en_US" => "English (United States)", "fr_FR" => "Francais (France)"] The result will be sorted by the key.

If the $displayLocale is set, the language- and region-names will be returned in that language.

Parameters

string|null $displayLocale

Language of the description. Set to null to get each locale name in its own language, set to '' to use the current locale, set to a specific locale to get the names in that language

Return Value

array

An associative Array with locale as the key and description as content

static string getLanguageDescription(string $locale, string|null $displayLocale = '')

Get the description of a locale consisting of language and region description e.g. "French (France)".

Parameters

string $locale

Locale that should be described

string|null $displayLocale

Language of the description. Set to null to get each locale name in its own language, set to '' to use the current locale, set to a specific locale to get the names in that language

Return Value

string

Description of a language

static clearCache()

Clear the translations cache.

static setupSiteLocalization(Translator $translate = null) deprecated

deprecated use \Concrete\Core\Localization\Translator\Adapter\Zend\Translation\Loader\Gettext\SiteTranslationLoader instead

Load the site language files (must be done after all packages called their setupPackageLocalization).

Parameters

Translator $translate

protected currentLocaleChanged(string $locale)

To be called every time the current locale changes.

Parameters

string $locale