Jump to content

Language switch signal by url param


rushy
 Share

Recommended Posts

Hello. I'm making a site based on the multi language example provided at installation and it works well except for the setting of the html lang tag below. This site has Italian as the default so I had to change the language pack following instruction I found on here. It seems to work but the country code below is 'home' when Italian is selected and 'en' when English is selected.

<html lang="<?php echo _x('it', 'HTML language code') ?>">

You see I hard wired it for now for the default language.

The code below is the language switcher PHP code basically taken from the installation example but with the default set to 'it' not 'home' !

			foreach($languages as $language) {
				if(!$page->viewable($language)) continue; // is page viewable in this language?
				if($language->id == $user->language->id) {
					$sel = "<span class='glyphicon glyphicon-ok'></span>";
					echo "<li class='current'>";
				} else {
					$sel = "";
					echo "<li>";
				}
				$url = $page->localUrl($language); 
				$hreflang = $homepage->getLanguageValue($language, 'name'); 
	   		if ($language->isDefault())  $hreflang = 'it'; // default is Italian
	   		$x = $url.'?lang='.$hreflang;				
			echo "<a hreflang='$hreflang' href='$x'>$sel $language->title</a></li>";
			}

You can see I added a url param ?lang= to pass the language selected. So in my case 2 links above are created with the lang param appended, and below is the code to extract the params. 

$q = $input->get('lang'); // retrieve value
$q = $sanitizer->text($q); // sanitize input as 1-line text
$lang = $sanitizer->entities($q);
?>
<!DOCTYPE html>
<html lang="<?php echo _x($lang, 'HTML language code') ?>">
<!-- <html lang="<?php echo _x('it', 'HTML language code') ?>"> -->
<head>
  <title><?php echo $title; ?></title>
  <meta charset="utf-8">

It works, the html lang= is now set correctly but I can't help feeling this is not a good way to do it. Could anyone tell me if this should be necessary?

Many thanks! Rushy

Link to comment
Share on other sites

You don't need to set GET parameters or URL segments. (which is not exactly the same b.t.w.)

  • Install LanguageSupportPageNames module and configure it for your needs.
  • go to home page and set names for the home page. Use 'it' for default language and 'en' for english. Don't use 'home'. Don't leave empty.
  • Access language code from $pages->get(1)->name for current user language and  $pages->get(1)->getLanguageValue($language, 'name') for specified language. Get the URLs with $pages->get(1)->localUrl($language)
  • Like 5
Link to comment
Share on other sites

  • 2 weeks later...

Hi @kixe

I try to do something similar, but at me don't works really well.

So, I using this for add the hreflang inside the <head> and this works well

<?php
$homepage = $pages->get('/');
foreach($languages as $language) {
	if(!$page->viewable($language)) continue;
	$url = $page->localHttpUrl($language);
	$hreflang = $homepage->getLanguageValue($language, 'name');
	echo "\n\t<link rel='alternate' hreflang='$hreflang' href='$url' />";
}
?>

But when I try to use inside the <html lang="<?php echo _x($hreflang, 'HTML language code') ?>"> 

or try your code 

<?php
	$leng = $pages->get(1)->getLanguageValue($language, 'name');
?>
<html lang="<?php echo _x($leng, 'HTML language code') ?>">

In all cases I have back only lang="en" And not show me other $languages.

Do you know way?

Thank you

 

 

Link to comment
Share on other sites


@MarcoPLY

Where did you set the vars $hreflang and $language? Maybe in the last turn of the foreach loop? If so, not a good idea.

The function _x() provides the option to translate a given string in a specific context. This is useful for hardcoded text strings in modules or templates. If you put $leng in quotes and translate this string again under SETUP > LANGUAGES > ANYLANGUAGE > Button: Find Files to translate it maybe will work but it is useless. You don't need _x()  here. Your variable $leng is already translated in the database.

The following code is enough and should work if you have properly implemented a language switcher which changes the language of the current user.

<? $lang = $pages->get(1)->getLanguageValue($user->language, 'name'); ?>
<html lang="<?php echo $lang ?>">

 

  • Thanks 1
Link to comment
Share on other sites

Thank you @kixe, yes sorry for the _x() I didn't know, thank you for your explanation.

I was try also without _x() and before didn't work. But Now I have understand my error, I didn't call ed $user->. Now when I switch between languages the tag lang change! because it recognizes the language that the user user has selected. Thank you again! 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

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