Functions to customize the language types and the negotiation process.

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 providers: functions for determining which language to use to present a particular piece of data to the user.

Both language types and language negotiation providers are customizable.

Backdrop 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 (see Field Language API for details).
  • URL language: The language associated with URLs. When generating a URL, this value will be used by url() 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 providers 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:

function my_module_language_types_info_alter(&$language_types) {
  unset($language_types[LANGUAGE_TYPE_CONTENT]['fixed']);
}

Every language type can have a different set of language negotiation providers assigned to it. Different language types often share the same language negotiation settings, but they can have independent settings if needed. If two language types are configured the same way, their language switcher configuration will be functionally identical and the same settings will act on both language types.

Backdrop defines the following built-in language negotiation providers:

  • URL: Determine the language from the URL (path prefix or domain).
  • Session: Determine the language from a request/session parameter.
  • User: Follow the user's language preference.
  • Browser: Determine the language from the browser's language settings.
  • Default language: Use the default site language.

Language negotiation providers are simple callback functions that implement a particular logic to return a language code. For instance, the URL provider searches for a valid path prefix or domain name in the current request URL. If a language negotiation provider does not return a valid language code, the next provider associated to the language type (based on provider weight) is invoked.

Modules can define additional language negotiation providers through hook_language_negotiation_info(), and alter existing providers through hook_language_negotiation_info_alter(). Here is an example snippet that lets path prefixes be ignored for administrative paths:

function my_module_language_negotiation_info_alter(&$negotiation_info) {
  // Replace the core function with our own function.
  $negotiation_info[LANGUAGE_NEGOTIATION_URL]['callbacks']['language'] = 'my_module_from_url';
  $negotiation_info[LANGUAGE_NEGOTIATION_URL]['file'] = backdrop_get_path('module', 'my_module') . '/my_module.module';
}

function my_module_from_url($languages) {
  // Use the core URL language negotiation provider to get a valid language
  // code.
  $langcode = locale_language_from_url($languages);

  // If we are on an administrative path, override with the default language.
  if (isset($_GET['q']) && strtok($_GET['q'], '/') == 'admin') {
    return language_default()->langcode;
  }
  return $langcode;
}

For more information, see Language Negotiation API

File

core/includes/language.inc, line 14
Language Negotiation API.

Functions

Name Locationsort descending Description
language_types_initialize core/includes/language.inc Chooses a language based on language negotiation provider settings.
language_types_info core/includes/language.inc Returns all the defined language types.
language_types_get_configurable core/includes/language.inc Returns only the configurable language types.
language_negotiation_get core/includes/language.inc Checks whether a language negotiation provider is enabled for a language type.
language_negotiation_get_any core/includes/language.inc Checks if the language negotiation provider is enabled for any language type.
language_negotiation_get_switch_links core/includes/language.inc Returns the language switch links for the given language.
language_negotiation_purge core/includes/language.inc Removes any unused language negotiation providers from the configuration.
language_negotiation_set core/includes/language.inc Saves a list of language negotiation providers.
language_negotiation_order core/includes/language.inc Get the language negotiator provider order for a language type.
language_negotiation_info core/includes/language.inc Returns all the defined language negotiation providers.
language_provider_invoke core/includes/language.inc Helper function used to cache the language negotiation providers results.
language_from_default core/includes/language.inc Returns the default language negotiation provider.
language_url_split_prefix core/includes/language.inc Splits the given path into prefix and actual path.
language_fallback_get_candidates core/includes/language.inc Returns the possible fallback languages ordered by language weight.
hook_language_types_info core/modules/system/language.api.php Define language types.
hook_language_types_info_alter core/modules/system/language.api.php Perform alterations on language types.
hook_language_negotiation_info core/modules/system/language.api.php Define language negotiation providers.
hook_language_negotiation_info_alter core/modules/system/language.api.php Perform alterations on language negotiation providers.