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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • 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.