class LanguageManager implements LanguageManagerInterface (View source)

Class responsible for providing language support on language-unaware sites.

Traits

Provides dependency injection friendly methods for serialization.

Properties

protected array $_serviceIds

An array of service IDs keyed by property name used for serialization.

from  DependencySerializationTrait
protected array $_entityStorages

An array of entity type IDs keyed by the property name of their storages.

from  DependencySerializationTrait
protected LanguageInterface[] $languages

A static cache of translated language lists.

protected LanguageDefault $defaultLanguage

The default language object.

Methods

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

__construct(LanguageDefault $default_language)

Constructs the language manager.

bool
isMultilingual()

Returns whether or not the site has more than one language added.

array
getLanguageTypes()

Returns an array of the available language types.

array
getDefinedLanguageTypesInfo()

Returns information about all defined language types.

getCurrentLanguage(string $type = LanguageInterface::TYPE_INTERFACE)

Returns the current language for the given type.

$this
reset(string|null $type = NULL)

Resets the given language type or all types if none specified.

getDefaultLanguage()

Returns a language object representing the site's default language.

getLanguages(int $flags = LanguageInterface::STATE_CONFIGURABLE)

Returns a list of languages set up on the site.

getNativeLanguages()

Returns a list of languages set up on the site in their native form.

getLanguage(string $langcode)

Returns a language object from the given language code.

string
getLanguageName(string $langcode)

Produced the printed name for a language for display.

getDefaultLockedLanguages(int $weight = 0)

Returns a list of the default locked languages.

bool
isLanguageLocked(string $langcode)

Checks whether a language is locked.

array
getFallbackCandidates(array $context = [])

Returns the language fallback candidates for a given context.

array
getLanguageSwitchLinks(string $type, Url $url)

Returns the language switch links for the given language type.

static array
getStandardLanguageList()

Some common languages with their English and native names.

static array
getUnitedNationsLanguageList()

The 6 official languages used at the United Nations.

$this
setConfigOverrideLanguage(LanguageInterface $language = NULL)

Sets the configuration override language.

getConfigOverrideLanguage()

Gets the current configuration override language.

filterLanguages(array $languages, int $flags = LanguageInterface::STATE_CONFIGURABLE)

Filters the full list of languages based on the value of the flag.

Details

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

__construct(LanguageDefault $default_language)

Constructs the language manager.

Parameters

LanguageDefault $default_language

The default language.

bool isMultilingual()

Returns whether or not the site has more than one language added.

Return Value

bool

TRUE if more than one language is added, FALSE otherwise.

array getLanguageTypes()

Returns an array of the available language types.

Return Value

array

An array of language type machine names.

array getDefinedLanguageTypesInfo()

Returns information about all defined language types.

Defines the three core language types:

  • Interface language is the only configurable language type in core. It is used by t() as the default language if none is specified.
  • Content language is by default non-configurable and inherits the interface language negotiated value. It is used by the Field API to determine the display language for fields if no explicit value is specified.
  • URL language is by default non-configurable and is determined through the URL language negotiation method or the URL fallback language negotiation method if no language can be detected. It is used by l() as the default language if none is specified.

Return Value

array

An associative array of language type information arrays keyed by language type machine name, in the format of hook_language_types_info(). In some implementing classes, this is based on information from hook_language_types_info() and hook_language_types_info_alter().

LanguageInterface getCurrentLanguage(string $type = LanguageInterface::TYPE_INTERFACE)

Returns the current language for the given type.

Parameters

string $type

(optional) The language type; e.g., the interface or the content language. Defaults to \Drupal\Core\Language\LanguageInterface::TYPE_INTERFACE.

Return Value

LanguageInterface

The current language object for the given type of language.

$this reset(string|null $type = NULL)

Resets the given language type or all types if none specified.

Parameters

string|null $type

(optional) The language type to reset as a string, e.g., LanguageInterface::TYPE_INTERFACE, or NULL to reset all language types. Defaults to NULL.

Return Value

$this

The language manager that has been reset.

LanguageInterface getDefaultLanguage()

Returns a language object representing the site's default language.

Return Value

