Page 1 of 2
Building a multi-language site?
Posted: Mon Apr 18, 2005 4:47 am
by Phoenixheart
Hello everyone. With yur great help I've completed my first PHP project (phew

). Now a new one comes

and it will be a multi-language site. For right now we just need 2 languages (English and Vietnamese) but in the future there might be one or two more (French eg). So I'm thinking of two ways to deal with this issue:
- Duplicate all html and php files into a new folder named from the corresponding language(Means there will be an index.php and a vie/index.php and so on), then just create a link between the two.
- Or use a php if...else like this:
Code: Select all
if($_GET['lang']=='eng'){
//ALL the code in PHP and HTML in English
<html><head></head><body>...</body></html>
}
else{
//ALL the code in PHP and HTML in Vietnamese
<html><head></head><body>...</body></html>
}
Though the file will be as twice in size, but the excuting time will remain the same as one seperated file, because PHP will run only "if" or "else", never both (in my opinion).
Those are 2 ways I'm thinking of. But they both look so "amateur"

Anyone could help me with a more professional method? Or just any idea about my methods?
Posted: Mon Apr 18, 2005 4:52 am
by malcolmboston
what i would do is hold alternative words in a database schema for eg.
fields: ENGLISH_LANG || VIET_LANG
each would hold alternative words for each, based on the user's config you could do:
Code: Select all
if ($user_language == "ENGLISH_LANG")
{
// query the ENGLISH_LANG field
}
elseif ($user_language == "VIET_LANG")
{
// query the VIET_LANG field
}
else
{
// unknown language
// choose a default (ENGLISH_LANG
}
a somewhat simple example, but its highly scalable.
Posted: Mon Apr 18, 2005 5:10 am
by Phoenixheart
Yes I did thought of a table

Then I would have to query and return a resultset called rs_lang. And go rs_lang['paragraph1'], rs_lang['paragraph2'] based on $_GET['lang']... If my site have a hundred paragraphs(must be more

) then I will have to create 200 columns or what? Would you explain more?
Posted: Mon Apr 18, 2005 5:11 am
by malcolmboston
like i said this is highly scalable, returning a hundreds of words, or many paragraphs will not be a problem for PHP to handle
Posted: Mon Apr 18, 2005 5:24 am
by Phoenixheart
Could you explain more details about this method? I'm in mud right now

Posted: Mon Apr 18, 2005 6:25 am
by paolone
I have just redo a site in multilanguage...
I used a database with all the text divided per page, and per language. In the DB next to each text field there is a 'lang' field for EN, ES, DE, etc....
On the page i have a simple "if lang = ''" that does the job.
This way you can add as many languages as you want and call it always in the same way...
Hope it helps...
Posted: Mon Apr 18, 2005 6:26 am
by CoderGoblin
Or use an ini file dedicated to the language rather than a db...
At the start of the code simply load the ini file for that location. The location stores all the strings as an array.
The advantage of this is you can give translators an ini file more easily than getting them to change a db.
A lot depends on the quantity of text involved. For a fairly simple GUI the ini file would work. For a textual based web site the DB method is better (Of course this is only my opinion).
Posted: Mon Apr 18, 2005 6:56 am
by Phoenixheart
paolone wrote:I used a database with all the text divided per page, and per language. In the DB next to each text field there is a 'lang' field for EN, ES, DE, etc....
But... will it slow down the excuting time? PHP and mysql must handle a lot of data in one resultset.
Posted: Mon Apr 18, 2005 8:22 am
by paolone
Well... indeed is not probably the best way... but is fast enough... the db is well structured. But there where also the need of having a upgrading system with some admin functionalities...

...
Posted: Mon Apr 18, 2005 10:28 am
by Calimero
I don't quite follow:
1) You have link, navigation etc.... constant content of the site
2) You have dinamical content like news, posts or similar things right?!?!?!
I would build a site and instead of different language words I would use one included php file that has one language translated and it the php would choose which language file to take depending on the input parameter you specify.
This is good enough for the option 1 in my post, however if you have constantly (often) changing content, then my choise would be the DB connection and query execution.
In the first option I would not use the DB because it is independent of your hosting providers SQL server ( whether it is up, or if it is on the other machine that is very "busy" with users that have their DB's on that server ).
Hope this helps.
Posted: Mon Apr 18, 2005 10:32 am
by malcolmboston
if you wanted a site where it would translate absolutely everything including user articles etc, i would personally build a massive word list over time and then do a
Code: Select all
$translation = str_replace($org, $new, $content);
this has many pitfalls such as getting a translation list of every word in every possible language but in my eyes its the only way to do a completely + truly languiage-independant web-site
Posted: Mon Apr 18, 2005 10:42 am
by JayBird
malcolmboston wrote:if you wanted a site where it would translate absolutely everything including user articles etc, i would personally build a massive word list over time and then do a
Code: Select all
$translation = str_replace($org, $new, $content);
this has many pitfalls such as getting a translation list of every word in every possible language but in my eyes its the only way to do a completely + truly languiage-independant web-site
That would be a very rough way of doing translation...translating involves more that just replaceing words...the grammar is completely different, and would render most of you site unreadable in other languages.
Posted: Mon Apr 18, 2005 10:44 am
by malcolmboston
exactly which is my whole point, doing a full translation site where the content is not decided by you (ie. user reviews etc) is unbelievably difficult
Posted: Thu Apr 21, 2005 10:52 pm
by Phoenixheart
I asked my tutor about this problem, and he said people often use a text file that contains all the strings, like this:
Code: Select all
VIE:Xin chao!
ENG:Hello!
FRE:Bonjour!
--
VIE:Cam on!
ENG:Thanks!
FRE:Merci! (or something, hehe, I don't know any French :oops: )
Then they select the correspoding string??? Anyone understand what he meaned?
Posted: Thu Apr 21, 2005 11:45 pm
by hongco
are you gonna translate the whole website's content to different languages? or just some menu thingy?
if the whole site, i bet you can't do it yourself but hire someone to do this as their fulltime job

would you hire me?
if it is just menu and few other things, then create each file for each langue which contains some array, for instance:
Code: Select all
// english file: eng.php
$menu = array(
'home' => 'home',
'logout' => 'logout'
);
// viet file:viet.php
$menu = array(
'home' => 'trang nha',
'logout' => 'thoat'
);
on your main page, you would have menu choice: eng | viet, and use cookie to track which language a user use; then select the correct lang file to be inlcuded on your document accordance to the user's choice. I would not use $_GET['lang'] from every single link....cookie or session is the way to go.
From there, any file requires the menu, etc..you can just use something like: $menu['home'], $menu['logout'] -> it will show up with the correct language the user had selected.
That is how i use for my site.