Jump to content

How to control the language redirect?


Karl_T
 Share

Recommended Posts

I would like to know how to hook or change the behavior that when I go to URL without the language segment.

For example:

If the domain is www.abc.com

English(default) URL is www.abc.com/en/

Japanese URL is www.abc.com/jp/

When I browse www.abc.com, it redirects to www.abc.com/en/, which is the default behavior after I set "Default language homepage URL is same as root URL?" to "NO" inside "Languages Support - Page Names" module config. I would like to change this hard redirect to check the user browser language before redirecting to other language. For URL that having the language segment should not be affected. That means browsing www.abc.com/en/ is always go to English language without redirect. I want to apply this change to any frontend pages, not only homepage. Thanks.

Link to comment
Share on other sites

I solve the problem by overriding the LanguageSupportPageNames core module.

1. Copy the LanguageSupportPageNames.module from wire/modules/LanguageSupport/ to site/modules/

2. Change the line 

if(!$setLanguage) $setLanguage = $languages->get('default'); 

to (for example, if you want to redirect some users to chinese site)

$user_lang = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE'])[0];
if($user_lang == 'zh-TW' || $user_lang == 'zh-HK' || $user_lang == 'zh-CN' || $user_lang == 'zh-SG') {
  if(!$setLanguage) $setLanguage = $languages->get('chinese'); //language name that you want to redirect from the above language code
}else{
  if(!$setLanguage) $setLanguage = $languages->get('default'); // else go to default language
}

3. Go to Admin > Modules and refresh modules. Choose to use module inside site/module/.

In this way, when no language segment is entered in the URL, the user will be redirected according to their HTTP_ACCEPT_LANGUAGE header. On the other hand, URL with language segment will not make any redirect.

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...