Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

multiple language

Hello,

I'm interested if someone knows what is the best way of making multiple language website.

1 Answer

This is called "localization". With websites that typically happens is there are a bunch of files (one for each language) with a key value pair. The key being common to the site and the value being the language. When the language is chosen (or detected) the site knows which file to load, and then uses that file to add the languages content to the website based on the key in the file.

So if i have contact page i should have two files for it
e.g contact-bg.php contact-eng.php ?

No, you only need one contact page you would do this.

dictionaries/english.php

<?

$dictionary = array(
  'contact' = > array('
                                 'first_name' => 'First Name'
                                 'last_name' => 'Last Name'
                                 'email' => 'email address'
                               ')
);

dictionaries/japanese.php

<?

$dictionary = array(
  'contact' = > array('
                                 'first_name' => 'First Name Jap'
                                 'last_name' => 'Last Name Jap'
                                 'email' => 'email address Jap'
                               ')
);

So you have 2 files with 2 multiplex arrays one for english one for Japanese

'''

<?php

// Get the language from the http headers. // It returns en-us for (english-us) jp-jp for Japanese Japan etc. // this could also be done in the URL /en/contact and have mod rewrite figure out the language and // page based on the URL $http_lang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);

// based on the http_lang decide if it's japanese dictionary or another dictionary // defaulting to english in this case. switch($http_lang) { case 'ja': require_once('dictionaries/japanese.php'); break; default: require_once('dictionaries/english.php'); } ?>

<!-- now output the label based by the dictionary this allows you to do everything on one page. --> <label><?php echo $dictionary['contact']['first_name']; ?></label>

Does that make sense?