Learning Is Fun

Talks on Web Technology and Better Product Development

PHP & MySQL: Creating a website in your local language smoothly

December29

Hello Developer,

You know English is the international language and accepted as international communication. So, most of the websites have been developed in English. But in many other times, a developer needs to work with local languages where they are developing a website in PHP. If you develop in local language, you need to know a small trick and that will show your content properly in all browsers smoothly.

So, what is the technique?

Well. Let me explain step by step.

Step One: An observation
I will show you the source code of two Bengali newspaper. In this newspaper: http://www.prothom-alo.com, the fonts and text comes properly in all browsers. But in this newspaper: http://www.ittefaq.com, it can show text and fonts properly only in Internet Explorer.

Now the matter is we must make sure our content will be displayed properly in any browser. Right?

OK. So what is the difference between the two websites I just gave example?

If you take a look at the source code of the first website’s content, you will find it is like this:সઃপাদক

This is another website that had been developed as smooth site in the same technique: http://www.bdnews24.com/bangla. If you check its code, you will see that it uses same type of code for its text.

So, what are these পাদকীয় things? It is very interesting that these are universal representation of local language in HTML entities. For every character of any local language, there is a unique and fixed symbol defined such as প in HTML entities. When you bring this kind of text in your browser source code, the site content looks smooth without any break and fonts displays properly.

Now if you open the source code of http://www.ittefaq.com, you will see something like AvR beg RvZxq msm` wbe©vP‡b jovB n‡e †RvU-gnv‡Rv‡Ui g‡a¨. The matter is they are also showing Bengali news content, but in a different way that is not useful in cross-broswer platform. This is often totally recognized by only Internet Explorer and often partially recognized by other browsers.

Step Two: Storing your local language content in the database
You see I have some content in my local language (Bangla) in the database.

Step Three: Converting your local language text in Universal code
Well. This is extremely easy.

<?php
	class UnicodeHandler
	{
		var $dbLink;
		var $sqlQuery;
		var $dbResult;
		var $dbRow;
		var $salary;
		var $bonus;
		/* Use this constructor in case your PHP version is 4. */
/*
		function UnicodeHandler()
		{
			$this->dbLink = '';
			$this->sqlQuery = '';
			$this->dbResult = '';
			$this->dbRow = '';
			$this->mySalary = 0;
			$this->myBonus = 0;
			$this->dbLink = mysql_connect('localhost', 'root', '');
			mysql_query("SET character_set_results=utf8", $this->dbLink);
			mb_language('uni');
			mb_internal_encoding('UTF-8');
			mysql_select_db('test', $this->dbLink);
			mysql_query("set names 'utf8'",$this->dbLink);
		}
*/
		/* Use this constructor in case your PHP version is 5 or 5+. */
		/* I assume you are using PHP 5 or 5+. */
		function __construct()
		{
			$this->dbLink = '';
			$this->sqlQuery = '';
			$this->dbResult = '';
			$this->dbRow = '';
			$this->mySalary = 0;
			$this->myBonus = 0;
			$this->dbLink = mysql_connect('localhost', 'root', '');
			mysql_query("SET character_set_results=utf8", $this->dbLink);
			mb_language('uni');
			mb_internal_encoding('UTF-8');
			mysql_select_db('test', $this->dbLink);
			mysql_query("set names 'utf8'",$this->dbLink);
		}
		function displayLocalContent()
		{
			mysql_query("SET character_set_results=utf8", $this->dbLink);
			$this->sqlQuery = "SELECT * FROM unitext ";
			$this->dbResult = mysql_query($this->sqlQuery, $this->dbLink);
			while($this->dbRow = mysql_fetch_object($this->dbResult))
			{
				echo 'News: ' . $this->convertToLocalHtml($this->dbRow->news) . '<br />';
			}
		}
		function convertToLocalHtml($localHtmlEquivalent)
		{
			$localHtmlEquivalent = mb_convert_encoding($localHtmlEquivalent,"HTML-ENTITIES","UTF-8");
			return $localHtmlEquivalent;
		}
	}
?>

The displayLocalContent() function is going to display the news and convertToLocalHtml() function converts my utf-8 content to HTML entities.

