Jump to content

Page doesn´t switch languages, but loads the right lang with browser preset.


freakstyling

Recommended Posts

Hi again very helpful people!

 

I´ve got my page set up for switching between English and Spanish. The problem is I can´t switch the languages on the page itself. Only when I use IE and Mac, it goes to the Spanish version. With the rest, Chrome, Firefox, it goes to the English version.

 

I´m thinking it´s maybe a preset, maybe something to do with $_SESSION . When I press the button to switch it gives the right link, but doesn´t change language...

 

Hope I made it clear more or less, otherwise ask me...

 

maybe something to do with this...?

 

 

<?php

session_start();

 

 

 

$_rootPath = dirname(__FILE__); // Root path per als includes / require's

 

$_version = 22; // Versió de la pàgina, força que s'hagin de recarregar les imatges que estiguin en cache.

 

$_availableLanguages = array('es', 'en'); // Idiomes disponibles

 

$_picturesMediaPath = "pictures/";

 

require_once($_rootPath . '/lib/db.php');

require_once($_rootPath . '/lib/lang.utils.php');

 

 

 

if( isset($_GET['lang']) && languageIsAvailable($_GET['lang']) )

{

$_lang = $_GET['lang'];

$_SESSION['lang'] = $_lang;

}

else if( isset($_SESSION['lang']) )

{

$_lang = $_SESSION['lang'];

}

else

{

$l = getPreferredUserLanguage();

if($l) $_lang = $l;

}

 

 

 

 

// Clear cache

Header("Cache-control: private, no-cache");

Header("Expires: Mon, 26 Jun 1997 05:00:00 GMT");

Header("Pragma: no-cache");

 

 

 

?>

 

 

or this...?

 

 

<?php

 

/* REQUIREMENTS

$_availableLanguages array of all available languages.

*/

 

 

function languageIsAvailable($l)

{

global $_availableLanguages;

 

return ( in_array($l, $_availableLanguages) );

}

 

 

function getPreferredUserLanguage()

{

if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) )

{

$acceptedlanguages = strtolower( str_replace(' ', '', $_SERVER["HTTP_ACCEPT_LANGUAGE"]) );

$acceptedlanguages = explode( ",", $acceptedlanguages );

 

foreach ( $acceptedlanguages as $l )

{

$aLanguage = substr( $l, 0, 2 );

if ( languageIsAvailable($aLanguage) )

{

return $aLanguage;

}

}

}

 

return false;

}

 

?>

 

 

Thanks again everybody for your help. You´re great!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.