views:

97

answers:

4

What's the best way (to avoid modifying repeated code) to building multilingual web pages? I know how to build a multilingual web page without having to modify CSS and Javascript files. But I can't think of a neat solution for HTML and Php files. Because if I have HTML or Php files for each language, I would have to modify each one if, for instance, I add an extra div or other element. I was thinking to have something like this:

<div id="multilingual div">
<p><?php echo($multilingual-paragraph); ?></p>
</div>

(So, even if I modify these elements, I will just do it once, because the text that is in other language will show up from the variable).

I don't know Php, so I don't know how to tell Php to display a different variable according to the language (I think it has something to do with IF conditions)

Is this a good way of creating multilingual web pages or there are other methods?

(So with this

+1  A: 

As you said, you have php and html language files, one way is to go like this:

$lang = '';

switch ($lang_file)
{
   case 'en.php': $lang = 'whatever'; break;
   case 'fr.php': $lang = 'whatever'; break;
   // etc
}

<div id="multilingual div">
<p><?php echo $lang; ?></p>

// or you may include files
<p><?php include_once ($lang); ?></p>
</div>
Sarfraz
+2  A: 

Check out the gettext() function. You don't really need to build different files for different languages. Altough, you'll have to struggle with the translation files.

metrobalderas
A: 

Maybe this article on internationalization in PHP may help.

Vincent Ramdhanie
+2  A: 

You can implement a constants-solution for output messages. Using APC's cache functions, you can store multiple messages inside the cache and load them according to the pages you're viewing (this might not be an easy solution though, you need to know php for this).

This would allow you to maintain an array with values for each language in the cache. For example:

apc_constants_define('en',array('welcomeMessage'=>'Welcome!'));
apc_constants_define('es',array('welcomeMessage'=>'Bienvenidos!'));
apc_constants_define('de',array('welcomeMessage'=>'Willkommen!'));

through AJAX/select form, you can allow the user to choose the language they want to view your pages. This language would be stored inside a session:

$_SESSION['language'] = 'en';

Next, on every page's top, you should check the session (simple switch statment) and load the constants from the cache accordingly.

apc_load_constants($_SESSION['language']);

then your html page would look like this:

<h1><?php echo welcomeMessage; ?></h1>

This is, as I see it, the most efficient way of internationalizing your website, and with an easily maintainable system, that doesn't require you to delve into the code when you want to translate your page to Romanian.

sombe
I tried the following, but it didn't work, I think I'm doing it wrong.<?php apc_define_constants('en',array('welcomeMessage'=>'Welcome!')); apc_define_constants('es',array('welcomeMessage'=>'Bienvenidos!')); apc_define_constants('de',array('welcomeMessage'=>'Willkommen!')); $_SESSION['language'] = 'en'; apc_load_constants($_SESSION['language']);?><html><body> <h1><?php echo welcomeMessage; ?></h1></body></html>
janoChen
you first need to download the APC library, as a dll file, and configure your php.ini file to load it. you can find the downloadable file from the manual/pear websites. if you can't find it there, run a google search for it, or even on stack overflow.
sombe
@janoChen, as a sidenote, I suggest you read APC's documentation as well, you may find it insightful since it takes a tad more than just copying and pasting the code.
sombe