LanguageInterface

A language object.

LanguageInterface[] getLanguages(int $flags = LanguageInterface::STATE_CONFIGURABLE)

Returns a list of languages set up on the site.

Parameters

int $flags

(optional) Specifies the state of the languages that have to be returned. It can be: LanguageInterface::STATE_CONFIGURABLE, LanguageInterface::STATE_LOCKED, or LanguageInterface::STATE_ALL.

Return Value

LanguageInterface[]

An associative array of languages, keyed by the language code.

LanguageInterface[] getNativeLanguages()

Returns a list of languages set up on the site in their native form.

Return Value

LanguageInterface[]

An associative array of languages, keyed by the language code, ordered by weight ascending and name ascending.

LanguageInterface|null getLanguage(string $langcode)

Returns a language object from the given language code.

Parameters

string $langcode

The language code.

Return Value

LanguageInterface|null

A fully-populated language object or NULL.

string getLanguageName(string $langcode)

Produced the printed name for a language for display.

Parameters

string $langcode

The language code.

Return Value

string

The printed name of the language.

LanguageInterface[] getDefaultLockedLanguages(int $weight = 0)

Returns a list of the default locked languages.

Parameters

int $weight

(optional) An integer value that is used as the start value for the weights of the locked languages.

Return Value

LanguageInterface[]

An array of language objects.

bool isLanguageLocked(string $langcode)

Checks whether a language is locked.

Parameters

string $langcode

The language code.

Return Value

bool

Returns whether the language is locked.

array getFallbackCandidates(array $context = [])

Returns the language fallback candidates for a given context.

Parameters

array $context

(optional) An associative array of data that can be useful to determine the fallback sequence. The following keys are used in core:

  • langcode: Language code of the desired language.
  • operation: The name of the operation indicating the context where language fallback is being applied. The following operations are defined in core, but more may be defined in contributed modules:
    • entity_view: Invoked when an entity is about to be displayed. The data key contains the loaded entity.
    • views_query: Invoked when a field based views query is performed. The data key contains a reference to the field object.
    • locale_lookup: Invoked when a string translation was not found. The data key contains the source string.
  • data: A data structure that makes sense in the provided context, see above.

Return Value

array

An array of language codes sorted by priority: first values should be tried first.

Returns the language switch links for the given language type.

Parameters

string $type

The language type.

Url $url

The URL the switch links will be relative to.

Return Value

array

A keyed array of links ready to be themed.

static array getStandardLanguageList()

Some common languages with their English and native names.

Language codes are defined by the W3C language tags document for interoperability. Language codes typically have a language and, optionally, a script or regional variant name. See: http://www.w3.org/International/articles/language-tags/ for more information.

Return Value

array

An array of language code to language name information. Language name information itself is an array of English and native names.

static array getUnitedNationsLanguageList()

The 6 official languages used at the United Nations.

This list is based on http://www.un.org/en/sections/about-un/official-languages/index.html and it uses the same format as getStandardLanguageList().

Return Value

array

An array with language codes as keys, and English and native language names as values.

$this setConfigOverrideLanguage(LanguageInterface $language = NULL)

Sets the configuration override language.

This function is a noop since the configuration cannot be overridden by language unless the Language module is enabled. That replaces the default language manager with a configurable language manager.

Parameters

LanguageInterface $language

The language to override configuration with.

Return Value

$this

See also

ConfigurableLanguageManager::setConfigOverrideLanguage

LanguageInterface getConfigOverrideLanguage()

Gets the current configuration override language.

Return Value

LanguageInterface

The current configuration override language.

protected LanguageInterface[] filterLanguages(array $languages, int $flags = LanguageInterface::STATE_CONFIGURABLE)

Filters the full list of languages based on the value of the flag.

The locked languages are removed by default.

Parameters

array $languages

Array with languages to be filtered.

int $flags

(optional) Specifies the state of the languages that have to be returned. It can be: LanguageInterface::STATE_CONFIGURABLE, LanguageInterface::STATE_LOCKED, or LanguageInterface::STATE_ALL.

Return Value

LanguageInterface[]

An associative array of languages, keyed by the language code.