0

I'm implementing a PHP function to translate my website based on HTTP_ACCEPT_LANGUAGE, but also by user choice:

<?php 
if (!isset($language)) {
$language = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
$language = strtolower(substr(chop($language[0]),0,2));
}

include("functions/content_".$language.".php"); 
?>

User choice comes when the user clicks a link in the navbar, like this:

<ul>
<li><a href="portfolio.php?language=es">Español</a></li>
<li><a href="portfolio.php?language=en">Enshlish</a></li>
<li><a href="portfolio.php?language=fr">Français</a></li>
</ul>

Problem is I can't get to change the value of $language with the hrefs, it always self-defines as "es" (my browser is set to Spanish).

1
  • 1
    try $language = $_GET['language']; (register_globals is probably off) Commented Oct 13, 2013 at 16:46

2 Answers 2

1

You may try this

$language = isset($_GET['language']) ? $_GET['language'] : false;
if(!$language) {
    $language = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
    $language = strtolower(substr(chop($language[0]),0,2));
}

Also, you can use a function (a helper function), like

function setlanguage()
{
    $language = isset($_GET['language']) ? $_GET['language'] : false;
    if(!$language) {
        $language = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
        $language = strtolower(substr(chop($language[0]),0,2));
    }
    return $language;
}

Use it like

$language = setlanguage();
include("functions/content_".$language.".php");
Sign up to request clarification or add additional context in comments.

2 Comments

i like to use if(isset($_GET['language'])) so the page doesn't generate an apache error every time it loads without the $_GET var set
@JoeT, It's better now, IMO.
1

are you getting the value of $language before checking whether it's set? With register_globals off you need to get it from the $_GET variable to get it from the URL:
if(isset($_GET['language'])){ $language = $_GET['language']; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.