Commit 661488af authored by Dries's avatar Dries

- Patch #1222106 by Gábor Hojtsy, plach, xjm: Unify language negotiation APIs,...

- Patch #1222106 by Gábor Hojtsy, plach, xjm: Unify language negotiation APIs, declutter terminology.
parent cf77b6e8
......@@ -20,22 +20,22 @@
* The negotiated language object.
*/
function language_types_initialize($type) {
// Execute the language providers in the order they were set up and return the
// first valid language found.
// Execute the language negotiation methods in the order they were set up and
// return the first valid language found.
$negotiation = variable_get("language_negotiation_$type", array());
foreach ($negotiation as $provider_id => $provider) {
$language = language_provider_invoke($provider_id, $provider);
foreach ($negotiation as $method_id => $method) {
$language = language_negotiation_method_invoke($method_id, $method);
if ($language) {
// Remember the provider key used to detect the language.
$language->provider = $provider_id;
// Remember the method ID used to detect the language.
$language->method_id = $method_id;
return $language;
}
}
// If no other language was found use the default one.
$language = language_default();
$language->provider = LANGUAGE_NEGOTIATION_DEFAULT;
$language->method_id = LANGUAGE_NEGOTIATION_DEFAULT;
return $language;
}
......@@ -68,11 +68,11 @@ function language_types_info() {
* language type itself.
*
* @param $stored
* Optional. By default retrieves values from the 'language_types' variable to
* avoid unnecessary hook invocations.
* If set to FALSE retrieves values from the actual language type definitions.
* This allows to react to alterations performed on the definitions by modules
* installed after the 'language_types' variable is set.
* (optional) By default, retrieves values from the 'language_types' variable
* to avoid unnecessary hook invocations. If set to FALSE, retrieves values
* from the actual language type definitions. This allows reaction to
* alterations performed on the definitions by modules installed after the
* 'language_types' variable is set.
*
* @return
* An array of language type names.
......@@ -99,7 +99,7 @@ function language_types_get_configurable($stored = TRUE) {
}
/**
* Disable the given language types.
* Disables the given language types.
*
* @param $types
* An array of language types.
......@@ -128,17 +128,17 @@ function language_types_set() {
// whether the 'fixed' key is defined. Non-configurable (fixed) language types
// have their language negotiation settings stored there.
$language_types = array();
$defined_providers = language_negotiation_info();
$negotiation_info = language_negotiation_info();
foreach (language_types_info() as $type => $info) {
if (isset($info['fixed'])) {
$language_types[$type] = FALSE;
$negotiation = array();
foreach ($info['fixed'] as $weight => $id) {
if (isset($defined_providers[$id])) {
$negotiation[$id] = $weight;
$method_weights = array();
foreach ($info['fixed'] as $weight => $method_id) {
if (isset($negotiation_info[$method_id])) {
$method_weights[$method_id] = $weight;
}
}
language_negotiation_set($type, $negotiation);
language_negotiation_set($type, $method_weights);
}
else {
$language_types[$type] = TRUE;
......@@ -154,52 +154,35 @@ function language_types_set() {
}
/**
* Check if a language provider is enabled.
*
* This has two possible behaviors:
* - If $provider_id is given return its ID if enabled, FALSE otherwise.
* - If no ID is passed the first enabled language provider is returned.
* Returns the ID of the language type's first language negotiation method.
*
* @param $type
* The language negotiation type.
* @param $provider_id
* The language provider ID.
*
* @return
* The provider ID if it is enabled, FALSE otherwise.
* The language type.
*/
function language_negotiation_get($type, $provider_id = NULL) {
function language_negotiation_method_get_first($type) {
$negotiation = variable_get("language_negotiation_$type", array());
if (empty($negotiation)) {
return empty($provider_id) ? LANGUAGE_NEGOTIATION_DEFAULT : FALSE;
}
if (empty($provider_id)) {
return key($negotiation);
}
if (isset($negotiation[$provider_id])) {
return $provider_id;
}
return FALSE;
return empty($negotiation) ? LANGUAGE_NEGOTIATION_DEFAULT : key($negotiation);
}
/**
* Check if the given language provider is enabled for any configurable language
* type.
* Checks if a language negotiation method is enabled for a language type.
*
* @param $provider_id
* The language provider ID.
* @param $method_id
* The language negotiation method ID.
* @param $type
* (optional) The language type. If none is passed, all the configurable
* language types will be inspected.
*
* @return
* TRUE if there is at least one language type for which the give language
* provider is enabled, FALSE otherwise.
* TRUE if the method is enabled for at least one of the given language
* types, or FALSE otherwise.
*/
function language_negotiation_get_any($provider_id) {
foreach (language_types_get_configurable() as $type) {
if (language_negotiation_get($type, $provider_id)) {
function language_negotiation_method_enabled($method_id, $type = NULL) {
$language_types = !empty($type) ? array($type) : language_types_get_configurable();
foreach ($language_types as $type) {
$negotiation = variable_get("language_negotiation_$type", array());
if (isset($negotiation[$method_id])) {
return TRUE;
}
}
......@@ -208,10 +191,10 @@ function language_negotiation_get_any($provider_id) {
}
/**
* Return the language switch links for the given language.
* Returns the language switch links for the given language type.
*
* @param $type
* The language negotiation type.
* The language type.
* @param $path
* The internal path the switch links will be relative to.
*
......@@ -222,19 +205,19 @@ function language_negotiation_get_switch_links($type, $path) {
$links = FALSE;
$negotiation = variable_get("language_negotiation_$type", array());
foreach ($negotiation as $id => $provider) {
if (isset($provider['callbacks']['switcher'])) {
if (isset($provider['file'])) {
require_once DRUPAL_ROOT . '/' . $provider['file'];
foreach ($negotiation as $method_id => $method) {
if (isset($method['callbacks']['language_switch'])) {
if (isset($method['file'])) {
require_once DRUPAL_ROOT . '/' . $method['file'];
}
$callback = $provider['callbacks']['switcher'];
$callback = $method['callbacks']['language_switch'];
$result = $callback($type, $path);
if (!empty($result)) {
// Allow modules to provide translations for specific links.
drupal_alter('language_switch_links', $result, $type, $path);
$links = (object) array('links' => $result, 'provider' => $id);
$links = (object) array('links' => $result, 'method_id' => $method_id);
break;
}
}
......@@ -244,7 +227,7 @@ function language_negotiation_get_switch_links($type, $path) {
}
/**
* Updates language configuration to remove any language provider that is no longer defined.
* Removes any language negotiation methods that are no longer defined.
*/
function language_negotiation_purge() {
// Ensure that we are getting the defined language negotiation information. An
......@@ -253,60 +236,54 @@ function language_negotiation_purge() {
drupal_static_reset('language_negotiation_info');
drupal_static_reset('language_types_info');
$defined_providers = language_negotiation_info();
$negotiation_info = language_negotiation_info();
foreach (language_types_info() as $type => $type_info) {
$weight = 0;
$negotiation = array();
foreach (variable_get("language_negotiation_$type", array()) as $id => $provider) {
if (isset($defined_providers[$id])) {
$negotiation[$id] = $weight++;
$method_weights = array();
foreach (variable_get("language_negotiation_$type", array()) as $method_id => $method) {
if (isset($negotiation_info[$method_id])) {
$method_weights[$method_id] = $weight++;
}
}
language_negotiation_set($type, $negotiation);
language_negotiation_set($type, $method_weights);
}
}
/**
* Save a list of language providers.
* Saves a list of language negotiation methods for a language type.
*
* @param $type
* The language negotiation type.
* @param $language_providers
* An array of language provider weights keyed by id.
* @see language_provider_weight()
* The language type.
* @param $method_weights
* An array of language negotiation method weights keyed by method id.
*/
function language_negotiation_set($type, $language_providers) {
function language_negotiation_set($type, $method_weights) {
// Save only the necessary fields.
$provider_fields = array('callbacks', 'file', 'cache');
$method_fields = array('callbacks', 'file', 'cache');
$negotiation = array();
$providers_weight = array();
$defined_providers = language_negotiation_info();
$negotiation_info = language_negotiation_info();
$default_types = language_types_get_configurable(FALSE);
// Initialize the providers weight list.
foreach ($language_providers as $id => $provider) {
$providers_weight[$id] = language_provider_weight($provider);
}
// Order providers list by weight.
asort($providers_weight);
foreach ($providers_weight as $id => $weight) {
if (isset($defined_providers[$id])) {
$provider = $defined_providers[$id];
// If the provider does not express any preference about types, make it
// available for any configurable type.
$types = array_flip(isset($provider['types']) ? $provider['types'] : $default_types);
// Check if the provider is defined and has the right type.
// Order the language negotiation method list by weight.
asort($method_weights);
foreach ($method_weights as $method_id => $weight) {
if (isset($negotiation_info[$method_id])) {
$method = $negotiation_info[$method_id];
// If the language negotiation method does not express any preference
// about types, make it available for any configurable type.
$types = array_flip(isset($method['types']) ? $method['types'] : $default_types);
// Check if the language negotiation method is defined and has the right
// type.
if (isset($types[$type])) {
$provider_data = array();
foreach ($provider_fields as $field) {
if (isset($provider[$field])) {
$provider_data[$field] = $provider[$field];
$method_data = array();
foreach ($method_fields as $field) {
if (isset($method[$field])) {
$method_data[$field] = $method[$field];
}
}
$negotiation[$id] = $provider_data;
$negotiation[$method_id] = $method_data;
}
}
}
......@@ -315,20 +292,20 @@ function language_negotiation_set($type, $language_providers) {
}
/**
* Return all the defined language providers.
* Returns all defined language negotiation methods.
*
* @return
* An array of language providers.
* An array of language negotiation methods.
*/
function language_negotiation_info() {
$language_providers = &drupal_static(__FUNCTION__);
$negotiation_info = &drupal_static(__FUNCTION__);
if (!isset($language_providers)) {
// Collect all the module-defined language negotiation providers.
$language_providers = module_invoke_all('language_negotiation_info');
if (!isset($negotiation_info)) {
// Collect all the module-defined language negotiation methods.
$negotiation_info = module_invoke_all('language_negotiation_info');
// Add the default language provider.
$language_providers[LANGUAGE_NEGOTIATION_DEFAULT] = array(
// Add the default language negotiation method.
$negotiation_info[LANGUAGE_NEGOTIATION_DEFAULT] = array(
'callbacks' => array('language' => 'language_from_default'),
'weight' => 10,
'name' => t('Default language'),
......@@ -336,74 +313,60 @@ function language_negotiation_info() {
'config' => 'admin/config/regional/language',
);
// Let other modules alter the list of language providers.
drupal_alter('language_negotiation_info', $language_providers);
// Let other modules alter the list of language negotiation methods.
drupal_alter('language_negotiation_info', $negotiation_info);
}
return $language_providers;
return $negotiation_info;
}
/**
* Helper function used to cache the language providers results.
* Invokes a language negotiation method and caches the results.
*
* @param $provider_id
* The language provider ID.
* @param $provider
* The language provider to be invoked. If not passed it will be explicitly
* loaded through language_negotiation_info().
* @param $method_id
* The language negotiation method ID.
* @param $method
* (optional) The language negotiation method to be invoked. If not passed it
* will be explicitly loaded through language_negotiation_info().
*
* @return
* The language provider's return value.
* The language negotiation method's return value.
*/
function language_provider_invoke($provider_id, $provider = NULL) {
function language_negotiation_method_invoke($method_id, $method = NULL) {
$results = &drupal_static(__FUNCTION__);
if (!isset($results[$provider_id])) {
if (!isset($results[$method_id])) {
global $user;
// Get the enabled languages only.
$languages = language_list(TRUE);
if (!isset($provider)) {
$providers = language_negotiation_info();
$provider = $providers[$provider_id];
if (!isset($method)) {
$negotiation_info = language_negotiation_info();
$method = $negotiation_info[$method_id];
}
if (isset($provider['file'])) {
require_once DRUPAL_ROOT . '/' . $provider['file'];
if (isset($method['file'])) {
require_once DRUPAL_ROOT . '/' . $method['file'];
}
// If the language provider has no cache preference or this is satisfied
// we can execute the callback.
$config = config('system.performance');
$cache = !isset($provider['cache']) || $user->uid || $provider['cache'] == $config->get('cache');
$callback = isset($provider['callbacks']['language']) ? $provider['callbacks']['language'] : FALSE;
// If the language negotiation method has no cache preference or this is
// satisfied we can execute the callback.
$cache = !isset($method['cache']) || $user->uid || $method['cache'] == variable_get('cache', 0);
$callback = isset($method['callbacks']['negotiation']) ? $method['callbacks']['negotiation'] : FALSE;
$langcode = $cache && function_exists($callback) ? $callback($languages) : FALSE;
$results[$provider_id] = isset($languages[$langcode]) ? $languages[$langcode] : FALSE;
$results[$method_id] = isset($languages[$langcode]) ? $languages[$langcode] : FALSE;
}
// Since objects are resources we need to return a clone to prevent the
// provider cache to be unintentionally altered. The same providers might be
// used with different language types based on configuration.
return !empty($results[$provider_id]) ? clone($results[$provider_id]) : $results[$provider_id];
}
/**
* Return the passed language provider weight or a default value.
*
* @param $provider
* A language provider data structure.
*
* @return
* A numeric weight.
*/
function language_provider_weight($provider) {
$default = is_numeric($provider) ? $provider : 0;
return isset($provider['weight']) && is_numeric($provider['weight']) ? $provider['weight'] : $default;
// language negotiation method cache to be unintentionally altered. The same
// language negotiation methods might be used with different language types
// based on configuration.
return !empty($results[$method_id]) ? clone($results[$method_id]) : $results[$method_id];
}
/**
* Default language provider.
* Returns the default language code.
*
* @return
* The default language code.
......
......@@ -264,7 +264,7 @@ function locale_language_from_session($languages) {
function locale_language_from_url($languages) {
$language_url = FALSE;
if (!language_negotiation_get_any(LANGUAGE_NEGOTIATION_URL)) {
if (!language_negotiation_method_enabled(LANGUAGE_NEGOTIATION_URL)) {
return $language_url;
}
......@@ -303,8 +303,8 @@ function locale_language_from_url($languages) {
* Determines the language to be assigned to URLs when none is detected.
*
* The language negotiation process has a fallback chain that ends with the
* default language provider. Each built-in language type has a separate
* initialization:
* default language negotiation method. Each built-in language type has a
* separate initialization:
* - Interface language, which is the only configurable one, always gets a valid
* value. If no request-specific language is detected, the default language
* will be used.
......@@ -322,8 +322,8 @@ function locale_language_from_url($languages) {
*
* @param $languages
* (optional) An array of valid language objects. This is passed by
* language_provider_invoke() to every language provider callback, but it is
* not actually needed here. Defaults to NULL.
* language_negotiation_method_invoke() to every language method callback,
* but it is not actually needed here. Defaults to NULL.
* @param $language_type
* (optional) The language type to fall back to. Defaults to the interface
* language.
......@@ -406,7 +406,7 @@ function locale_language_switcher_session($type, $path) {
}
/**
* Rewrite URLs for the URL language provider.
* Rewrite URLs for the URL language negotiation method.
*/
function locale_language_url_rewrite_url(&$path, &$options) {
static $drupal_static_fast;
......@@ -492,7 +492,7 @@ function locale_language_negotiation_url_domains_save(array $domains) {
}
/**
* Rewrite URLs for the Session language provider.
* Rewrite URLs for the Session language negotiation method.
*/
function locale_language_url_rewrite_session(&$path, &$options) {
static $query_rewrite, $query_param, $query_value;
......@@ -506,16 +506,16 @@ function locale_language_url_rewrite_session(&$path, &$options) {
$languages = language_list(TRUE);
$query_param = check_plain(variable_get('locale_language_negotiation_session_param', 'language'));
$query_value = isset($_GET[$query_param]) ? check_plain($_GET[$query_param]) : NULL;
$query_rewrite = isset($languages[$query_value]) && language_negotiation_get_any(LANGUAGE_NEGOTIATION_SESSION);
$query_rewrite = isset($languages[$query_value]) && language_negotiation_method_enabled(LANGUAGE_NEGOTIATION_SESSION);
}
else {
$query_rewrite = FALSE;
}
}
// If the user is anonymous, the user language provider is enabled, and the
// corresponding option has been set, we must preserve any explicit user
// language preference even with cookies disabled.
// If the user is anonymous, the user language negotiation method is enabled,
// and the corresponding option has been set, we must preserve any explicit
// user language preference even with cookies disabled.
if ($query_rewrite) {
if (is_string($options['query'])) {
$options['query'] = drupal_get_query_array($options['query']);
......
......@@ -140,9 +140,9 @@ function update_prepare_stored_includes() {
// Update language negotiation settings.
foreach (language_types_get_all() as $language_type) {
$negotiation = variable_get("language_negotiation_$language_type", array());
foreach ($negotiation as $id => &$provider) {
if (isset($negotiation[$id]['file']) && $negotiation[$id]['file'] == 'includes/locale.inc') {
$negotiation[$id]['file'] = 'core/includes/locale.inc';
foreach ($negotiation as $method_id => &$method) {
if (isset($method['file']) && $method['file'] == 'includes/locale.inc') {
$method['file'] = 'core/includes/locale.inc';
}
}
variable_set("language_negotiation_$language_type", $negotiation);
......
......@@ -16,7 +16,7 @@ function language_negotiation_configure_form() {
'#theme' => 'language_negotiation_configure_form',
'#language_types' => language_types_get_configurable(FALSE),
'#language_types_info' => language_types_info(),
'#language_providers' => language_negotiation_info(),
'#language_negotiation_info' => language_negotiation_info(),
);
foreach ($form['#language_types'] as $type) {
......@@ -33,7 +33,7 @@ function language_negotiation_configure_form() {
}
/**
* Helper function to build a language provider table.
* Builds a language negotion method configuration table.
*/
function language_negotiation_configure_form_table(&$form, $type) {
$info = $form['#language_types_info'][$type];
......@@ -42,74 +42,74 @@ function language_negotiation_configure_form_table(&$form, $type) {
'#title' => t('@type language detection', array('@type' => $info['name'])),
'#tree' => TRUE,
'#description' => $info['description'],
'#language_providers' => array(),
'#language_negotiation_info' => array(),
'#show_operations' => FALSE,
'weight' => array('#tree' => TRUE),
'enabled' => array('#tree' => TRUE),
);
$language_providers = $form['#language_providers'];
$enabled_providers = variable_get("language_negotiation_$type", array());
$providers_weight = variable_get("locale_language_providers_weight_$type", array());
$negotiation_info = $form['#language_negotiation_info'];
$enabled_methods = variable_get("language_negotiation_$type", array());
$methods_weight = variable_get("locale_language_negotiation_methods_weight_$type", array());
// Add missing data to the providers lists.
foreach ($language_providers as $id => $provider) {
if (!isset($providers_weight[$id])) {
$providers_weight[$id] = language_provider_weight($provider);
// Add missing data to the methods lists.
foreach ($negotiation_info as $method_id => $method) {
if (!isset($methods_weight[$method_id])) {
$methods_weight[$method_id] = isset($method['weight']) ? $method['weight'] : 0;
}
}
// Order providers list by weight.
asort($providers_weight);
// Order methods list by weight.
asort($methods_weight);
foreach ($providers_weight as $id => $weight) {
// A language provider might be no more available if the defining module has
foreach ($methods_weight as $method_id => $weight) {
// A language method might be no more available if the defining module has
// been disabled after the last configuration saving.
if (!isset($language_providers[$id])) {
if (!isset($negotiation_info[$method_id])) {
continue;
}
$enabled = isset($enabled_providers[$id]);
$provider = $language_providers[$id];
$enabled = isset($enabled_methods[$method_id]);
$method = $negotiation_info[$method_id];
// List the provider only if the current type is defined in its 'types' key.
// List the method only if the current type is defined in its 'types' key.
// If it is not defined default to all the configurable language types.
$types = array_flip(isset($provider['types']) ? $provider['types'] : $form['#language_types']);
$types = array_flip(isset($method['types']) ? $method['types'] : $form['#language_types']);
if (isset($types[$type])) {
$table_form['#language_providers'][$id] = $provider;
$provider_name = check_plain($provider['name']);
$table_form['#language_negotiation_info'][$method_id] = $method;
$method_name = check_plain($method['name']);
$table_form['weight'][$id] = array(
$table_form['weight'][$method_id] = array(
'#type' => 'weight',
'#title' => t('Weight for !title language detection method', array('!title' => drupal_strtolower($provider_name))),
'#title' => t('Weight for !title language detection method', array('!title' => drupal_strtolower($method_name))),
'#title_display' => 'invisible',
'#default_value' => $weight,
'#attributes' => array('class' => array("language-provider-weight-$type")),
'#attributes' => array('class' => array("language-method-weight-$type")),
);
$table_form['title'][$id] = array('#markup' => $provider_name);
$table_form['title'][$method_id] = array('#markup' => $method_name);
$table_form['enabled'][$id] = array(
$table_form['enabled'][$method_id] = array(
'#type' => 'checkbox',
'#title' => t('Enable !title language detection method', array('!title' => drupal_strtolower($provider_name))),
'#title' => t('Enable !title language detection method', array('!title' => drupal_strtolower($method_name))),
'#title_display' => 'invisible',
'#default_value' => $enabled,
);
if ($id === LANGUAGE_NEGOTIATION_DEFAULT) {
$table_form['enabled'][$id]['#default_value'] = TRUE;
$table_form['enabled'][$id]['#attributes'] = array('disabled' => 'disabled');
if ($method_id === LANGUAGE_NEGOTIATION_DEFAULT) {
$table_form['enabled'][$method_id]['#default_value'] = TRUE;
$table_form['enabled'][$method_id]['#attributes'] = array('disabled' => 'disabled');
}
$table_form['description'][$id] = array('#markup' => filter_xss_admin($provider['description']));
$table_form['description'][$method_id] = array('#markup' => filter_xss_admin($method['description']));
$config_op = array();
if (isset($provider['config'])) {
$config_op = array('#type' => 'link', '#title' => t('Configure'), '#href' => $provider['config']);
if (isset($method['config'])) {
$config_op = array('#type' => 'link', '#title' => t('Configure'), '#href' => $method['config']);
// If there is at least one operation enabled show the operation column.
$table_form['#show_operations'] = TRUE;
}
$table_form['operation'][$id] = $config_op;
$table_form['operation'][$method_id] = $config_op;
}
}
......@@ -169,12 +169,12 @@ function theme_language_negotiation_configure_form($variables) {
$variables = array(
'header' => $header,
'rows' => $rows,
'attributes' => array('id' => "language-negotiation-providers-$type"),
'attributes' => array('id' => "language-negotiation-methods-$type"),
);
$table = theme('table', $variables);