Question

I am building a English/french website and was wondering if there is a best practice for such a job.

  1. Duplicating the site and making a french and english folder with the appropriate site inside.

  2. Using PHP to swap the content with html tags. eg. if($lang=='en'):

  3. Use php to swap only the content leaving the html tags the same for both. eg. if statements all over the place. Would this be bad for efficiency?

Any other suggestions would be appreciated

Was it helpful?

Solution

We have a framework in place for when (if) our site goes international that works like this...

Folder structure;

/
  lang/
    english/
      images/
      text/
    dutch/
      images/
      text/

Any text or images that are language specific are removed from the page directly and replaced by constants. eg On the login screen, we drop in;

echo TEXT_LOGIN_WELCOME;

which is defined in /lang/english/text/login.php as;

define('TEXT_LOGIN_WELCOME', 'Welcome, please login:');

but in /lang/dutch/text/login.php it's defined as;

define('TEXT_LOGIN_WELCOME', 'Welcome, please login (in dutch):'); 

;-)

Each language define file is named exactly the same as the page it is used for, so when we load a public-facing page, we only need to figure out which language the user speaks and we can include the relevant language define file.

The good thing about this system is that all the language info is centralised. When you need to add a new language, simply copy the main (english?) folder, rename it, zip the whole thing up and send it to a translation service to work their magic. Of course, the downside of this system is the maintenance as both languages and content grow... If anyone has any bright ideas with regard to this then I'd love to hear them!

Btw, if you end up needing to guess a user's location by IP, you might want to check out geoIP.

OTHER TIPS

Use a templating system. Smarty Template Engine is probably one of the most well-known PHP ones. Not only does a templating system serve the exact purpose you're trying to accomplish, it also makes maintaining pages far easier by separating the display code from the content (which also allows you to use the same template for lots of different content pages of a similar nature).

As the simplest way I recommend you to use i18n internationalization method & gettext catalogs (.po files).
The famous WordPress project is using it as well.

1 - Duplicating the entire site will force you to repeat every code touch-up into the 2 folders :-[

2 - If you mean somenting like

<?php if($lang=='en') { ?>  
  <p>English text</p>  
<? } else { ?>  
  <p>Text français</p>  
<? } ?>

This solution is perfect to manage two languages in the same page.
But you still have duplicated tags.

3 - Change only content it's really satisfaction.
Maybe proliferate of if statements can weigh down php compiling... I don't know.
Anyway document can be more concise with this approach:

<?php            
  function interpreter($buffer) {
    $pieces = explode('#', $buffer);

    if (isset($_GET['lang'])) $begin=$_GET['lang'];
    else $begin = 1;  // 1 to display français, 2 to display english

    $tot = count($pieces);          

    for ($i=$begin; $i<$tot; $i+=3) {  // remove one language
    unset($pieces[$i]); }

    foreach ($pieces as $value) {  // recompose the text
    $output .= $value; }

    return $output;
  }

  ob_start("interpreter");      
?>

<a href="?lang=#1#2#">#Français#English#</a>
<p>#English text#Texte français#.</p>

<?php ob_end_flush() ?>

The text between ob_start and ob_end_flush is parsed AFTER php compiling.
That means are affected strings coming eg. from echo statement, not inside < ?php ?> tags.
Also content coming from php include IS affected.
But NOT external css or javascript.

Keep attention delimiter # isn't a caracter yet used elsewhere.
Maybe you'll prefer to replace with || or ^^

Of course in the future you can adapt this solution into 3 languages or more. But if you have to insert the "Third language translation#" in many lines of a big site, maybe the solution from MatW fits you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top