Redirect users to their main language first visit. I needed it only on the front page, but you can simply remove the if statement if you need the functionality on every page. The language detection is done by Smart IP. Which is a separate module.
I made a separate module for this. The code uses smart_ip module so i made it a requirement.
Source code viewer
name = Smart IP redirect description = "Redirect users to right language on first visit, using Smart IP for country detection." core = 7.x dependencies[] = smart_ipProgramming Language: YAML
The code with comments. Everything should be self explanatory.
Source code viewer
<?php /** * Implements hook_init(). */ function HOOK_init() { // don't forget to rename the HOOK to your modules machine name if( // if country is detected // if is front page drupal_is_front_page() && // if user has not been already redirected ) { // 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(); // if current language is not active language { // does the visitors language exist in our Drupal { // set session variable mekaia_smartip_redirect if the user will be redirected $_SESSION['smartip_redirect'] = TRUE; // redirect user to the designated language } } } }Programming Language: PHP