Creating multilingual website

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tonchily
Forum Commoner
Posts: 54
Joined: Thu Sep 02, 2010 10:44 am

Creating multilingual website

Post by tonchily »

What would be the best way to create a multilingual website? Saving strings in mysql and make every language has its own table? I really don't have an idea on how to do this so any help is appreciated.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Creating multilingual website

Post by Darhazer »

There is no need of a table for every language.
Usually multilingual support is created with just one extra table for each table that has text fields
For example if you have:
products
----------
product_id
category_id
title
description
price

in multilingual architecture it will look like:

products
----------
product_id
category_id
price

products_ml
----------
product_id
language_id
title
description


The user interface can use either language files that contain all the string, or also a database table, if templating engine permits it.
tonchily
Forum Commoner
Posts: 54
Joined: Thu Sep 02, 2010 10:44 am

Re: Creating multilingual website

Post by tonchily »

Lets say I want to use it via language files (so it would be lang_english.php, lang_german.php, lang_spanish.php) which have the same strings (which are stored into variables) and just translate the text of these variables - how would I import it into lets say index.php? Using include function and then just put the variable names into the corresponding spots? Or there is easier way to do this?
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Creating multilingual website

Post by cpetercarter »

The method you describe is a common way of creating a multilingual website.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Creating multilingual website

Post by klevis miho »

I would encourage you to use different .php files for every language. And in each of those PHP files you should make an array which holds the language strings. For example, in the lang_eng.php it will be something like this:
$lang['What_is_your_name'] = 'What is your name?';

in the german file it will be something like this:
$lang['What_is_your_name'] = 'Wie heisst du?';

Then you will check what language has been selected and add the selected language into a session variable:
if($_POST['language'] = 'eng') {
$_SESSION['language'] = 'eng';
} else {
$_SESSION['language'] = 'de';
}

include('lang_'.$_SESSION['language'].'php');


Something like this...
Post Reply