The first thing we need to do is to create a couple of files that will contain the text for each of the languages that will be supported by the website. For demo purpose I have chosen English, Spanish and German. Make a directory named “directory”. In this directory create 3 files: lang.de.php, lang.en.php, and lang.es.php. In our main file (index.php) we will include a file (common.php) containing a piece of code that gets the requested language. Here’s the content of those 3 language files:
https://bc.anglican.ca/blog/2014/05/22/surprised-grace#comment-874
http://ageofstupid.tao.de/people/alvin_duvernay#comment-13021
http://www.stockholmradio.se/nyheter/2012/juni/20/stockholmradio-leverantoer-offshore-race#comment-9717
http://esto.tomsk.gov.ru/activity/14958/?MID=894&result=reply#message894
http://sas.scrippscollege.edu/sas-blog/welcome-back
http://mediamusings.dsc.rmit.edu.au/2013/10/18/sorry-to-the-world/comment-page-7/#comment-118562
http://newsservice.gordon.edu/2013/10/final-school-committee-forum-explores.html?showComment=1487311072219#c7295692958611639216
http://www.dc3.edu/mobile/athletics/mens-sports/rodeo/2012/06/22/conq-athletics-have-10-academic-all-americans
http://gahi.ntua.edu.tw/community/viewtopic.php?CID=4&Topic_ID=114
https://portfolio.cssd.ac.uk/view/artefact.php?view=21&artefact=179&offset=100
http://blog.goo.ne.jp/kossykossy/e/70bfcbe26c7213b1397c47c999ad353e?st=0#comment-form
http://www.aliciasvigals.com/blog.php?category=&entry=4404#new_comment
http://www.artitude.eu/?p=articolo&categoria=news&id_pill=2286&#commenti
http://www.fiverdesign.co.uk/blog/drupal/drupal-advanced-forum-how-to-add-advertising/
http://camerondokey.com/blog/2012/11/12/coming-up-for-air
http://www.imaginecentralarkansas.org/news/pop-park-hill?page=3#comment-3760
http://www.fiverdesign.co.uk/blog/drupal/drupal-sciencecamp-cambridge-2012-recap/
https://blogs.technet.microsoft.com/office_integration__sharepoint/2015/04/20/office-2013-error-certificate-error-the-application-experienced-an-internal-error-loading-the-ssl-libraries-when-opening-files/#comment-7315
http://www.successfulblogging.com/what-you-need-to-know-about-blogging-but-no-ones-telling-you/#comment-247028
http://kikolani.com/social-distraction-aka-media-adventures.html
http://codes-sources.commentcamarche.net/forum/affich-606342-upload-de-plusieurs-fichiers#p10073596
http://www.cds.edu/?attachment_id=1258#comment-364200
http://janu-herjanto.blog.ugm.ac.id/2012/06/24/hello-world/#comment-37358
http://china.uniongraduatecollege.edu/2012/12/more-shanghai-touring-happy-group-shot.html?showComment=1487356583104#c1212485099246969240
http://newsservice.gordon.edu/2010/01/local-band-sharing-spotlight-with-haiti.html?showComment=1487356654640#c463871985733499185
http://mediamusings.dsc.rmit.edu.au/2012/09/28/who-do-you-trust-reviews-in-the-online-world/comment-page-1/#comment-118645
http://portal.edu.ro/index.php/articles/19172?
http://beasiswa.mercubuana.ac.id/2010/11/meees-masters-scholarships-in.html?showComment=1487357385192#c5775888623494275525
http://gahi.ntua.edu.tw/community/viewtopic.php?CID=4&Topic_ID=112
https://www.blogger.com/comment.g?blogID=2262100939310697869&postID=8671808897589769833&isPopup=true
http://blog.mohe.gov.my/2012/11/pindah-kredit-maksimum.html?showComment=1487358200212#c1374801140296663084
https://bc.anglican.ca/blog/2014/05/22/surprised-grace#comment-874
http://ageofstupid.tao.de/people/alvin_duvernay#comment-13021
http://www.stockholmradio.se/nyheter/2012/juni/20/stockholmradio-leverantoer-offshore-race#comment-9717
http://esto.tomsk.gov.ru/activity/14958/?MID=894&result=reply#message894
http://sas.scrippscollege.edu/sas-blog/welcome-back
http://mediamusings.dsc.rmit.edu.au/2013/10/18/sorry-to-the-world/comment-page-7/#comment-118562
http://newsservice.gordon.edu/2013/10/final-school-committee-forum-explores.html?showComment=1487311072219#c7295692958611639216
http://www.dc3.edu/mobile/athletics/mens-sports/rodeo/2012/06/22/conq-athletics-have-10-academic-all-americans
http://gahi.ntua.edu.tw/community/viewtopic.php?CID=4&Topic_ID=114
https://portfolio.cssd.ac.uk/view/artefact.php?view=21&artefact=179&offset=100
http://blog.goo.ne.jp/kossykossy/e/70bfcbe26c7213b1397c47c999ad353e?st=0#comment-form
http://www.aliciasvigals.com/blog.php?category=&entry=4404#new_comment
http://www.artitude.eu/?p=articolo&categoria=news&id_pill=2286&#commenti
http://www.fiverdesign.co.uk/blog/drupal/drupal-advanced-forum-how-to-add-advertising/
http://camerondokey.com/blog/2012/11/12/coming-up-for-air
As you can notice, some constants are created using the define() function. In every file the defined constants have the same name, bu the values is different. We will output the values of the constants inside the index.php file. Therefore we will see different text every time we will call other language file.
Determine the right language
Let’s analyze the common.php file:
http://www.imaginecentralarkansas.org/news/pop-park-hill?page=3#comment-3760
http://www.fiverdesign.co.uk/blog/drupal/drupal-sciencecamp-cambridge-2012-recap/
https://blogs.technet.microsoft.com/office_integration__sharepoint/2015/04/20/office-2013-error-certificate-error-the-application-experienced-an-internal-error-loading-the-ssl-libraries-when-opening-files/#comment-7315
http://www.successfulblogging.com/what-you-need-to-know-about-blogging-but-no-ones-telling-you/#comment-247028
http://kikolani.com/social-distraction-aka-media-adventures.html
http://codes-sources.commentcamarche.net/forum/affich-606342-upload-de-plusieurs-fichiers#p10073596
http://www.cds.edu/?attachment_id=1258#comment-364200
http://janu-herjanto.blog.ugm.ac.id/2012/06/24/hello-world/#comment-37358
http://china.uniongraduatecollege.edu/2012/12/more-shanghai-touring-happy-group-shot.html?showComment=1487356583104#c1212485099246969240
http://newsservice.gordon.edu/2010/01/local-band-sharing-spotlight-with-haiti.html?showComment=1487356654640#c463871985733499185
http://mediamusings.dsc.rmit.edu.au/2012/09/28/who-do-you-trust-reviews-in-the-online-world/comment-page-1/#comment-118645
http://portal.edu.ro/index.php/articles/19172?
http://beasiswa.mercubuana.ac.id/2010/11/meees-masters-scholarships-in.html?showComment=1487357385192#c5775888623494275525
http://gahi.ntua.edu.tw/community/viewtopic.php?CID=4&Topic_ID=112
https://www.blogger.com/comment.g?blogID=2262100939310697869&postID=8671808897589769833&isPopup=true
http://blog.mohe.gov.my/2012/11/pindah-kredit-maksimum.html?showComment=1487358200212#c1374801140296663084
After we determine the value of $lang, we use switch() to compare its value with some different values, and execute a different piece of code depending on which value it equals to. After the value of the $lang_file is determined, the script will include the necessary language file. As you can see I have used sessions to register the value of $lang. This way users can navigate through the whole site and see the content in the chosen language (lang=[language here] does not need to be passed in every URL). Additionally, I have used cookies to store the selected language in users computer for 30 days. When the visitor will come back he will see the site in the language that he previously selected.
How if the website’s language requested?
In this demo I have chosen to use some image flags, each image having a link to index.php?lang=[LANG HERE]. So, to see the site in german we will use the German image flag which links toindex.php?lang=de.
Lastly, the constants values should be outputted in the page. Examples