Jump to content

Language detection and switching


Rhialto

Recommended Posts

Hello,

 

I will tell you whant I want to do first and what I've done so far and hopefuly someone will be able to help.

 

What I hope for is to have one unique index.php that will work everywhere.  I don't mind if I have to modify each and add path where it is located since this won't happen often.

 

It needs to take car of preferred language and if someone land in a directory, I want it to load the index.php there.

 

I also look to not expose hidden URL in the browser URL box.  See *note 1 below.

 

My directory structure is as follow:

 

/index.php

/about/index.php

/blahblah/index.php

 

The current index.php I'm trying to build looks like this:

 

<?php
    if (preg_match('/^www.(.+)$/i', $_SERVER['HTTP_HOST'], $matches)) {
      header("Status: 301 Move permanently", false, 301);
      header('Location: http://'.$matches[1].$_SERVER['REQUEST_URI']);
      exit;
    }

    session_start();
    $Lang = $_SESSION['lang'];
    
    if (!isset($Lang))
         {
        if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
            {
            $Lang = explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
            $Lang = strtolower(substr(chop($Lang[0]),0,2));
            if ($Lang !== 'fr' && $Lang !== 'en')
                { 
                $Lang = "en";
                }
            }
        else
            {
      $Lang = "en";
            }
        $_SESSION['lang'] = $Lang;
        }

    include $_SERVER["DOCUMENT_ROOT"] . ("/index_" . $_SESSION['lang'] . ".php");
?>

 

First I remove "www." in the address when applies.  Not a must but I like that.

 

Then I check if Language is already set, if not I detect and set it for next pages.

 

I finally load the proper page according to language with an "include" so index_en.php remain secret (sort of).

 

*note 1: all my index_xx.php pages in every directory have this:

<script language="javascript" type="text/javascript">
        if (self == top) 
        top.location.href="directory/index.php";
    </script>

 

 

When in the root, it seems to work and I get the proper page but if I place this in /about/ then when I tried to load /about/index.php it doesn't work.  I think the include on the last line could be the problem.

 

I'm either close or far from it, I don't know.

 

Thank you.

Link to comment
Share on other sites

I came too late, cannot edit.  :-\

 

Well I think I found out what's happening.

 

The fact that the index.php have an include instead of loading index_xx.php, it triggers the javascript I added (look 1st post) to prevent visitor to view index_xx.php pages.

 

Any suggestion?

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.