Website in two languages..

Soldato
Joined
18 Oct 2002
Posts
3,245
Location
melbourne
Hey Guys,

I have to build a website for a client that needs to be in Irish and English, this usually isn't a problem, but the client wants to have a button on the top of each page for each language, so regardelss of what page you're on, if you click English it will take you to the English version of that page. Do you follow me?

I could always manualy edit the Irish/English links on every page, but this would take a while, and I'd really like to know how to do it properly.

Their webserver supports PHP.

Any pointers?

Thanks
 
Are your pages mostly static HTML files, or stored in a database and served by a CGI/PHP script or equivalent? If the latter, then it should be pretty easy to add a language column in the table for each page/text and store that in a cookie or session variable.

If your pages are mostly static, and you have a naming scheme like page.en.html for english and page.ie.html for irish, you could use a PHP script to view the pages, link instead of linking to page.<language>.html from a page, you could link to view.php?page=<page> and that PHP script could read a cookie to determine which language to display in. Switching between languages would then be a simple case of setting a cookie.

You can also do this 'automatically' via mod_rewrite in apache, turning static page references into PHP requests :

Consider a .htaccess file with mod_rewrite rules such as :
Code:
RewriteEngine on
RewriteRule (.*)\.en\.html$ view.php?page=$1&l=en [L]
RewriteRule (.*)\.ie\.html$ view.php?page=$1&l=ie [L]
RewriteRule (.*)\.html$ view.php?page=$1 [L]

This would convert a request for 'page1.en.html' into 'view.php?page=page1&l=en' and also 'page1.html' into 'view.php?page=page1', so every request for a HTML page gets redirected transparently (to the browser) through a PHP script where you can do your magic :
Code:
<?php

// if a language is explicitally set, via access to :
//   <page>.<language>.html -or-
//   view.php?page=<page>&l=<language>
if (isset($_REQUEST['l']) && $_REQUEST['l'] != '') {
        // then use the language from the request, and set a cookie.
        $lang = $_REQUEST['l'];
        setcookie("lang", $lang);
        echo "switching language : $lang<br>";
} else {
        // otherwise,
        // is a cookie set with a language?
        if (isset($_COOKIE['lang']) && $_COOKIE['lang'] != '') {
                // yes, use that language
                $lang = $_COOKIE['lang'];
        } else {
                // no, use a default language for now.
                // (could use the 'Accept-Language' HTTP header to get this)
                $lang = 'en';
        }
}

// write the switch-language links which we want on every page
echo '<a href="' . $_REQUEST['page'] . '.en.html">english</a> ';
echo '<a href="' . $_REQUEST['page'] . '.ie.html">irish</a> ';
echo "<hr>\n";

// read the actual page
readfile($_REQUEST["page"].".".$lang.".html");
?>

... with this, you'd store your pages like (notice the same link) :

page1.en.html :
Page in english, another page <a href="another-page.html">here</a>

page1.ie.html :
Page in irish, another page <a href="another-page.html">here</a>

... and the PHP script would be invoked on every request which would make display the page according to the language cookie set.

edit : Maybe an example would explain it better :) heres a link to a working demo on my website http://haxor.me.uk/languagetest/test.html
 
Last edited:
Wow, Dude, best reply EVER :)

I'll get cracking on this and paste you a link once I get something done.

Thanks again!
 
Eeeew don't do it that way, it'll take you forever and be a pain when you need to update any page as you'll have twice the work to do (I think anyway, speed read...but here goes my idea :D). Keep his cookie code and the .htaccess idea (if poss) but do the rest my vay! (So long as you're using PHP on your site, or have it available on your hosting.)

Put all your text strings into a language file, one per language, in a lang directory, as follows:

Code:
lang/english.php:

$lang['welcome'] = "Welcome to my site!";
$lang['a_string'] = "Some other bit of text";

lang/irish.php:

$lang['welcome'] = "Welcome to my site would you like a Guinness?";
$lang['a_string'] = "We have lots of lol pubs";

Now, in your site's header file (which you have because you're a good coder who doesn't want to repeat the same header on every page manually) add something like this:

Code:
$lang = $_GET['lang'];

if($lang == "en" || $lang == "ie") {
     include 'lang/' . $lang . '.php';
} else {
     include 'lang/english.php';
}
Then, on your page, all you need to do is replace every single text string as follows:

From:
Code:
<h1>Welcome to my site!</h1>

To:
Code:
<h1><?=$lang['welcome']?></h1>

If you don't use a global header you can be naughty and use auto_prepend_file :)

This is obviously a bit of a pain to do but I'll wager it's better than duplicating all your pages. I've also just realised you probably know most of this anyway and were only really asking about the cookie bit....but hey :)
 
This is great guys, thanks. I assume I can use Beansprouts code for images aswell. eg, there are GIF navigation buttons in both languages.
 
Morlan said:
This is great guys, thanks. I assume I can use Beansprouts code for images aswell. eg, there are GIF navigation buttons in both languages.
Yup, in combination with matja's strategy - two image files:

Code:
<img src="name.<?=$lang['img_name']?>.gif" />
:)
 
I got a phonecall from the client saying he's got a copy of Contribute and wants to edit the text himself. :mad:

Short of setting up a full blown CMS with lingo support, I don't think there's anyway around it, I'll just have to do all the pages as static HTML :( ... unless anyone has a suggestion.

Anyway, I frequently get dual-language jobs so the above code will come in very handy.
 
Morlan said:
I got a phonecall from the client saying he's got a copy of Contribute and wants to edit the text himself. :mad:

Short of setting up a full blown CMS with lingo support, I don't think there's anyway around it, I'll just have to do all the pages as static HTML :( ... unless anyone has a suggestion.

Anyway, I frequently get dual-language jobs so the above code will come in very handy.
Well - that's simple.

Use beansprout's idea, but use a MySQL db instead of the two language flat files.

Then create a snazzy php backend admin area for him so he can edit the text in each langauge....

Bingo! No need for a full blown CMS.

No?
 
Last edited:
EDIT:

i.e.
Code:
Table - "English"

field - value
-----  -----
title - blah
welcome text - blah2

Table - "Irish"

field - value
-----  -----
title - blah
welcome text - eirebirdsarehot

And just call each value based on cookie :)
 
I could do that, but the client has stretched his budget to the max at this stage.

Pitty, but this is how it goes. I'm not willing to spend anymore time on this project.

edit: Thanks so much for your input, guys. I've bookmarked this page, and I'll defo use it in the future :)
 
Morlan said:
I could do that, but the client has stretched his budget to the max at this stage.

Pitty, but this is how it goes. I'm not willing to spend anymore time on this project.

edit: Thanks so much for your input, guys. I've bookmarked this page, and I'll defo use it in the future :)
Fair enough....

How have you decided to do it?
 
jdickerson said:
Fair enough....

How have you decided to do it?


You shall see. It's as static as you can get. I'm quite happy with the site, I'll paste yez a link by tomorrow or Sunday. :)
 
Last edited:
Morlan said:
You shall see. It's as static as you can get. I'm quite happy with the site, I'll paste yez a link by tomorrow or Sunday. :)
To be honest, sometimes there is nothing better than a static page.
:D
 
Back
Top Bottom