Hello,

I have used Single Translation Table Approach to make multilanguage site in PHP. You may see here

The current index page is not working.

My objective is to have one example to use these tables and display one working page. For example: It should work like this by using these tables.

What i did so far on my home page:

<!DOCTYPE html>
<?php session_start();   
    $_SESSION['current_language'] = "fr";
    $con = mysql_connect("localhost","worksite","");
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("worksite", $con);
?>

<?php

    echo '<a href="./index.php?lang=fr">Fran&ccedil;ais</a> | ';
    echo '<a href="./index.php?lang=en">Anglais</a> | ';

    $languages = array('en', 'fr');

// handle language selection
if(in_array($_GET['lang'], $languages)) 
{
    $_SESSION['lang'] = $_GET['lang'];
}

// define LANG constant only if it exists in $languages array, otherwise default to EN
define('LANG', in_array($_SESSION['lang'], $languages) ? $_SESSION['lang'] : 'fr');
define('LANG', in_array($_SESSION['lang'], $languages) ? $_SESSION['lang'] : 'en');

// display language options
foreach($languages as $language) {
    echo '<a href="?lang='.$language.'">'.$language.'</a>';
}

    $sql = "SELECT p.*, l.name as language_name, te.field_text as title
        FROM `app_product` p
        INNER JOIN `app_translation_entry` te ON p.title = te.translation_id
        INNER JOIN `app_language` l ON te.language_code = l.code
        WHERE p.id = 1";
if($result = mysql_query($sql)){
    while($row = mysql_fetch_assoc($result)){
        echo "Language (".$row["language_name"]."): ".$row["title"]."<br>";
    }
}

// Retrieve appropriate title according to the chosen language in the system
$sql = "SELECT p.*, l.name as language_name, te.field_text as title
        FROM `app_product` p
        INNER JOIN `app_translation_entry` te ON p.title = te.translation_id
        INNER JOIN `app_language` l ON te.language_code = l.code 
        WHERE p.id = 1 AND 
              te.language_code = '".$_SESSION['current_language']."'";
if($result = mysql_query($sql)){
    if($row = mysql_fetch_assoc($result)){
        echo "Current Language: ".$row["title"];
    }
}
?>

</body>  
</html>  

THANKS IN ADVANCED for your time and input.

On line 3: You define ['current_language'] as 'fr' and never change it, so it will always be 'fr'.

On line 15: This is personal preference, I wouldn't create a link to a language that is translated. English would always be English and never Anglais, since when the website opens I want to know where my language is and know how to read it. If the base language was Russian I wouldn't know where English was.

On line 26 and 27: You're defining LANG as either 'fr' if it's not in the array and then again to 'en', this isn't necessary as you already tested on line 20 before placing it into the session variable. You could expand that condition to default to 'en' on the else part of the statement, like so:

// handle language selection
if(in_array($_GET['lang'], $languages)) 
{
    $_SESSION['lang'] = $_GET['lang'];
    define('LANG', $_SESSION['lang']);
} else {
    define('LANG', 'en');
}

However I don't see you using either $_SESSION['lang'] or LANG anywhere else on the script, so that logic won't be applied to the output.

On line 31: You're basically creating anchor blocks with the contents of the $languages array -> <a href="?lang=en">en</a>. This would need to be used after collecting the translations from the database.

On line 34 to end: You could consider, although it depends on how big those databases will become, collect an array with everything you need from that database, with a key = te.language_code, where you could then use it like so:

$dbArray[$_SESSION['lang']]['title'];
// or
$dbArray[LANG]['title'];

If you need more help just reply to this and I'll try to keep an eye on the discussion.

Member Avatar for diafol

Aren't you finding this approach tedious? Ok, if you have more than two languages, it may be easier to keep all languages in sync, but even a file-based approach would be less work (IMO).

Anyway, have you considered XGETTEXT and .po files?

Here are several blog posts on the matter...

http://diafol.blogspot.co.uk/2013/01/php-localization-with-gettext-on-windows.html

http://diafol.blogspot.com/2013/01/creating-pot-files-in-poedit-for.html

http://diafol.blogspot.co.uk/2013/01/supporting-xgettext-on-windows-from.html

http://diafol.blogspot.co.uk/2013/03/gettext-issues-fix-with-php-gettext.html

@Fernando_4

It sounds good. Thanks for quick reply. Yes, you are right that it must be English, never Anglais. Actually, i tried to look from tutorial and wrote it out.

One can change the whole file but the objetive is to have one page as per link, there is a demo on that link should working.

I am sking to have on page like this
http://demo.codeursolitaire.com/multilanguage/index.php

with 4 tables.

Thanks a lot.

@diafol

I want to try out that approch to make static website. Thanks for your suggestions. I am not sure that it will be best or not but still i would like to try my best.

@diafol Nice info there, I'm going to keep that for a project I'm working on. I'm implementing a file-based translation, but I'm foreseeing tons of errors and "I can't remember where this was" type of situations when I need to revisit those files. Gotta give that a spin, thanks^^

@PriteshP23 Having a page like the one you linked is the easy part, the problem is that you can go about it in 1000 different ways where you're only going to pick out one.
I'm having a hard time expanding on my answer since the question isn't really specific. There isn't any note on what's not working, what errors are you getting, what output isn't updating properly, etc...
What I mentioned in my first post was what caught my eye, but without building the whole thing I wouldn't know what's actually wrong about it.
Try to make the question more specific or even post some results. Also don't forget to echo some of the variables to see if they're changing as you need them to.

@Fernando_4: I have already put the code in my question that i did so far. I agree that there are 1000 ways but i asked specifically for this approach. I am beginner for multi-language site. In all cases, i just want to use those for tables to make one page. I know that it is not easy to do that when one need to do from above idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.