I'm using custom module to redirect users from Serbia on Serbian language. So, if user comes from Serbia on front page, in URL will be added '/sr'. The problem is that user can't change the language on front page because it always will be redirected.
Can someone help me to fix this issue and make visitor able to change language on front page?
There is the code of custom module:
global $user;
function smart_ip_redirect_init() {
if(
// if country is detected
!empty($_SESSION['smart_ip']['location']['country_code']) &&
// if is front page and anonymous user
drupal_is_front_page() && user_is_anonymous()
) {
// load some global variables, language contains information about current language, base_url is base url
global $language, $base_url;
// load all active languages
$languages = language_list();
// resolve the country code to language, you have to make cases for your needs
function country_to_language() {
switch ($_SESSION['smart_ip']['location']['country_code']) {
case 'RS':
return 'sr';
break;
default:
return '';
break;
}
}
// if the URL have no lang parameter
if(strtoupper(request_uri()) == strtoupper("/") )
{
// does the visitors language exist in our Drupal
if(array_key_exists(strtolower(country_to_language()), $languages))
{
// redirect user to the designated language
drupal_goto($base_url.'/'.strtolower(country_to_language()));
}
}
}
}