predict language and change for front page only

Events happening in the community are now at Drupal community events on www.drupal.org.
mdubnz's picture

I'm currently using 'Path prefix only' for Language Negotiation. Default language is Japanese (no prefix) and the user can then switch to English (/en) if required, using Language Switcher block. However, it appears that contrary to what we expected more than 50% of users are arriving at the site with their browser language being US English. The site had been designed with the understanding the majority of visitors would be Japanese. This is interesting in terms of development of this particular client's business, but means that many visitors to the site have to switch language immediately (if indeed they don't move on before noticing the Language Switcher icons). So, in an attempt to remedy this for the home page only (my main concern), this is what I've done at the very top of the page template. Keen for some comments on whether or not this is an ideal solution... Cheers

global $base_root;
$browser_language = language_from_browser();
if ( ($base_root . request_uri()) == 'http://www.clientsite.co.nz/' && $browser_language->language != 'ja') {
  header ("Location: http://www.clientsite.co.nz/en");
}

Comments

If you set Path prefix only

iztok's picture

If you set Path prefix only and set default language Japanese it should open Japanese version of the site. Or I got this wrong?

Iztok

Yes, it does, but this isn't

mdubnz's picture

Yes, it does, but this isn't ideal if in fact over half of the users aren't Japanese. So, whilst keeping Japanese as the default language that the site opens in I wanted to automatically open the English version of the site if the viewer wasn't Japanese.
Cheers

Module based solution

Al01's picture

You can do this also through a custom module:

<?php
/**
* Implementation of hook_init().
*/
function MYMODULE_init() {
  global
$language;
 
$browser_language = language_from_browser();
  if (
drupal_is_front_page() && $language == language_default() && $browser_language != language_default() &&  !$_SESSION['i18n_switch']) {
    
$_SESSION['i18n_switch'] = 'done';
   
drupal_goto('en');
  }
  else {
    
$_SESSION['i18n_switch'] = 'done';
  }
}
?>

$_SESSION['i18n_switch'] I added to executed the redirect just if the first hit is on the front page without prefix. So later the visitor can switch to the Japanese version also on the front page.

I thought "Path prefix with

LarsKramer's picture

I thought "Path prefix with language fallback" was meant for this use? So if the user has English as preferred language in his browser settings, the page should show in English instead of default language.
I would also recommend this module: http://drupal.org/project/languageinterface: apart from offering a better language switcher, it can save anonymous users' language choice in a cookie so that when they return, they will se the frontpage in the same language as last time they visited the site. As far as I remember, it's not enabled by default, so you should check the module settings after installing.

Internationalization

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: