Jump to content

Add Language Selector In Client Side


Blesta Addons

Recommended Posts

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

Link to comment
Share on other sites

you can use my own toolbox plugin .

 

 

Add Custom html
simple
Pages : All Client Pages
Markup to set in : before the /body tag
HTML 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 JS
simple
Pages : All Client Pages
Markup to set in : before the /body tag
JS 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

Link to comment
Share on other sites

  • 1 month later...
  • 8 months later...

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.

Link to comment
Share on other sites

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');
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 .

Link to comment
Share on other sites

I got this working with Tyson and Naja7Host's code but I can't seem to get the cookie to set :P

<?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>
Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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>
Link to comment
Share on other sites

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')
Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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.

Loading...
×
×
  • Create New...