And here in index.php I am executing my displayLocalContent() function.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Displaying Your Local Language</title>
</head>
<body>
<?php
	include_once 'class.unicode.php';
	$unicodeObject = new UnicodeHandler();
	$unicodeObject->displayLocalContent();
?>
</body>
</html>

As a result, the output is now as ঢাকা, ডিসে etc.

Step Four: You need to check my other posting for SELECT, INSERT & UPDATE your local language.
Here goes my other article so that you can perform all required operations for storing and displaying information in browser smoothly:
http://www.tanzilo.com/2008/10/13/php-mysql-unicode-solution-to-chinese-russian-or-any-language/

An alternative way
I have an alternative way in my mind and that is when you store the data in the database, you can first convert them to universal code such as &#2453;&#2496;&#2527;and then directly show your text.

This article will cover any language such as Afrikaans, Albanian, Amharic, Arabic, Armenian, Assamese, Aymara, Azeri, Belarusian, Bengali, Bislama, Bosnian, Bulgarian, Burmese, Catalan, Chinese, Mandarin,Croatian, Czech, Danish, Dari, Dhivehi, Dutch, Dzongkha, English, Esperanto, Estonian, Fijian, Filipino, Finnish, French, Frisian,  Gagauz, Georgian, German, Greek, Guaraní, Gujarati, Haitian Creole, Hebrew, Hindi, Hiri Motu, Hungarian, Icelandic, Indonesian, Italian, Japanese, Kannada, Kashmiri, Kazakh, Khmer, Korean, Kurdish, Kyrgyz, Lao, Latvian, Lithuanian, Luxembourgish, Macedonian, Malagasy, Malay, Malayalam, Maltese, Māori, Marathi, Mayan, Moldovan, Mongolian, Montenegrin,Náhuatl, Ndebele, Nepali, New Zealand Sign Language, Northern Sotho, Norwegian, Oriya, Papiamento, Pashto, Persian, Polish, Portuguese, Punjabi, Quechua, Romanian, Rhaeto-Romansh, Russian, Sanskrit, Serbian, Shona, Sindhi, Sinhala, Slovak, Slovene, Somali, Sotho, Spanish, Swahili, Swati, Swedish, Tagalog, Tajik, Tamil, Telugu, Tetum, Thai, Tok Pisin, Tsonga, Tswana, Turkish, Turkmen, Ukrainian, Urdu, Uzbek, Venda, Vietnamese, Welsh, Xhosa, Yiddish, Zulu etc.

You can also download this small piece of code from here:
http://www.tanzilo.com/demo/code/convert_to_unicode/convert_to_unicode.zip

Thank you for reading.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
posted under Uncategorized
22 Comments to

