Blesta Addons Posted April 12, 2015 Report Posted April 12, 2015 Hello all, this thread will show you how to add a langaueg selection for clients/visitors . first thing , the admin tools plugins should be installed and activated to get this working . now what we should add is the language selector in the cleint area , something like <form method="GET" class="form-inline" id="change_language"> <label for="set_language">Language:</label> <select name="set_language" class="form-control input-sm" id="set_language"> <option value="en_us">English</option> <option value="fr_fr">Français</option> <option value="es_es">Spanish</option> <option value="ar_sa">Arabic</option> </select> </form> you can add your own html markup but you should conserve only the selector name "set_language" . didn't change GET with POST form . also add the jquery code inthe footer of the view // Process change Language request $("#set_language").change(function() { $(this).closest("form").attr('action', window.location.href); $(this).closest("form").submit(); }); note any language should be exist in your blesta installation . now the clients/visitor can change language in the client portal/login/order page . best regards a.daniello and PauloV 2 Quote
Blesta Addons Posted April 12, 2015 Author Report Posted April 12, 2015 you can use my own toolbox plugin . Add Custom htmlsimplePages : All Client PagesMarkup to set in : before the /body tagHTML To Add : <form method="GET" class="form-inline text-center" id="change_language"> <label for="set_language">Language:</label> <select name="set_language" class="form-control input-sm" id="set_language"> <option value="en_us">English</option> <option value="fr_fr">Français</option> <option value="es_es">Spanish</option> <option value="ar_sa">Arabic</option> </select> </form> save . Add Custom JSsimplePages : All Client PagesMarkup to set in : before the /body tagJS To Add : // Process change Language request $("#set_language").change(function() { $(this).closest("form").attr('action', window.location.href); $(this).closest("form").submit(); }); save . you are done Michael and a.daniello 2 Quote
JDG Posted April 20, 2015 Report Posted April 20, 2015 Hi I try to make it work but is not working for me I follow the steps but maybe is something else I need todo beacuse once the selector is available is not changing the language. Thanks for your help. Quote
Blesta Addons Posted April 21, 2015 Author Report Posted April 21, 2015 have you adminn tools plugin installed ? http://www.blesta.com/forums/index.php?/topic/2925-admin-tools-plugin/ Quote
Blesta Addons Posted May 28, 2015 Author Report Posted May 28, 2015 I have missed a javascript code from my tuto to make it wokring . i will update this later this weekend . Quote
Blesta Addons Posted June 1, 2015 Author Report Posted June 1, 2015 hi any updates i have updated the first post . check it now . PauloV 1 Quote
deydod Posted February 10, 2016 Report Posted February 10, 2016 In addition to naja7host I've added some code so the selected language stays selected: <?php if (!isset($this->Session)) Loader::loadComponents($this, array("Session")); ?> <form method="GET" class="form-inline text-center"> <select name="set_language" class="form-control input-sm" id="set_language"> <option value="en_us" <?php if ($this->Session->read('language') == 'en_us') echo 'selected' ?>>English</option> <option value="fr_fr" <?php if ($this->Session->read('language') == 'fr_fr') echo 'selected' ?>>French</option> </select> </form> <script type="text/javascript"> jQuery(document).ready(function() { jQuery("#set_language").change(function() { jQuery(this).closest("form").attr('action', window.location.href); jQuery(this).closest("form").submit(); }); }); </script> Just in case anyone needs this. Tyson, Blesta Addons and PauloV 3 Quote
Tyson Posted February 11, 2016 Report Posted February 11, 2016 In addition to naja7host I've added some code so the selected language stays selected: <?php if (!isset($this->Session)) Loader::loadComponents($this, array("Session")); ?> <form method="GET" class="form-inline text-center"> <select name="set_language" class="form-control input-sm" id="set_language"> <option value="en_us" <?php if ($this->Session->read('language') == 'en_us') echo 'selected' ?>>English</option> <option value="fr_fr" <?php if ($this->Session->read('language') == 'fr_fr') echo 'selected' ?>>French</option> </select> </form> <script type="text/javascript"> jQuery(document).ready(function() { jQuery("#set_language").change(function() { jQuery(this).closest("form").attr('action', window.location.href); jQuery(this).closest("form").submit(); }); }); </script> Just in case anyone needs this. It's a nice addition. I just hate to see templates making calls to the session. I think you can use the configured language, no? // User's language $language = Configure::get('Blesta.language'); Michael and PauloV 2 Quote
deydod Posted February 11, 2016 Report Posted February 11, 2016 Hi Tyson // User's language $language = Configure::get('Blesta.language'); Only returns the the blesta default language when you are not authenticated. Maybe I am wrong but in my tests it always returns "en_us". Quote
Tyson Posted February 11, 2016 Report Posted February 11, 2016 Hi Tyson // User's language $language = Configure::get('Blesta.language'); Only returns the the blesta default language when you are not authenticated. Maybe I am wrong but in my tests it always returns "en_us". The company language will be the default when not logged in, and en_us will be the default if no other language is known. I haven't looked at the AdminTools plugin, but I think it should probably be setting the language like this: // Selected language $language_code = 'en_us'; // Set the language Configure::set('Blesta.language', $language_code); Language::setLang(Configure::get('Blesta.language')); Then the configuration value I mentioned above would be accurate. The system also uses this configuration value for various actions/queries and does not look at the session, so I think it's important that it be updated. Michael 1 Quote
Blesta Addons Posted February 11, 2016 Author Report Posted February 11, 2016 The company language will be the default when not logged in, and en_us will be the default if no other language is known. I haven't looked at the AdminTools plugin, but I think it should probably be setting the language like this: // Selected language $language_code = 'en_us'; // Set the language Configure::set('Blesta.language', $language_code); Language::setLang(Configure::get('Blesta.language')); Then the configuration value I mentioned above would be accurate. The system also uses this configuration value for various actions/queries and does not look at the session, so I think it's important that it be updated. impossible to make it work without sessions . normally blesta load the default language, and there no option to allow guests to change language of display . so my simple solution was to add a language selector and store the selected language in sessions , then read it from sessions and set the language . Quote
Michael Posted February 12, 2016 Report Posted February 12, 2016 I got this working with Tyson and Naja7Host's code but I can't seem to get the cookie to set <?php // Selected language $language_code = $_GET['value']; if( $language_code != '' ){ Configure::set('Blesta.language', $language_code); Language::setLang(Configure::get('Blesta.language')); setcookie($set_language, $language_code, time() + (86400 * 30), "/"); // 86400 = 1 day }else{ $language_code = "en_us"; } ?> <form method="GET" class="form-inline text-center"> <select name="set_language" class="form-control input-sm" id="set_language"> <option value="en_us" <?php if ( $_COOKIE["set_language"] == 'en_us' ){ echo 'selected'; } ?>>English</option> <option value="fr_fr" <?php if ( $_COOKIE["set_language"] == 'fr_fr' ){ echo 'selected'; } ?>>French</option> </select> </form> <script type="text/javascript"> jQuery(document).ready(function() { jQuery("#set_language").change(function() { jQuery(this).closest("form").attr('action', window.location.href); jQuery(this).closest("form").submit(); }); }); </script> Quote
deydod Posted February 12, 2016 Report Posted February 12, 2016 Licensecart your code should be: <?php // Selected language $language_code = $_GET['set_language']; if( $language_code != '' ){ Configure::set('Blesta.language', $language_code); Language::setLang(Configure::get('Blesta.language')); setcookie('set_language', $language_code, time() + (86400 * 30), "/"); // 86400 = 1 day }else{ $language_code = "en_us"; } ?> <form method="GET" class="form-inline text-center"> <select name="set_language" class="form-control input-sm" id="set_language"> <option value="en_us" <?php if ( $_COOKIE["set_language"] == 'en_us' ){ echo 'selected'; } ?>>English</option> <option value="fr_fr" <?php if ( $_COOKIE["set_language"] == 'fr_fr' ){ echo 'selected'; } ?>>French</option> </select> </form> <script type="text/javascript"> jQuery(document).ready(function() { jQuery("#set_language").change(function() { jQuery(this).closest("form").attr('action', window.location.href); jQuery(this).closest("form").submit(); }); }); </script> also this part needs to be changed: }else{ $language_code = "en_us"; } to: }else{ $language_code = Configure::get('Blesta.language'); } This way it will get the default language that is set in admin. (can be different than en_us). activa and Michael 2 Quote
activa Posted February 12, 2016 Report Posted February 12, 2016 i go with session rather than cookies . Quote
Michael Posted February 12, 2016 Report Posted February 12, 2016 Licensecart your code should be: <?php // Selected language $language_code = $_GET['set_language']; if( $language_code != '' ){ Configure::set('Blesta.language', $language_code); Language::setLang(Configure::get('Blesta.language')); setcookie('set_language', $language_code, time() + (86400 * 30), "/"); // 86400 = 1 day }else{ $language_code = "en_us"; } ?> <form method="GET" class="form-inline text-center"> <select name="set_language" class="form-control input-sm" id="set_language"> <option value="en_us" <?php if ( $_COOKIE["set_language"] == 'en_us' ){ echo 'selected'; } ?>>English</option> <option value="fr_fr" <?php if ( $_COOKIE["set_language"] == 'fr_fr' ){ echo 'selected'; } ?>>French</option> </select> </form> <script type="text/javascript"> jQuery(document).ready(function() { jQuery("#set_language").change(function() { jQuery(this).closest("form").attr('action', window.location.href); jQuery(this).closest("form").submit(); }); }); </script> also this part needs to be changed: }else{ $language_code = "en_us"; } to: }else{ $language_code = Configure::get('Blesta.language'); } This way it will get the default language that is set in admin. (can be different than en_us). Doesn't work for some reason either doesn't set the cookie on my Safari? <?php // Selected language $language_code = $_GET['set_language']; if( $language_code != '' ){ Configure::set('Blesta.language', $language_code); Language::setLang(Configure::get('Blesta.language')); setcookie('set_language', $language_code, time() + (86400 * 30), "/"); // 86400 = 1 day }else{ $language_code = Configure::get('Blesta.language'); } ?> <form method="GET" class="form-inline text-center"> <select name="set_language" class="form-control input-sm" id="set_language"> <option value="en_us" <?php if ( $_COOKIE["set_language"] == 'en_us' ){ echo 'selected'; } ?>>English</option> <option value="fr_fr" <?php if ( $_COOKIE["set_language"] == 'fr_fr' ){ echo 'selected'; } ?>>French</option> </select> </form> <script type="text/javascript"> jQuery(document).ready(function() { jQuery("#set_language").change(function() { jQuery(this).closest("form").attr('action', window.location.href); jQuery(this).closest("form").submit(); }); }); </script> Quote
deydod Posted February 12, 2016 Report Posted February 12, 2016 Well I am using the sessions way, that's the reason I havent tested this. But in my opinion the part that grabs the language and sets the cookie should be before outputing any content. That's probably the reason why its not working correctly. Quote
Tyson Posted February 12, 2016 Report Posted February 12, 2016 impossible to make it work without sessions . normally blesta load the default language, and there no option to allow guests to change language of display . so my simple solution was to add a language selector and store the selected language in sessions , then read it from sessions and set the language . You should still store it in the session. What I am suggesting is that when you read it from the session, you set the configuration value afterward. // Selected language $language_code = $this->Session->read('language'); # # TODO: ensure the $language_code is a valid language in the system, # otherwise fall back to the default language # // Set the language Configure::set('Blesta.language', $language_code); Language::setLang(Configure::get('Blesta.language')); The end result is that these values are consistent: $this->Session->read('language') === Configure::get('Blesta.language') Blesta Addons and Michael 2 Quote
Blesta Addons Posted February 15, 2016 Author Report Posted February 15, 2016 You should still store it in the session. What I am suggesting is that when you read it from the session, you set the configuration value afterward. // Selected language $language_code = $this->Session->read('language'); # # TODO: ensure the $language_code is a valid language in the system, # otherwise fall back to the default language # // Set the language Configure::set('Blesta.language', $language_code); Language::setLang(Configure::get('Blesta.language')); The end result is that these values are consistent: $this->Session->read('language') === Configure::get('Blesta.language') added to the next release of admin tools plugins. PauloV 1 Quote
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.