The Most Effective Way to Template a Multilingual Website

I am creating an English / French website and am wondering if there is any best practice for such a job.

  • Duplication of the site and the creation of the French and English folders with the corresponding site inside.

  • Using PHP to share content using html tags. eg. if ($ lang == 'en'):

  • Use php to share only content, leaving the html tags the same for both. eg. if statements are everywhere. Would it be bad for efficiency?

Any other suggestions would be appreciated.

+4
source share
4 answers

We have a basis for when (if) our site goes beyond the international, which works like this ...

Folder structure;

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

Any text or language-specific images are removed directly from the page and replaced with constants. for example, on the login screen:

 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 is defined as:

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

; -)

Each language-defining file is called exactly the same as for the page for which it is used, therefore, when we load the page with open access, we only need to find out what language the user speaks, and we can include the corresponding language definition file.

The good thing about this system is that all language information is centralized. When you need to add a new language, just copy the main (English?) Folder, rename it, write down the whole thing and send it to the translation service to work with their magic. Of course, the disadvantage of this system is the maintenance, as languages ​​and content are growing ... If anyone has any bright ideas about this, I would like to hear them!

Btw, if you need to guess the user's location by IP, you can check geoIP .

+5
source

Use a template system. Smarty Template Engine is probably one of the most famous PHP. Not only does the template system fulfill the exact goal you are trying to accomplish, it also simplifies page maintenance by separating the displayed code from the content (which also allows you to use the same template for many different content pages of a similar nature).

+4
source

As the easiest way, I recommend using the i18n internationalization method and gettext (.po files).
The famous WordPress project using it as well .

+2
source

1 . Duplication of the entire site will force you to repeat each code relating to two folders: - [

2 . If you mean somenting like

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

This solution is ideal for managing two languages ​​on one page.
But you still have duplicate tags.

3 . Only change content that it truly satisfies.
Maybe the proliferation of if commands can affect the compilation of php ... I don't know.
In any case, the document may 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 analyzed AFTER php compilation.
This means that strings are affected, for example, for example. from the echo instruction, not inside <? php? >.
Also, content coming from php includes an IS file.
But NOT external css or javascript.

The attention divider # not a symbol used elsewhere. You might prefer to replace || or ^^

Of course, in the future you will be able to adapt this solution in 3 languages ​​or more. But if you need to insert a "third language translation" in many lines of a large site, perhaps a solution from MatW is right for you.

0
source

All Articles