Jump to content

Another change langauge based on browser


Evonets

Recommended Posts

Hello,

 

I have been bussy creating my whmcs website, but now i am looking into changing the langauge based on the browser langauge.
I have the code up and running but there is 1 problem, the Android app isnt functioning anymore.

The hook is breaking the /includes/api.php end-point with a redirect loop adding language=english to the URL.
Does anyone have the correct hook?

 

<?php

add_hook('ClientAreaPage', 1, switch_language($vars));

function switch_language($vars) {
 
$country_to_language = array(
'default' => 'english',
'AR' => 'arabic',
'AR-DZ' => 'arabic',
'AR-BH' => 'arabic',
'AR-EG' => 'arabic',
'AR-IQ' => 'arabic',
'AR-JO' => 'arabic',
'AR-KW' => 'arabic',
'AR-LB' => 'arabic',
'AR-LY' => 'arabic',
'AR-MA' => 'arabic',
'AR-OM' => 'arabic',
'AR-QA' => 'arabic',
'AR-SA' => 'arabic',
'AR-SY' => 'arabic',
'AR-TN' => 'arabic',
'AR-AE' => 'arabic',
'AR-YE' => 'arabic',
'AZ' => 'azerbaijani',
'CA' => 'catalan',
'ZH' => 'chinese',
'ZH-HK' => 'chinese',
'ZH-CN' => 'chinese',
'ZH-SG' => 'chinese',
'ZH-TW' => 'chinese',
'HR' => 'croatian',
'CS' => 'czech',
'DA' => 'danish',
'NL' => 'dutch',
'NL-BE' => 'dutch',
'EN' => 'english',
'EN-AU' => 'english',
'EN-BZ' => 'english',
'EN-CA' => 'english',
'EN-IE' => 'english',
'EN-JM' => 'english',
'EN-NZ' => 'english',
'EN-PH' => 'english',
'EN-ZA' => 'english',
'EN-TT' => 'english',
'EN-GB' => 'english',
'EN-US' => 'english',
'EN-ZW' => 'english',
'ET' => 'estonian',
'FA' => 'farsi',
'FR' => 'french',
'FR-BE' => 'french',
'FR-CA' => 'french',
'FR-FR' => 'french',
'FR-LU' => 'french',
'FR-MC' => 'french',
'FR-CH' => 'french',
'DE' => 'german',
'DE-AT' => 'german',
'DE-DE' => 'german',
'DE-LI' => 'german',
'DE-LU' => 'german',
'DE-CH' => 'german',
'HE' => 'hebrew',
'HU' => 'hungarian',
'IT' => 'italian',
'IT-CH' => 'italian',
'MK' => 'macedonian',
'NO' => 'norwegian',
'NB' => 'norwegian',
'NN' => 'norwegian',
'PT-BR' => 'portuguese-br',
'PT' => 'portuguese-pt',
'RO' => 'romanian',
'RO-MO' => 'romanian',
'RU' => 'russian',
'RU-MO' => 'russian',
'ES' => 'spanish',
'ES-AR' => 'spanish',
'ES-BO' => 'spanish',
'ES-CL' => 'spanish',
'ES-CO' => 'spanish',
'ES-CR' => 'spanish',
'ES-DO' => 'spanish',
'ES-EC' => 'spanish',
'ES-SV' => 'spanish',
'ES-GT' => 'spanish',
'ES-HN' => 'spanish',
'ES-MX' => 'spanish',
'ES-NI' => 'spanish',
'ES-PA' => 'spanish',
'ES-PY' => 'spanish',
'ES-PE' => 'spanish',
'ES-PR' => 'spanish',
'ES-ES' => 'spanish',
'ES-UY' => 'spanish',
'ES-VE' => 'spanish',
'SV' => 'swedish',
'SV-FI' => 'swedish',
'SV-SV' => 'swedish',
'TR' => 'turkish',
'UK' => 'ukranian',
// NOTE: You can add more below
);

if(!isset($_SESSION['switch-language']) && $_SESSION['uid'] == false) 
    {
        $_SESSION['switch-language'] = true; // prevent from redirecting back again in this session
    }

$current_country = strtoupper(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));

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

if(!isset($_SESSION['Language']) || $_SESSION['Language'] != $language) {
       $location = '?language='.$language;
       if($_SERVER['QUERY_STRING'] != '' && strpos($_SERVER['QUERY_STRING'],'?language')===0) {
                $location .= '&'.$_SERVER['QUERY_STRING'];
                ob_clean();
                header('location: '.$location);
                die();
       }
}
 

?>

 

 

 

 

Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • 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