I am setting up my interface language detection as such:

  1. User
  2. Session
  3. Browser

When anonymous users access the website, the interface is shown in the sites default language regardless of their browser setting. Is there a way to fix this?

There is indeed 2 separate caches for example.com/ and example.com/?language=ja, but we just need to have the users be redirected to the correct path. Perhaps we can write a redirect script in hook_boot() for anonymous users without a $_GET['language'] variable set? But then again $language is not accessible in hook_boot()..

Comments

fenda’s picture

So it turns out browser detection is skipped by design for anonymous users with page cache enabled. I implemented the following to work around this:

/**
 * Implements hook_boot().
 */
function cyac_boot() {
  global $user;

  if (!$user->uid && !isset($_GET['language'])) {
    global $base_url;

    require_once DRUPAL_ROOT . '/includes/locale.inc';

    // Get languages grouped by status and select only the enabled ones.
    $languages = language_list('enabled');
    $languages = $languages[1];

    $langcode = locale_language_from_browser($languages);

    if ($langcode != 'en') {
      $path = $base_url . '/' . $_GET['q'] . '?language=' . $langcode;

      header('Location: ' . $path, TRUE, 302);
      exit;
    }
  }
}
kspal’s picture

Where did you put this code? Did you create a specific module to do so?

fenda’s picture

Status: Active » Closed (works as designed)

I put it in my custom module. My custom module had a weight in the system table of -1, not sure if that helped. :)

facal’s picture

Thank you!
I would also suggest this http://drupal.org/node/339958#comment-2503184 which seems to me an alternative solution (for Apache only).
drupaljoe, please correct me if I misunderstood