0

I want to change the language without showing the url: http://myweb.com/?lan=AL or http://myweb.com/about-us?lan=AL

How can it be done in background, not to show in url. This is the code below.

require('_inc_lang/lan_en.php');
require('_inc_lang/lan_al.php');
require('_inc_lang/lan_de.php');

if(!isset($_SESSION['lan'])){
session_start();
}

    if(isset($_GET['lan'])){
    $_SESSION['lan'] = $_GET['lan'];
    }

    $lan = isset($_SESSION['lan']) ? $_SESSION['lan'] : 'al';
    switch ($lan) {
    case 'al':
    $TEXT = $TEXT_AL;
    break;
    case 'de':
    $TEXT = $TEXT_DE;
    break;
    case 'en':
    $TEXT = $TEXT_EN;
    break;
}
Riat Abduramani
  • 53
  • 3
  • 13

3 Answers3

2

You can do it based on the browser language

<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
    case "de":
        $TEXT = $TEXT_DE;
        break;
    case "en":
        $TEXT = $TEXT_EN;
        break;
    case "al":
        $TEXT = $TEXT_AL;
        break;        
    default:
        $TEXT = $TEXT_EN;
}
?>
Dieterg
  • 16,118
  • 3
  • 30
  • 49
  • Please don't. I cannot count the amount of times I've gotten annoyed at a website/application for being in Danish just because I have a Danish OS/browser, or let alone reside in Denmark. At *least* give me the option to change it. – h2ooooooo Feb 21 '14 at 14:04
  • 1
    Let me know one of your sites. My language is set to Klingon :) – Ne Ma Feb 21 '14 at 14:05
  • James Anderson on http://programmers.stackexchange.com/questions/101607/why-do-websites-have-to-ask-language-and-country-when-the-browser-can-tell-them basically lists why we reheally should not use the browsers language to auto set. Its massively annoying. – Ne Ma Feb 21 '14 at 14:15
  • @NeilMasters Everything on my computer is set in English. However, because I am located in Peru, Google insists on presenting all its pages in Spanish so they seem to be using ip-based detection instead of the http header (unless my isp changes headers without my knowledge of course...). So for a default - with the option to change it - this could work. – jeroen Feb 21 '14 at 14:38
1

Change from GET to POST.

<form method="post">
    ..button/select/whatever have you
</form>

if(isset($_POST) && /* sanitise */)
    $_SESSION['lan'] = $_POST['lan'];

Code needs a tidy up but you can do that yourself :) Is this what you are looking for?

Edit: Absolutely totally must use a link or you will explode? The following SO pages will magically show you how!

Use a normal link to submit a form

How to submit a form with JavaScript by clicking a link?

Community
  • 1
  • 1
Ne Ma
  • 1,719
  • 13
  • 19
  • Can it be done with Javascript, because the user should click for a language. ENGLISHT – Riat Abduramani Feb 21 '14 at 14:06
  • I think it is not working. Better solution is to use javascript. If you can help me with that, it will be more easy for me. When I will click the link for language, then the website will turn to that language. – Riat Abduramani Feb 21 '14 at 15:44
1

You're already copying the language choice to the session, so if it is found in the URL, just location: to the version without the URL:

if(isset($_GET['lan'])){
    $_SESSION['lan'] = $_GET['lan'];
    header("Location: ".$_SERVER['SCRIPT_URI']);
}

Edit: note that I added variable $_SERVER['SCRIPT_URI'] instead of hard coded location; now it will work regardless of where it is called (SCRIPT_URI will give you server/page without query string)

Digital Chris
  • 6,177
  • 1
  • 20
  • 29
  • As I understand it, OP wants to remove the use of the lan from the URI. Your solution would not remove it but would redirect after setting. – Ne Ma Feb 21 '14 at 14:17
  • @NeilMasters Looking at the question, it repeatedly mentions not wanting the query string in the URL, nothing about removing functionality from the code. A form post solution introduces other complications, like the inability to link from another site to a specific language version. – Digital Chris Feb 21 '14 at 14:19
  • you raise a valid point and my hat has been tipped. OP will have to utilise both methods if he wants users to link to language based content. [edit] Cant @your name :( – Ne Ma Feb 21 '14 at 14:23