summaryrefslogtreecommitdiff
path: root/modules/locale
diff options
context:
space:
mode:
Diffstat (limited to 'modules/locale')
-rw-r--r--modules/locale/locale.install11
-rw-r--r--modules/locale/locale.module15
-rw-r--r--modules/locale/locale.test49
3 files changed, 69 insertions, 6 deletions
diff --git a/modules/locale/locale.install b/modules/locale/locale.install
index c51033ea7..cabdc0b34 100644
--- a/modules/locale/locale.install
+++ b/modules/locale/locale.install
@@ -113,6 +113,17 @@ function locale_update_7001() {
}
/**
+ * Updates URL language negotiation by adding the URL fallback detection method.
+ */
+function locale_update_7002() {
+ $language_types_info = language_types_info();
+ $info = $language_types_info[LANGUAGE_TYPE_URL];
+ if (isset($info['fixed'])) {
+ language_negotiation_set(LANGUAGE_TYPE_URL, array_flip($info['fixed']));
+ }
+}
+
+/**
* @} End of "defgroup updates-6.x-to-7.x"
*/
diff --git a/modules/locale/locale.module b/modules/locale/locale.module
index d3e961823..696e0afdc 100644
--- a/modules/locale/locale.module
+++ b/modules/locale/locale.module
@@ -503,8 +503,8 @@ function locale_entity_info_alter(&$entity_info) {
* 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 provider. It is used by l() as the default language if none is
- * specified.
+ * URL language provider or the URL fallback provider if no language can be
+ * detected. It is used by l() as the default language if none is specified.
*/
function locale_language_types_info() {
require_once DRUPAL_ROOT . '/includes/locale.inc';
@@ -517,7 +517,7 @@ function locale_language_types_info() {
'fixed' => array(LOCALE_LANGUAGE_NEGOTIATION_INTERFACE),
),
LANGUAGE_TYPE_URL => array(
- 'fixed' => array(LOCALE_LANGUAGE_NEGOTIATION_URL),
+ 'fixed' => array(LOCALE_LANGUAGE_NEGOTIATION_URL, LOCALE_LANGUAGE_NEGOTIATION_URL_FALLBACK),
),
);
}
@@ -582,6 +582,15 @@ function locale_language_negotiation_info() {
'description' => t('Use the detected interface language.'),
);
+ $providers[LOCALE_LANGUAGE_NEGOTIATION_URL_FALLBACK] = array(
+ 'types' => array(LANGUAGE_TYPE_URL),
+ 'callbacks' => array('language' => 'locale_language_url_fallback'),
+ 'file' => $file,
+ 'weight' => 8,
+ 'name' => t('URL fallback'),
+ 'description' => t('Use an already detected language for URLs if none is found.'),
+ );
+
return $providers;
}
diff --git a/modules/locale/locale.test b/modules/locale/locale.test
index 2efa63d82..6bc2fb757 100644
--- a/modules/locale/locale.test
+++ b/modules/locale/locale.test
@@ -1730,15 +1730,14 @@ class LocaleUILanguageNegotiationTest extends DrupalWebTestCase {
parent::setUp('locale', 'locale_test');
require_once DRUPAL_ROOT . '/includes/language.inc';
drupal_load('module', 'locale');
+ $admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages', 'administer blocks'));
+ $this->drupalLogin($admin_user);
}
/**
* Tests for language switching by URL path.
*/
function testUILanguageNegotiation() {
- $admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages'));
- $this->drupalLogin($admin_user);
-
// A few languages to switch to.
// This one is unknown, should get the default lang version.
$language_unknown = 'blah-blah';
@@ -1892,6 +1891,50 @@ class LocaleUILanguageNegotiationTest extends DrupalWebTestCase {
$this->drupalGet($test['path'], array(), $test['http_header']);
$this->assertText($test['expect'], $test['message']);
}
+
+ /**
+ * Test URL language detection when the requested URL has no language.
+ */
+ function testUrlLanguageFallback() {
+ // Add the Italian language.
+ $language_browser_fallback = 'it';
+ locale_add_language($language_browser_fallback);
+ $languages = language_list();
+
+ // Enable the path prefix for the default language: this way any unprefixed
+ // URL must have a valid fallback value.
+ $edit = array('prefix' => 'en');
+ $this->drupalPost('admin/config/regional/language/edit/en', $edit, t('Save language'));
+
+ // Enable browser and URL language detection.
+ $edit = array(
+ 'language[enabled][locale-browser]' => TRUE,
+ 'language[enabled][locale-url]' => TRUE,
+ 'language[weight][locale-browser]' => -8,
+ 'language[weight][locale-url]' => -10,
+ );
+ $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
+ $this->drupalGet('admin/config/regional/language/configure');
+
+ // Enable the language switcher block.
+ $edit = array('blocks[locale_language][region]' => 'sidebar_first');
+ $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
+
+ // Access the front page without specifying any valid URL language prefix
+ // and having as browser language preference a non-default language.
+ $http_header = array("Accept-Language: $language_browser_fallback;q=1");
+ $this->drupalGet('', array(), $http_header);
+
+ // Check that the language switcher active link matches the given browser
+ // language.
+ $args = array(':url' => base_path() . (!empty($GLOBALS['conf']['clean_url']) ? $language_browser_fallback : "?q=$language_browser_fallback"));
+ $fields = $this->xpath('//div[@id="block-locale-language"]//a[@class="language-link active" and @href=:url]', $args);
+ $this->assertTrue($fields[0] == $languages[$language_browser_fallback]->native, t('The browser language is the URL active language'));
+
+ // Check that URLs are rewritten using the given browser language.
+ $fields = $this->xpath('//div[@id="site-name"]//a[@rel="home" and @href=:url]//span', $args);
+ $this->assertTrue($fields[0] == 'Drupal', t('URLs are rewritten using the browser language.'));
+ }
}
/**