Hello,

I have this very very strange bug using locale. What I'm trying to achieve is a cookie based snippet to make a language fallback redirect when a user lands (again) on the root page at / .

I create on every page request a cookie with the current page language. On the next page request this cookie (language of previous page) will be checked: if the user is at the front page, this should do a redirect to /nl or /fr for example. This redirect only happens on the landing page, not on any other page like /fr/content/somewhere/in/french .

The problem now is, when a user is at /nl or /fr root, a cookie is created as well. If the user switches to the default language (english for example) using the language switcher block, the page will be automatically redirected back to the /nl or /fr page.

So I created a snippet that fills the cookie with the default language when the user is at /nl or /fr . _> if(($language->language != language_default('language')) AND ($_COOKIE['usr-language'] != language_default('language')) AND ($is_front) )

So the weirdest issue now is that when I add this last snippet (to change the cookie value when the user is at /nl or /fr) , the user located at /fr/content/somewhere/in/french has its language cookie set to 'fr'. But when he navigates from there directly to the root / , the cookie value will become 'en' WITHOUT the cookie being set somewhere to 'en' before. I even tried it by adding a break; statement right after printing the cookie. So because the cookie mysteriously will be set to 'en' , the redirect will not happen.

This means that it will just open the default landing page in English. So I have two solutions, but they don't work together. It's either or.

When I comment out the second cookie set, the first functionality will work again (redirect from / to /fr coming from /fr/content/somewhere/in/french ). This is funny because if you are located at /fr/content/somewhere/in/french , the second cookie isn't even read because of the if statement ( $is_front ) .

Anyhow, this is the raw production snippet I placed in page.tpl.php. I tried many many options over the past days.

Default language: <?php print language_default('language'); ?>
<br> Cookie language 1: <?php print $_COOKIE['usr-language']; 
//if(request_uri() == '/') break;
?> - 
<br> page language: <?php print $language->language; ?>
<br> URI: <?php print request_uri(); ?> 

<?php

if(!isset($_COOKIE['usr-language'])) setcookie('usr-language', $language->language, time() + 3600 * 24 * 180);

	if((request_uri() == '/')
	   ){
			print '<br>-- phase one --';
			drupal_goto($path = $_COOKIE['usr-language']);
			break;
		}
		else{
			print '<br>-- phase two --';
			//setcookie('usr-language', '', time() - 3600 * 24);
			//setcookie('usr-language', $language->language, time() + 3600 * 24 * 180);
		}
    
?>
<br> Cookie language 2: <?php print $_COOKIE['usr-language']; ?><br>
<?php
    
	if(($language->language != language_default('language')) AND
	  ($_COOKIE['usr-language'] != language_default('language')) and 
	   ($is_front) ){
			print 'FRONT';
			
			//set the cookie to the default language to prevent being redirected back to the translated page.
			setcookie('usr-language', language_default('language'), time() + 3600 * 24 * 180);
		}
?>

<br> Cookie language 3: <?php print $_COOKIE['usr-language']; ?><br>

I'm sorry the snippet isn't all very clean written.

Does anyone have an idea? Any help is highly appreciated.

I also find no information about cookies to set a language fallback. I find lots of questions, but no answers.

Cheers,
Danny

Comments

danny_joris’s picture

This might be a easier way to recreate this issue. Should I use something else than setcookie() ?

<br> 1) Cookie language before setcookie(): <?php print $_COOKIE['usr-language']; 

	setcookie('usr-language', $language->language, time() + 3600 * 24 * 180);
?>
<br> 2) Cookie language after setcookie(): <?php print $_COOKIE['usr-language']; ?><br>

The results of both 1 and 2 are always the same.

- when I load a page, bot 1 and 2 display the current language. 1 doesn't display the result of the previous page language.

- When I first load /fr or /nl and I then load / , both 1 and 2 display the previous language ( nl or fr ). When I reload the root page / , they both will output 'en'.

Any ideas? I'm a bit confused about this.

kscheirer’s picture

I didnt read through your whole issue, but I've seen something similar before. Try passing in a path of '/' when using setcookie - otherwise the url thats writing the cookie will be used.

Also, $_COOKIE will not have a new value, even right after a call to setcookie(). $_COOKIE will be refreshed at the next page load. Check the comments on php.net/setcookie - some folks there worked around the issue by writing to $_COOKIE directly right after calling setcookie(), like

  setcookie('usr-language', $language->language, time() + 3600 * 24 * 180);
  $_COOKIE['usr-language'] = $language->language; 
ellenvdk’s picture

yes kscheirer, that was my problem too.
I set cookies without using the path.
You can see your cookies in Firefox at any time. So I had 2 cookies with the name 'usr-language', one for the path '/' and for another path '/fr'.
There can exist 2 cookies with the same name, if they have a different path.

So set tour cookies like this :

setcookie('usr-language', $language->language, time() + 3600 * 24 * 180, '/');

Thanks Danny_Joris for your code, you set me on the right way!
Greetz,
Ellen

Status: Active » Closed (outdated)

Automatically closed because Drupal 6 is no longer supported. If the issue verifiably applies to later versions, please reopen with details and update the version.