“PHP & MySQL: Creating a website in your local language smoothly”

  1. On December 29th, 2008 at 7:16 am Brian Barker Says:

    As far as learning another language is concerned, can I put in a word for Esperanto?

    During a short period of 121 years Esperanto is now placed within the top 100 languages, out of 6,000 worldwide, according to the CIA factbook. It is the 17th most used language in Wikipedia, and in use by Skype, Firefox and Facebook.

    Native Esperanto speakers, include George Soros, Nobel Laureate Daniel Bovet, Ulrich Brandenberg, the new German Ambassador to NATO, and World Champion Chess Player, Susan Polger.

    Further arguments can be seen at http://video.google.com/videoplay?docid=-8837438938991452670 and a glimpse of the language can be seen at http://www.lernu.net

  2. On December 29th, 2008 at 8:37 am admin Says:

    @ Brian

    OK.
    Esperanto is added!
    :)

  3. On March 21st, 2009 at 1:07 am Bdbangla Says:

    Nice post .. I will play around with your code tonight to develop my new site.. anyway thanks for this post this is really helpful

  4. On March 25th, 2009 at 6:00 am swathi Says:

    how to insert telugu font in database…..can u please answer me

  5. On March 25th, 2009 at 9:44 am admin Says:

    @ swathi

    there is no need to insert Telegu font in the database.

    if you make your site in UTF-8:

    (i) if the user has Telegu font, she can see your site properly

    (ii) if the user does not have Telegu font installed in her PC, keep a font download page and give a link to download the font and how to install the font. next, request the user to refresh the page after the font is installed

  6. On April 22nd, 2009 at 3:22 am S.G. Hussain Says:

    Hi,
    Well, now I am able to retrive the characters from mysql stored in hindi/Urdu. So one part is solved. Another part is that the browser sourse page still display the Universal code and not the hindi/Urdu. I’d like that the browser sourse page should also display local language and not the Universal codes. Like your example: ঢাকা, ডিসে on this page.
    Thanks.

  7. On April 22nd, 2009 at 5:27 am admin Says:

    @ S.G. Hussain

    If you want so, you need to convert your UNICODE back to local language.

    I think PHP has built-in function for this.

  8. On April 22nd, 2009 at 8:31 am S.G. Hussain Says:

    Hi there,
    I’m sorry to enumerate the problem in such a detail. It was something to do with connect function and defining language there itself. I tried. It works now.
    Thanks

  9. On April 22nd, 2009 at 8:45 am admin Says:

    @ S.G. Hussain

    Ya.
    That is what I meant for you to do.

    And happy to know that your problem is solved.

  10. On July 2nd, 2009 at 4:52 am Narayan Says:

    Thank you this helped me a lot.

  11. On July 15th, 2009 at 7:56 am josh Says:

    i have arabic text already iserted into the database.

    now,i want to convert it into the english,i have already made the script for translation.But have prblm with it.

    whent i translate the text into english it shows strang characters when printed and database is remained empty when inserted this text into it.
    if u can hepl me i’ll give u more detaill.

  12. On August 26th, 2009 at 12:27 am rosee Says:

    can help me in storing the Tamil font in phpMySQL.

    When i copy a tamil text from the browser it is displaying, but when i type in the MSword and copy them to store in MySQL TABLE. It is showing only the keyboard english characters.
    So plz guide me sir and mail the answer to my ID

  13. On October 8th, 2009 at 3:43 am ashiq Says:

    your website very nice……….

  14. On October 8th, 2009 at 3:45 am ashiq Says:

    can u tell me ,how to show a video in our web site?

  15. On October 8th, 2009 at 8:13 am admin Says:

    @ ashiq

    use wordpress plugin or youtube.com embedded code.

  16. On November 21st, 2009 at 2:44 am Zaidul Haque Says:

    I can not see font in my website.
    Please send me language of PHP for input the font in my website.
    I write contents in sutonnyMj font.
    I have seen source code of http://www.ittefaq.com, http://www.jugantor.com
    all are using Bangla_css.php
    But I can not find it how to create and how is functioning properly

  17. On February 4th, 2010 at 2:14 am Kumar Says:

    nice.. keep it up

  18. On February 22nd, 2010 at 6:10 am kishore Says:

    @Admin : hi sir, this is looking good…i m getting the output from db wat i want as telugu script…. how can i insert data into db in telugu font? plz do needful..

    thanks

  19. On February 22nd, 2010 at 6:17 am kishore Says:

    @Admin : when i m trying to insert data into DB , it takes as different formate. wat i need to do? plz explain me clearly….

  20. On March 12th, 2010 at 2:22 am ramakrishna Says:

    i have a table named search in that i have keywords,here i want to crawl the results from the web for these keywords i did this fine for english now i want to crawl the results for slovak language i mean for slovak keywords
    i put the collation for table and database ” utf8_unicode_ci ” but it not worked properly it shows the warning message like “mysql_query() expects parameter 2 to be resource, null given”
    can u please help me.

  21. On March 12th, 2010 at 2:36 am admin Says:

    @ ramakrishna

    I think you missed somewhere.

    Check this one:
    http://www.tanzilo.com/2008/10/13/php-mysql-unicode-solution-to-chinese-russian-or-any-language/

  22. On July 12th, 2010 at 1:31 am Niraj Jari Says:

    Respected Sir,

    I want to convert a whole English Website to Gujarati when user clicks on Gujarati & vice versa… The Website is Dynamic & the data is to be entered by Admin…, Can You help me in this? Plz let me know how this can be done? How data daily typed from Admin Panel in English can be converted in Gujarati by just 1 click & vice versa??

Email will not be published

Website example

Your Comment: