interface LanguageNegotiatorInterface (View source)

Common interface for language negotiation services.

The language negotiation API is based on two major concepts:

  • Language types: types of translatable data (the types of data that a user can view or request).
  • Language negotiation methods: responsible for determining which language to use to present a particular piece of data to the user. Both language types and language negotiation methods are customizable.

Drupal defines three built-in language types:

  • Interface language: The page's main language, used to present translated user interface elements such as titles, labels, help text, and messages.
  • Content language: The language used to present content that is available in more than one language.
  • URL language: The language associated with URLs. When generating a URL, this value will be used for URL's as a default if no explicit preference is provided. Modules can define additional language types through hook_language_types_info(), and alter existing language type definitions through hook_language_types_info_alter().

Language types may be configurable or fixed. The language negotiation methods associated with a configurable language type can be explicitly set through the user interface. A fixed language type has predetermined (module-defined) language negotiation settings and, thus, does not appear in the configuration page. Here is a code snippet that makes the content language (which by default inherits the interface language's values) configurable:

Constants

METHOD_ID

The language negotiation method id for the language negotiator itself.

Methods

reset()

Resets the negotiated languages and the method instances.

setCurrentUser(AccountInterface $current_user)

Sets the current active user and resets all language types.

initializeType(string $type)

Initializes the specified language type.

array[]
getNegotiationMethods(string $type = NULL)

Returns the language negotiation methods enabled for a language type.

getNegotiationMethodInstance(string $method_id)

Returns an instance of the specified language negotiation method.

string
getPrimaryNegotiationMethod(string $type)

Returns the ID of the language type's primary language negotiation method.

bool
isNegotiationMethodEnabled(string $method_id, string $type = NULL)

Checks whether a language negotiation method is enabled for a language type.

saveConfiguration(string $type, int[] $enabled_methods)

Saves a list of language negotiation methods for a language type.

purgeConfiguration()

Resave the configuration to purge missing negotiation methods.

updateConfiguration(array $types)

Updates the configuration based on the given language types.

Details

reset()

Resets the negotiated languages and the method instances.

setCurrentUser(AccountInterface $current_user)

Sets the current active user and resets all language types.

Parameters

AccountInterface $current_user

The current active user.

LanguageInterface[] initializeType(string $type)

Initializes the specified language type.

Parameters

string $type

The language type to be initialized.

Return Value

LanguageInterface[]

Returns an array containing a single language keyed by the language negotiation method ID used to determine the language of the specified type. If negotiation is not possible the default language is returned.

array[] getNegotiationMethods(string $type = NULL)

Returns the language negotiation methods enabled for a language type.

Parameters

string $type

(optional) The language type. If no type is specified all the method definitions are returned.

Return Value

array[]

An array of language negotiation method definitions keyed by method id.

LanguageNegotiationMethodInterface getNegotiationMethodInstance(string $method_id)

Returns an instance of the specified language negotiation method.

Parameters

string $method_id

The method identifier.

Return Value

LanguageNegotiationMethodInterface

string getPrimaryNegotiationMethod(string $type)

Returns the ID of the language type's primary language negotiation method.

Parameters

string $type

The language type.

Return Value

string

The identifier of the primary language negotiation method for the given language type, or the default method if none exists.

bool isNegotiationMethodEnabled(string $method_id, string $type = NULL)

Checks whether a language negotiation method is enabled for a language type.

Parameters

string $method_id

The language negotiation method ID.

string $type

(optional) The language type. If none is passed, all the configurable language types will be inspected.

Return Value

bool

TRUE if the method is enabled for at least one of the given language types, or FALSE otherwise.

saveConfiguration(string $type, int[] $enabled_methods)

Saves a list of language negotiation methods for a language type.

Parameters

string $type

The language type.

int[] $enabled_methods

An array of language negotiation method weights keyed by method ID.

purgeConfiguration()

Resave the configuration to purge missing negotiation methods.

updateConfiguration(array $types)

Updates the configuration based on the given language types.

Stores the list of the language types along with information about their configurable state. Stores the default settings if the language type is not configurable.

Parameters

array $types

An array of configurable language types.