• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1070
  • Last Modified:

multilanguage site in PHP

We currently have a web site written in (simple) PHP.

We would need to add the multilanguage feature,
that is, that the user can select in which language to view the website.

What would be the best way to implement this?
without changing too much our current PHP code.

Many thanks,
DAvid
0
dportabella
Asked:
dportabella
  • 2
1 Solution
 
nizsmoDeveloperCommented:
No need to rewrite too much, but need to translate all your text into a foreign language, and store them say in a database table (each language has different table). Upon visitor visiting your site, default the site to english and let them choose language, if they chose another language, replace all the text you have translated (by retrieving foreign text from the database) and output them to the appropriate places to replace the english text.

It is a smart idea to have a table for english as well (your default language). Then when the user visits, retrieve the english values from the database. Make all your title, menutext, browser heading, etc.... as variables that are replaceable in terms of which language you have to retrieve from the database.

Hope this is an understandable explanation.
0
 
x_bakosCommented:
If the site isn' t that big, you could also use a language file,i.e. english.php, german.php, each one containing the text (in each language) you want in variables. In your code you just have to use the proper variables, depending in the user's language selection. I would prefer using XML to do that, but it might require a bit more programming work...

Bakos
0
 
dportabellaAuthor Commented:
Hello,

Thanks for the comments.
I guess that we are not the only ones with that need,
and we would prefer to use a standard approach.

We would prefer not using a sql database.
Defining variables (in separate php files or xml files) would be nice, but still a little cumbersome.

We would really like to have everything (the php code + the text in the different languages) all together in the same php files.

Do you know of full simple concrete implementation examples that we can look at?


Many thanks,
DAvid
0
 
x_bakosCommented:
Ok, let's do it using .php files.

Let's suppose we need 2 languages, english and french (en,fr). We create 2 different subfolders in the site, i.e. /en and /fr.
In there we have a file (translation.php) which will contain the translations. The contents of this file will look like the following:

------en/translation.php--------
<?php
$_text_1 = 'text_1';
$_text_2 = 'text_2';
$_text_3 = 'text_3';
php?>
--------------------------------

------fr/translation.php--------
<?php
$_text_1 = 'texte_1';
$_text_2 = 'texte_2';
$_text_3 = 'texte_3';
php?>
--------------------------------

Ok, now we have everything translated in the 2 languages. Notice that we use the same variable names in both files, so that we can easily access the wanted variable.

Now suppose that we have 2 icons, each one used for the language selection. When a user clicks any of them you POST/GET a variable and reload the page. The variable you POST/GET will contain either 'en', or 'fr', so that you be able to know in which path to look for the text...I think you now get the point ;)

In each .php page, you will have the following code:

----------
<?
$lang  = $_GET['lang'];

$path = "./". $lang ."/translation.php";

if ( is_file($path) ) {
  include $path;
}
?>
----------
Now you include the selected file (/en/translation.php OR /fr/translation.php) and you can use the same variables, but the text is in the language the user has chosen....

Easy enough, isn't it?

Bakos
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now