Jump to content

Improved support for multiple languages​​, currencies, and countries


starnetwork

Recommended Posts

Hi,

Regarding to Modulegarden post here: http://blog.whmcs.com/?t=67990

the updated file: http://www.fast-files.com/getfile.aspx?file=59930

 

I just add to the code the option so switch template with the language file, but there is more few thing to improve it and make it better.

 

the open Issues:

1. when the hook is work, the user can't change language manually.

2. I want to add the option to use maxmind geolite instead of hostip.info API

3. I want to add the option to switch template by manual language selection (and not only with the geoIP)

4. the currency selector not working well also,it's need to be fixed.

5. when user is register to the whmcs / place order, Set the language according to the Geo location and not the default language

 

Any help would be welcome :)

 

the code:

<?php

/**
* GEOLOCATION HOOK FOR WHMCS
* This hook will automatically setup currency and language
* for nonlogged users in your clientarea.
* 
* 
* For more information, read the whole article here:
* http://blog.whmcs.com
* 
* 
* @author  ModulesGarden
* @link    http://www.modulesgarden.com
*/

$country_to_currency = array(
   'default' => 'ILS',
   'US'      => 'USD',
   'UK'      => 'GBP',
   'DE'      => 'EUR',
   'NL'      => 'EUR',
   'FR'      => 'EUR',
   'IT'      => 'EUR',
   'IL'      => 'ILS',
   // NOTE: You can add more below
);

$country_to_language = array(
   'default' => 'hebrew',
   'IL'      => 'hebrew',
   'US'      => 'english',
   'DE'      => 'german',
   'FR'      => 'french',
   'IT'      => 'italian',
   // NOTE: You can add more below
);


$language_to_template = array(
   'default' => 'default-he',
   'hebrew'      => 'default-he',	
   'english'      => 'default',
   'german'      => 'default',
   'french'      => 'default',
   'italian'      => 'default',
   // NOTE: You can add more below
);


/**
* FUNCTION geolocation_getCurrencyId
* This function will return currency id for specific code.
* 
* @param  string 
* @return int
*/
function geolocation_getCurrencyId($currency) {
   $q = mysql_query('SELECT id FROM tblcurrencies WHERE code = "'.mysql_escape_string($currency).'"'); // escape string just in case
   $r = mysql_fetch_assoc($q);
   mysql_free_result($q);

   if(isset($r['id'])) {
       return $r['id'];
   }
}


/**
* Main Geolocation Script
* 
* NOT run script
* - if we are in adminarea
* - if already setup for this session
* - if user is logged in
*/
if(strpos($_SERVER['REQUEST_URI'], $customadminpath) === false && !isset($_SESSION['geolocation_setup']) && $_SESSION['uid'] == false) {

   $_SESSION['geolocation_setup'] = true; // prevent from redirecting back again in this session


   /**
    * Get Country using external service - Hostip.info example
    * NOTE: You can handle this part with any other method.
    */
   $current_country = '';
   $ret = file_get_contents("http://api.hostip.info/get_json.php?ip=".$_SERVER['REMOTE_ADDR']); // this can be called also via cURL
   $parsed_json = @json_decode($ret,true);
   if(isset($parsed_json['country_code'])) {
       $current_country = $parsed_json['country_code'];
   }


   /**
    * Get language, currency and currency ID in order to setup the right values in the system
    */
   $currency = $current_country != '' && isset($country_to_currency[$current_country]) 
               ? $country_to_currency[$current_country]
               : $country_to_currency['default'];    

   $currency_id = geolocation_getCurrencyId($currency);

   $language = $current_country != '' && isset($country_to_language[$current_country]) 
               ? $country_to_language[$current_country]
               : $country_to_language['default'];


   $systpl = $current_template != '' && isset($language_to_template[$current_template]) 
               ? $language_to_template[$current_template]
               : $language_to_template['default'];


   /**
    * Setup Currency Session
    * NOTE: You can remove/disable this part if not needed.
    */
   if($currency_id && (!isset($_SESSION['currency']) || $_SESSION['currency'] != $currency)) {         
       $_SESSION['currency'] = $_SESSION['switched_currency'] = $currency_id;
   }

   /**
    * Setup URL Redirection to Switch Language
    * NOTE: You can remove/disable this part if not needed.
    */
   if(!isset($_SESSION['Language']) || $_SESSION['Language'] != $language) {
       $location = '?language='.$language.'&systpl='.$systpl;     
       if($_SERVER['QUERY_STRING'] != '')
           $location .= '&'.$_SERVER['QUERY_STRING'];

       ob_clean();
       header('location: '.$location);
       die();
   }

}
/**
* Preventing from switching currency by user
* NOTE: You can remove/disable this part if not needed.
*/
if(isset($_SESSION['switched_currency']) && $_SESSION['switched_currency'] != $_SESSION['currency'] ) {
   $_SESSION['currency'] = $_SESSION['switched_currency'];
}
?>

 

Best Regards,

Star Network.

Edited by starnetwork
text fix
Link to comment
Share on other sites

  • 4 years later...
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated