Jump to content

Define $session->language == 'var'


buothz
 Share

Recommended Posts

I've already problem with multilanguages...

I wanna start with a NON default lang...

it is possibile define $session->language == 'myvar' at start?ù

When I click to eng language become default?

$language = '';
if($input->get->language) {
// user clicked on a link to set the language
 $name = $sanitizer->pageName($input->get->language);
 $language = $languages->get($name);
 if($language) $session->language = $language->name;
}
if($session->language) {
 // language is defined in user's session
 $user->language = $languages->get($session->language);
 $language = $languages->get($session->language);
...

If I change processwire/access/users/edit/ default to mylang, language system don't work...

thank u...

Link to comment
Share on other sites

$user->language = $languages->get('de'); 
$user->language = $languages->get('de'); 

does not solve the problem, I just change some of the text while the controlled with

if ($session->language == 'it') {...

remains in the default language (eng).

// LANGUAGES
//$language = '';
$user->language = $languages->get('it'); //OK! Only a part become not default lang...
if($input->get->language) {
// user clicked on a link to set the language
$name = $sanitizer->pageName($input->get->language);
$language = $languages->get($name);
if($language) $session->language = $language->name;
//$user->language = $languages->get($session->language);
}
if($session->language) {
// language is defined in user's session
$user->language = $languages->get($session->language);
$language = $languages->get($session->language);
//if($session) $language->name = $session->language;
//$user->language = $languages->get($session->language);
}
//LANGUAGES END

where's the problem?

thank u in advice...

Link to comment
Share on other sites

This looks like it should work to me. Just make sure you are executing all of this before you are starting output. If you find it's still not working, examine what values are getting thrown around in there. For instance,

if($session->language) {
 echo "<p>Language is: $session->language</p>";
}

Likewise, make sure that the names like 'it' are consistent with the actual page name of the language.

  • Like 1
Link to comment
Share on other sites

This looks like it should work to me. Just make sure you are executing all of this before you are starting output. If you find it's still not working, examine what values are getting thrown around in there. For instance,

if($session->language) {
echo "<p>Language is: $session->language</p>";
}

Likewise, make sure that the names like 'it' are consistent with the actual page name of the language.

thank u but i've tried all methods and nothing to do...

If i change user language admin or guest (1010 = it | 1080 = eng/default) to 1010 system make this problem:

- part of the text it's ok

- part of the text remain default while I press this button

if ($session->language == 'it') {
 //italiano
 echo '<li><a href="./?language=en"><img src="/img/gb.png" alt="English Language"></a></li>';

 } else {
 echo '<li><a href="./?language=it"><img src="/img/it.png" alt="Italian Language"></a></li>';
 }

when I click the flag (made by me) it's all ok...

first

echo "User Language: ".$user->language;
echo "Session Language: ".$session->language;
echo "Language: ".$language;
echo "Language name: ".$language->name;

are ALL blank (null i think...) exept $user->language, 1008 if i set default and 1010 if i set it...

If i don't change admin/guest lang (1008 -> default) are all blank but system go... problem it that the only info that system has is "$user->language = 1010" and nothing other... session remain = null! language = null!

If i leave $user->language = 1008 all is ok, if $user->language = 1008 problem is while I press my lang flag and start

if($input->get->language) {
 $name = $sanitizer->pageName($input->get->language);
 $language = $languages->get($name);
 if($language) $session->language = $language->name;
}

I'm becoming crazy!!!

ALL PAGE LANG CODE

<?php
echo "User Language: ".$user->language;
echo "Session Language: ".$session->language;
echo "Language: ".$language;
echo "Language name: ".$language->name;
/*if ( $user->language == '1008') {
//$language = 'it';
$session->language = 'it';
}*/
/*if ( $user->language == '1010') {
//$language = 'it';
$session->language = 'it';
}*/
/*if($session->language) {
echo "
Language is: $session->language
";
}*/
//echo $session->language;
/**
?>
<ul id='topnav'><?php
// LANGUAGES
//$language = '1008'; //IT
//$language = '1008'; //EN
//$language = '';

//$user->language = $languages->get('it'); //soluzione proposta
if($input->get->language) {
// user clicked on a link to set the language
 $name = $sanitizer->pageName($input->get->language);
 //echo 'name: '.$name;
 $language = $languages->get($name);
 //echo 'lang: '.$language;
 if($language) $session->language = $language->name;
 /*echo 'lang name: '.$language->name;
 echo "User Language: ".$user->language;
 echo "CLICK!!!";
echo "Session Language: ".$session->language;
echo "Language: ".$language;
echo "Language name: ".$language->name;*/
 //$user->language = $languages->get($session->language);
}
if($session->language) {
 // language is defined in user's session
 $user->language = $languages->get($session->language);
 $language = $languages->get($session->language);
 /*echo "SESSION!!!";
 echo "User Language: ".$user->language;
echo "Session Language: ".$session->language;
echo "Language: ".$language;
echo "Language name: ".$language->name;*/
 //if($session) $language->name = $session->language;
 //$user->language = $languages->get($session->language);
}
//LANGUAGES END

/*if ($session->language == 'it') {
 $language = 'it';
 echo "if session-lang".$language;
} else {
 $language = '';
 echo "if session-lang".$language;
}*/
...
</ul>...
Link to comment
Share on other sites

I don't think that your code above will work because you are setting session values in the middle of output. Everything that you do with regard to getting/setting language needs to happen before you start outputting stuff. You'd do it like this:

<?php
if($input->get->language) {
 $name = $sanitizer->pageName($input->get->language);
 $language = $languages->get($name);
 if($language->id) {
  $session->languageName = $language->name;
  $user->language = $language;
 }
} else if($session->languageName) {
 $user->language = $languages->get($session->languageName); 
}
?>
<html>
<head>
...

I'm putting that <html> tag there just to show that you need to take care of the language getting/setting stuff before you start sending markup. More likely you would include('./head.inc'); or something like that.

Link to comment
Share on other sites

I don't think that your code above will work because you are setting session values in the middle of output. Everything that you do with regard to getting/setting language needs to happen before you start outputting stuff. You'd do it like this:

<?php
if($input->get->language) {
$name = $sanitizer->pageName($input->get->language);
$language = $languages->get($name);
if($language->id) {
$session->languageName = $language->name;
$user->language = $language;
}
} else if($session->languageName) {
$user->language = $languages->get($session->languageName);
}
?>
<html>
<head>
...

I'm putting that <html> tag there just to show that you need to take care of the language getting/setting stuff before you start sending markup. More likely you would include('./head.inc'); or something like that.

thank u for all the help, i've tried this version on the first row of head.inc but nothing to do... if i set in "processwire/access/users/edit/?id=..." not default language system don't go.

If i return with my old code (move to the start or head.inc) and I click my language flag icon

if ($session->language == 'it') {
 echo '<li><a href="./?language=en"><img src="/img/gb.png" alt="English Language"></a></li>';
  ...

that do

if($input->get->language) {
   // user clicked on a link to set the language
  $name = $sanitizer->pageName($input->get->language);
  $language = $languages->get($name);
  if($language) $session->language = $language->name;

   }

system go ALL ok!!!

When i chage user guest and admin I see:

...
Change: unset:tariffe
Change: unset:title
Change: unset:trasmissione
Change: language
Saved Page: /processwire/access/users/nissim/

gived to me when I change access -> user -> admin -> language?

The perfect solution it would be change index.php to http://MYURL/?language=it, is possibile? I've tried with .htacces and my server conf but I do not know it...

Link to comment
Share on other sites

I don't really understand any or all off what you wrote in this thread. Impossible to help.

Anyway. Are you aware that if a text isn't translated it will output the default language text instead? Just in case you maybe experience this and think it's wrong.

Link to comment
Share on other sites

I don't really understand any or all off what you wrote in this thread. Impossible to help.

Anyway. Are you aware that if a text isn't translated it will output the default language text instead? Just in case you maybe experience this and think it's wrong.

Sry my eng is bad :)

"The perfect solution it would be change index.php to http://MYURL/?language=it, is possibile? I've tried with .htacces and my server conf but I do not know it..." Now when visit my site guest go to http://MYURL/index.php, it is possible make default url http://MYURL/?language=it?

Thanks...

Link to comment
Share on other sites

"The perfect solution it would be change index.php to http://MYURL/?language=it, is possibile? I've tried with .htacces and my server conf but I do not know it..." Now when visit my site guest go to http://MYURL/index.php, it is possible make default url http://MYURL/?language=it?

You could certainly set your homepage template to redirect to that URL when the homepage is accessed without a language GET variable. But I think it would be much better to just assume that your 'default' language is 'it'. There is no rule that the default language has to be English or anything else. The only rule is that it just has to be named 'default' (in the admin), but it is there to represent whatever you want your default language to be. In your case, it sounds like you want your default language to be 'it' since you are wanting an access to /index.php to instead go to /index.php?language=it. So the proper way to do this is to use the default language as its intended. But if you are still wanting to do exactly what you asked, you would do this in your homepage template:

if(!$input->get->language) $session->redirect($page->url . '?language=it'); 
  • Like 1
Link to comment
Share on other sites

You could certainly set your homepage template to redirect to that URL when the homepage is accessed without a language GET variable. But I think it would be much better to just assume that your 'default' language is 'it'. There is no rule that the default language has to be English or anything else. The only rule is that it just has to be named 'default' (in the admin), but it is there to represent whatever you want your default language to be. In your case, it sounds like you want your default language to be 'it' since you are wanting an access to /index.php to instead go to /index.php?language=it. So the proper way to do this is to use the default language as its intended. But if you are still wanting to do exactly what you asked, you would do this in your homepage template:

if(!$input->get->language) $session->redirect($page->url . '?language=it'); 

thank u a lot to all people...

the last problem: I've just made this function on my mind but when I pass to ?language=en and I click another link in the menu return "currentpage?language=it" and system lost the change of the language >:D :'(

Link to comment
Share on other sites

the last problem: I've just made this function on my mind but when I pass to ?language=en and I click another link in the menu return "currentpage?language=it" and system lost the change of the language

This is where the session comes in. So if you are going to go this route, you'd want to add another condition to check that you haven't already stored a language in the session too:

if(!$input->get->language && !$session->languageName) $session->redirect($page->url . '?language=it'); 
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...