3

I have a Drupal 7 site that is available in both English and Japanese. I am using i18n and entity translation; I have configured the User interface text language detection at /admin/config/regional/language/configure as follows:

  • User
  • URL
  • Browser
  • Default

This is great for authenticated users and for all pages of the site except the front page, which is the most important.

I would like users with Japanese browsers to be taken to the Japanese version and all other users to be taken to the English version. However, since the site default language is English, it appears that all users are by default taken to the English version.

If it matters, my front page is actually a panel.

How can I configure it so that users with Japanese browsers are redirected to the Japanese language version? I don't want to move browser to the top of the list because I want to respect user preferences for authenticated users, and I also need URL to precede browser because I want people to be able to use the language switcher block to change the language if for some reason they are using a browser in a language they are not comfortable with.

4
  • Just to be really clear, you are talking about automagically redirecting users on the initial page view and not self selection?
    – mpdonadio
    Jul 12, 2012 at 15:33
  • Yes, I want to automatically redirect based on the browser language setting. Jul 12, 2012 at 15:48
  • Which language switching method are you using? Are the Japanese and English versions panel variants of the same page?
    – mpdonadio
    Jul 12, 2012 at 17:09
  • I'm open to doing this any possible way in Panels. Jul 12, 2012 at 17:46

1 Answer 1

0

This is inspired by a deleted answer, but my first approach would be to have the whole site built with Panels, and then have an English variant and Japanese variant for each.

For each variant, you would use the PHP Code selection rule, and then add your logic.

For unauthenticated users, you could examine $_SERVER["HTTP_ACCEPT_LANGUAGE"] and return the proper thing. For authenticated users, would would use your normal method.

Preferably, you would put the logic in a module function that you would call from the PHP Code selection rule, so you only need to change things in one place. You could also do a ctools plugin to make your own selection rule, too, using the same logic.

3
  • Thanks for this idea. I only need the logic on the front page, so I won't need to make the whole site in Panels. However, I wasn't able to get this to work. Perhaps I'm missing sometime obvious, but I set my selection rule to if ($_SERVER["HTTP_ACCEPT_LANGUAGE"] == 'ja-jp') for the Japanese variant, but it is not selected. However, a custom content in the English variant which is displayed prints the value of "HTTP_ACCEPT_LANGUAGE" as ja-jp. Jul 13, 2012 at 5:00
  • I am not sure if you can use that as a comparison. HTTP_ACCEPT_LANGUAGE will have all of the acceptable languages with their priorities for proper content negitiation. Does using strpos($_SERVER["HTTP_ACCEPT_LANGUAGE"], "ja-jp") !== FALSE work as the conditional?
    – mpdonadio
    Jul 13, 2012 at 10:39
  • Unfortunately, that doesn't work as the conditional, either. Jul 14, 2012 at 1:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.