Functions to customize the language types and the negotiation process.
The language negotiation API is based on two major concepts:
Both language types and language negotiation methods are customizable.
Drupal defines three built-in language types:
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:
function mymodule_language_types_info_alter(&$language_types) {
unset($language_types[Language::TYPE_CONTENT]['fixed']);
}
Every language type can have a different set of language negotiation methods 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.
Drupal defines the following built-in language negotiation methods:
Language negotiation methods are simple callback functions that implement a particular logic to return a language code. For instance, the URL method searches for a valid path prefix or domain name in the current request URL. If a language negotiation method does not return a valid language code, the next method associated to the language type (based on method weight) is invoked.
Modules can define additional language negotiation methods through hook_language_negotiation_info(), and alter existing methods through hook_language_negotiation_info_alter(). Here is an example snippet that lets path prefixes be ignored for administrative paths:
function mymodule_language_negotiation_info_alter(&$negotiation_info) {
// Replace the core function with our own function.
module_load_include('language', 'inc', 'language.negotiation');
$negotiation_info[LANGUAGE_NEGOTIATION_URL]['callbacks']['negotiation'] = 'mymodule_from_url';
$negotiation_info[LANGUAGE_NEGOTIATION_URL]['file'] = drupal_get_path('module', 'mymodule') . '/mymodule.module';
}
function mymodule_from_url($languages) {
// Use the core URL language negotiation method to get a valid language
// code.
module_load_include('language', 'inc', 'language.negotiation');
$langcode = 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;
}
?>
?><?php
For more information, see Language Negotiation API
Name | Location | Description |
---|---|---|
hook_language_negotiation_info |
drupal/ |
Define language negotiation methods. |
hook_language_negotiation_info_alter |
drupal/ |
Perform alterations on language negotiation methods. |
hook_language_types_info |
drupal/ |
Define language types. |
hook_language_types_info_alter |
drupal/ |
Perform alterations on language types. |
language_fallback_get_candidates |
drupal/ |
Returns the possible fallback languages ordered by language weight. |
language_from_selected |
drupal/ |
Identifies language from configuration. |
language_negotiation_get_switch_links |
drupal/ |
Returns the language switch links for the given language type. |
language_negotiation_info |
drupal/ |
Returns all defined language negotiation methods. |
language_negotiation_method_enabled |
drupal/ |
Checks whether a language negotiation method is enabled for a language type. |
language_negotiation_method_get_first |
drupal/ |
Returns the ID of the language type's first language negotiation method. |
language_negotiation_method_invoke |
drupal/ |
Invokes a language negotiation method and caches the results. |
language_negotiation_purge |
drupal/ |
Removes any language negotiation methods that are no longer defined. |
language_negotiation_set |
drupal/ |
Saves a list of language negotiation methods for a language type. |
language_types_disable |
drupal/ |
Disables the given language types. |
language_types_get_configurable |
drupal/ |
Returns only the configurable language types. |
language_types_info |
drupal/ |
Returns information about all defined language types. |
language_types_initialize |
drupal/ |
Chooses a language based on language negotiation method settings. |
language_types_set |
drupal/ |
Updates the language type configuration. |
language_url_split_prefix |
drupal/ |
Splits the given path into prefix and actual path. |