Jump to content

Language switcher using directory structure


e11world

Recommended Posts

Hi all,

Hi all,

I've been looking on the net for something like this and found a few things but because I'm a beginner still, I can't put it together.

I need to change the site's language based on the folder /en/ or /fr/ and the file names are always the same. The site is not big and this would work perfect.

 

I have this code in asp that does the job but not sure how to convert it into php:

A sample site using it is http://www.contacservices.com/fr/contactus.html but on home page, it's using index.html and index_fr.html because they're not in the /en/ and /fr/ folders

    <%
    Dim PATH_INFO
    PATH_INFO = Request.ServerVariables("PATH_INFO")
    If (Instr(PATH_INFO,"/fr/")>0) Then
       'Response.Write("FRENCH")
       PATH_INFO =Replace( PATH_INFO , "/fr/", "/en/")
    ElseIf (Instr(PATH_INFO,"/en/")>0) Then
       'Response.Write("ENGLSIH")
       PATH_INFO =Replace( PATH_INFO , "/en/", "/fr/")
    End If
    %>
    <%'Response.Write(Request.ServerVariables("PATH_INFO"))%>
    <div id="topslimbar"><a href="<%=PATH_INFO %>" target="_self" title="Passer au français">Français</a></div>

 

I also found this http://www.phpbuilder.com/tips/item.php?id=343 but I can't figure out why it's not working. Maybe I'm just not using it right.

This is another place I thought was related but I'm a newbie and can't figure it out http://www.phpfreaks.com/forums/index.php/topic,271750.msg1282931.html

 

Can you guys please help me?

 

In a typical PHP website the language switcher calls the appropriate file which holds the language variables. These variables are then used throughout the interface. I guess your directory structure for different languages is virtual, as having a physical one wouldn't make much sense in a web application, but it would make perfect sense in a static HTML website.

 

A quick solution to having virtual directories for each language is to append the language in a GET variable and create a virtual directory using htaccess. I'll give a basic example:

 

<?php
/*
Place here some code that deals with the language switch.
It can specify the language from a GET variable and/or cookies.
Check if the specified language is in your "safe list".
I'm assuming the language is returned in the $lang variable using the form: "en" and "fr"
*/
include("lang/$lang.php"); //change the interface language
?>
<a href="/en/index.html">English</a> | <a href="/fr/index.html">English</a>

 

The language files contain variables (or constants) with the interface strings. You can call those variable throughout the website, be it in menu items, content, messages, etc. An example of the fictional English language file:

<?php
$lang_hello = 'Hello guys';
$lang_menu = 'This is a menu item';
?>

 

The French language file would contain exactly the same variable names, but with the strings in the French language obviously.

 

You will also need htaccess to rewrite the URLs so you can use those easy ones and still GET the variables. The mos basic rules:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-z]+)/([a-z]+).html index.php?lang=$1&page=$2 [NC]

 

Remember that you will need to use absolute paths in all your links and calls to javascript/css/images, or the virtual directory structure will break everything.

 

Hope everything makes sense and is [even roughly] what you're seeking to achieve.

So what you need to do is figure out if you need:

domain.com/en/about.php

or

domain.com/fr/about.php

 

You need a flag for each language and then set a session var for whatever flag is clicked. Then you can:

domain.com/$language/about.php

 

 

HTH

Teamatomic

Thanks for the reply GuiltyGear but I don't want that solution. The site is static but I don't want to use variables because it's not what I imagine happening on the site.

teamatomic, your answer doesn't really tell me anything. I don't know much about php and your answer sums up my question and this domain.com/$language/about.php would be the only thing I can use but not sure how to do so.

Take a french flag and use it like this on your pages

<a href="./index.php?language=fr">img src="french_flag.png"></a>

Take a US or UK flag and use it the same way

<a href="./index.php?language=en">img src="english_flag.png"></a>

 

At the top of your pages

<?php
session_start();
if (isset($_GET['language'])
{$_SESSION['language']=$_GET['language'];}
else
{$_SESSION['language']='en';}//this makes your default language english
$language=$_SESSION['language'];

all your links can now use:
<a href="domain.com/<?php echo "$language"; ?>/about.php

HTH
Teamatomic

Well I tried your code tonight but that still doesn't actually change (refresh) the page into the french version of the site. Clicking on the flag adds the ?language=en and then I have to still click on the link (navigation) to go to that page now in the new language.

I found another sample but also not sure how to use this one either http://www.phpbuilder.com/tips/item.php?id=343&print_mode=1  :confused:

Please, I hope someone answer my question the way I wrote it  :(. I need it to do the same thing shown here http://www.contacservices.com/en/aboutus.html (when you click on the Français in the top right corner). It just changes the folder from en to fr and reloads/redirects the page. This works from any page except the home page which is because it's not within the en or fr folders.

Alight I found a solution!!! :D :D Basically converting the code I already had from the start from ASP to PHP:

 

I put the following code in the include folder and I named it hdr-ar.php

<?php
$path_info = $_SERVER['REQUEST_URI'];
if (strpos($path_info, "/ar/") > 0){
    echo "YOU ARE IN THE ARABIC SITE";
    $path_info = str_replace("/ar/", "/en/", $path_info);
}else if (strpos($path_info, "/en/") > 0){
    echo "YOU ARE IN THE ENGLISH SITE";
    $path_info = str_replace("/en/", "/ar/", $path_info);
}
echo '<br />';
echo $path_info;
?>
<!-- this is where the link will show -->
<div id="langbar"><a href="<?php echo $path_info; ?>" target="_self" title="Switch to English">Switch to English</a></div>

You can comment the echo statements. It's just there for testing.

 

I put the following code in the include folder and I named it hdr-en.php

<?php
$path_info = $_SERVER['REQUEST_URI'];
if (strpos($path_info, "/ar/") > 0){
    echo "YOU ARE IN THE ARABIC SITE";
    $path_info = str_replace("/ar/", "/en/", $path_info);
}else if (strpos($path_info, "/en/") > 0){
    echo "YOU ARE IN THE ENGLISH SITE";
    $path_info = str_replace("/en/", "/ar/", $path_info);
}
echo '<br />';
echo $path_info;
?>

<div id="langbar"><a href="<?php echo $path_info; ?>" target="_self" title="عربي">عربي</a></div>

 

I had a problem running this using PATH_INFO which didn't really do anything. Then I used REQUEST_URI and that fixed it right away.

*The arabic writing might show weird on this post but you get the point.

Just include the file(s) using

<?php include("../include/hdr-en.php"); ?>

and you're good to go.

 

It's too bad not many people answered my question properly (maybe didn't fully understand it) and I'm more amazed/sad  :o at how little responses there were but anyways, I hope this helps someone in the future.  ;D

By the way, this works in php5 and should be fine in php4 too.  :D

 

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.