Search code examples
phpinternationalizationtranslationmultilingual

multilaguage site - web page translation


Before I start I'd like to mention that I know that there is alot of answers about multilanguage functionality, I just didn't found the answer I 'd hope I found from my little search.

I have a page (e.g. about.php) where all the content is just html, having many heading tags, p tags, list-items etc etc. The content is 'static', meaning that it is NOT coming from the database and I'd like to apply translation on it by clicking a link (english or french).

I've followed some tutorials and examples and come up with something quite similar to this example, but what concerns me is about the content of the page. What I'm trying to say is this: Suppose my page structure looks like this:

<html>
<body>
<ul>
<li><a href="index.php"><?php echo $lang['HOME']; ?></a></li>
<li><a href="about.php"><?php echo $lang['ABOUT']; ?></a></li>
<li><a href="?lang=fre">french</a></li>
</ul>
<h1><?php echo $lang['HEADING']; ?></h1>
<p><?php echo $lang['PARAGR']; ?></p>
<p><?php echo $lang['ANOTHER_PAR']; ?></p>
</body>
</html>

The values of the nav menu could easily stored inside a $lang array and echo out the specific one each time, but is this also the best (or more efficient) way to do it and with the page content?? Do I have to put in the $lang all my headings and paragraphs and lists etc etc or this can be easily done with a better way? What if some paragraphs have too much text inside? etc

Hope that make sence.

P.S Iwas also thinking of making two different files, like about_eng.php and about_fre.php, where I have all the html content in english and in french respectively and with a simple if statement to check which language is selected each time and include the right file


Solution

  • Keeping your language specific data in different php files declaring $lang array is a good way I think. That will avoid retyping all content with language, meaning less redundancy. So I agree with Jaspal. For typing $lang['XXX'] you can convert the php into this easily(maybe a stupid solution but it will work):

    function displayMyPage() {
    global $lang;     // do not forget the other globals you need in the page...
    $l = $lang;       // localize $lang as $l... 
    ?>  
    <html>
    <body>
    <ul>
    <li><a href="index.php"><?php echo $l['HOME']; ?></a></li>
    <li><a href="about.php"><?php echo $l['ABOUT']; ?></a></li>
    <li><a href="?lang=fre">french</a></li>
    </ul>
    <h1><?php echo $l['HEADING']; ?></h1>
    <p><?php echo $l['PARAGR']; ?></p>
    <p><?php echo $l['ANOTHER_PAR']; ?></p>
    </body>
    </html>
    <?php
    }
    ?>