webix Posted December 12, 2016 Share Posted December 12, 2016 Hello Folks! I looked here in the forums about a way to get auto-translate working on WHMCS and i only found a hook based on ip address. For most of users, IP address is not accurate to get the correct language (example: switzerland has 3 languages: french, italian and german. A spanish living in UK). So, i focused on the browser language. Many people use their computers on their foreign language (browser included) but it's not a rule to follow. So, for those who want to get whmcs showed up with the correct language for the users, i developped the following hook based on another that i found on the whmcs blog for a very old version. Just create the file includes/hooks/switch-language.php and place the following code inside of it: <?php $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(strpos($_SERVER['REQUEST_URI'], $customadminpath) === false && !isset($_SESSION['switch-language']) && $_SESSION['uid'] == false) { $_SESSION['switch-language'] = true; // prevent from redirecting back again in this session // we will add more here.... } $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'] != '') $location .= '&'.$_SERVER['QUERY_STRING']; ob_clean(); header('location: '.$location); die(); } Once done your whmcs will switch language automatically right away. The hook is loaded automatically. This won't change the currency has it's a bit more invasive. I hope this will help you. 1 Quote Link to comment Share on other sites More sharing options...
LMG Posted May 17, 2017 Share Posted May 17, 2017 Hi, Would this work with the current version? Regards. 0 Quote Link to comment Share on other sites More sharing options...
funmaking Posted May 26, 2017 Share Posted May 26, 2017 Hello there, The code is working well in WHMCS 7 client area but there is a problem with the admin login. When I try to access to admin url, I get an error in the browser: Too many redirections.... This script add a infinite loop in the administrator url: ?language=spanish&language=spanish&language=spanish&language=spanish&language=spanish&amp;language=spanish This is the old post blog: https://blog.whmcs.com/?t=67990 Anybody has a script or hook that works ok? 0 Quote Link to comment Share on other sites More sharing options...
Remitur Posted October 8, 2017 Share Posted October 8, 2017 On 26/5/2017 at 11:06 PM, funmaking said: The code is working well in WHMCS 7 client area but there is a problem with the admin login. When I try to access to admin url, I get an error in the browser: Too many redirections.... This script add a infinite loop in the administrator url: ?language=spanish&language=spanish&language=spanish&language=spanish&language=spanish&amp;language=spanish I guess the issue is in this statement: strpos($_SERVER['REQUEST_URI'], $customadminpath) === false I think that checking $customadminpath is not a valid method, because: it may be undefined (if you have not a custom admin directory...) even if defined, it may be slightly different from REQUEST_URI So, the question is: how to check if a code is running in client area or in admin area? or even: how to make a hook which works in client area only? 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted October 8, 2017 Share Posted October 8, 2017 9 hours ago, Remitur said: how to make a hook which works in client area only? use a ClientAreaPage hook ? 1 Quote Link to comment Share on other sites More sharing options...
Remitur Posted October 9, 2017 Share Posted October 9, 2017 This version seems to be working fine (test it before using in production environment). Only issue: user is not able to switch language himself. Even if he select another language from the menu, client area will stay on his browser's language. <?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'] != '') $location .= '&'.$_SERVER['QUERY_STRING']; ob_clean(); header('location: '.$location); die(); } } ?> 1 Quote Link to comment Share on other sites More sharing options...
brian! Posted October 9, 2017 Share Posted October 9, 2017 perhaps a static variable so that the function is only called once? so after the language is set by the browser, you should then be able to change it via the language dropdown... 0 Quote Link to comment Share on other sites More sharing options...
tkalfaoglu Posted December 4, 2019 Share Posted December 4, 2019 I made a slight improvement to the above code to prevent the loops and forced language changes.. change the last lines to this: 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(); } } if the query string already contains a language= clause this prevents another one from being appended to it.. 2 Quote Link to comment Share on other sites More sharing options...
haroun Posted January 20, 2021 Share Posted January 20, 2021 i did use it amazing <3 0 Quote Link to comment Share on other sites More sharing options...
WebFanny Posted February 23, 2023 Share Posted February 23, 2023 This should be funcationality in WHMCS Core. As of today I think this hook is not working anymore. Is someone able to test this and provide a fix that works with WHMCS V8.6.1? Adding all info in this topic: the current (broken code) should be: ```<?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')=== false) { $location .= '&'.$_SERVER['QUERY_STRING']; ob_clean(); header('location: '.$location); die(); } } } ?> ``` 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.