Click to See Complete Forum and Search --> : Multilingual Site


ShawnK
12-20-2004, 03:03 PM
I am working on my php portfolio and I noticed that there are a lot of clients that don't speak fluent English and it is quite funny when they use a web translator. So I decided that it would be worth it to make my site multilingual.

On my index page it says English, Espanol, Duetsch, and Francias. I have that image hot-spotted so that when you click on the American flag it goes to: main.php?pg=main&lang=eng and so on for all of the languages.

And this is the code in my main.php file:


<?php

switch($_GET[lang]){

default:
$pages = array(
"main" => "eng/main.html",
"services" => "eng/services.html",
"portfolio" => "eng/portfolio.html",
"contact" => "eng/contact.html",
"nav" => "eng/nav.html",
"copy" => "eng/copy.html"
);
break;

case "eng":
$pages = array(
"main" => "eng/main.html",
"services" => "eng/services.html",
"portfolio" => "eng/portfolio.html",
"contact" => "eng/contact.html",
"nav" => "eng/nav.html",
"copy" => "eng/copy.html"
);
break;

case "esp":
$pages = array(
"main" => "esp/main.html",
"services" => "esp/services.html",
"portfolio" => "esp/portfolio.html",
"contact" => "esp/contact.html",
"nav" => "esp/nav.html",
"copy" => "esp/copy.html"
);
break;

case "deu":
$pages = array(
"main" => "deu/main.html",
"services" => "deu/services.html",
"portfolio" => "deu/portfolio.html",
"contact" => "deu/contact.html",
"nav" => "deu/nav.html",
"copy" => "deu/copy.html"
);
break;

case "fre":
$pages = array(
"main" => "fre/main.html",
"services" => "fre/services.html",
"portfolio" => "fre/portfolio.html",
"contact" => "fre/contact.html",
"nav" => "fre/nav.html",
"copy" => "fre/copy.html"
);
break;
}
?>


It sets the array with the files for the differant languages.

And anytime I was to call a file into my layout I just do this:


<?php

include($pages[services]);

?>


So what do you think? Is there an easier or better way to do it?

onion2k
12-20-2004, 04:30 PM
I think I'll add multilingual capabilities to my site.. but in a much more traditionally British way:


if ($lang != "en") {
$fontsize += 20;
}


;)

ShawnK
12-20-2004, 11:37 PM
LOL. That's pretty good. But seriously is there a more efficient way of doing this?

cyberlew15
01-01-2005, 09:47 AM
I created a script where a wordlist of words is loaded into an array and replaced with a word list of foreign languages it supports french,german,italian,portuguese and spanish however the sentences may not be gramatically correct in that language I think that people will understand the jist of what is being said.

<?php
$startchar = " ";
$endchar = " ";
function translate($text) {
global $export;
$search = array_map('trim', file('./langs/sourcelang.lng'));
$search2 = implode(" | ",$search);
$search3 = explode("|", $search2);
$replace = array_map('trim', file('./langs/translang.lng'));
$replace2 = implode(" | ",$replace);
$replace3 = explode("|", $replace2);
$export = str_replace($search3, $replace3, $text);
return $export;
}
$translated = translate($startchar."hello my name is cat".$endchar);
echo $translated;
?>

the lng files should be a wordlist with one word per line startingg with the longest words i created my wordlists with excel and babelfish. Good Luck:D

trooper
01-02-2005, 08:55 PM
Howdy there its ages since i posted here so i may be a tad rusty.

Anyway having looked at your question i noticed that you have the same arrays but differentl languages.

Well instead of actually creating a 4 way case statement why not just do this


<?php

var $lan;
if(empty($lan))
{
$lan = "en";
}

switch($lan)
{
case "en":
case "fr":
case "de":
$pages = array (
"main" => "{$lan}/main.html",
"services" => "{$lan}/services.html",
"portfolio" => "{$lan}/portfolio.html",
"contact" => "{$lan}/contact.html",
"nav" => "{$lan}/nav.html",
"copy" => "{$lan}/copy.html"
);
break;

default:

break;

}

?>

Incidentally you would find it even easier to have speparate language files and then simply read in the vars and values from the language files. Then in each of the above files (copy.html,nav.html etc) you could simply put the appropriate value that related to the var.

This would allow you to have one copy of each of those files.

HTH

suepahfly
01-04-2005, 03:39 PM
Well, gettext seems to be the standard in multilanguage apps.

I't pretty simple to implement in too ypur apps. But the only drawback is that most webservers don't offer gettext support with php.

cyberlew15
01-07-2005, 08:09 PM
Hey I think that this whole thing of having different versions of the pages is ggreat for a business perspective but if all this guy wants this for then machine translatin on the fly could save him a tonne on hosting fees also this is a forum for php developers and you are suggesting writing a script that could just as well be written in html with server side includes:p