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

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.

{code type=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) . ‘
‘;
}

}

function convertToLocalHtml($localHtmlEquivalent)
{

$localHtmlEquivalent = mb_convert_encoding($localHtmlEquivalent,”HTML-ENTITIES”,”UTF-8″);
return $localHtmlEquivalent;
}
}

?>
{/code}

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.

{code type=PHP}




include_once 'class.unicode.php';
$unicodeObject = new UnicodeHandler();
$unicodeObject->displayLocalContent();

?>


{/code}

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 কীয়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.

43 thoughts on “PHP & MySQL: Creating a website in your local language smoothly

  1. 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. @ 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

  3. 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.

  4. @ 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.

  5. 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

  6. @ S.G. Hussain

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

    And happy to know that your problem is solved.

  7. 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.

  8. 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

  9. @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

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

  11. 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.

  12. 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??

  13. Can u help me to submit bangla data using ajax into mysql table?

    if i submit it stored as
    চিকিৎসা

  14. @ Bashar

    try to use jQuery post method.

    i am working in a big bangla project where I am using Ajax now and then with the help of jQuery get & post methods.

  15. Hi,

    I am creating a website in telugu using PHP, MySQL. I need to display the telugu in eenadu font. I have to type telugu in MS-Word or Notepad and have to copy n paste in Admin section. My need is all text will be displayed in Admin section as well as in front end in eenadu font i.e. in Telugu. Can you tell me how can I do this using PHP, MySQL?

  16. @ swagata

    You need to embed fonts. Nowadays there are scripts that helps to show custom fonts as it it. I used something in Javascript such as for wordpress I used “wp-cufon” plugin.

  17. I need clear and easy way of inserting telugu font into a webpage so that the website can accessible in any PC/computer without telugu software being installed so far. Give me idea/suggestion about uploading local language font into the database.. Please..

  18. i m using ckeditor with marathi hindi lan and was getting problem.
    Your Below line worked for me !
    Many thanks !

    Thanks Admin !

    //——————————————-
    $localHtmlEquivalent = mb_convert_encoding($localHtmlEquivalent,”HTML-ENTITIES”,”UTF-8″);

  19. when I using this, I got the message that error that is Fatal error: Call to undefined function mb_language() in ../class.unicode.php on line. that line is mb_language(‘uni’); what is the real problem.

  20. Hi, Could you please tell me if we can translate the language form english to nepali using php and mysql. I am doing this project and my client wants both in english and nepali.

  21. Thanks

    I need to write below line every time when I call mysql query through PHP.

    mysql_query(“SET character_set_results=utf8″, $this->dbLink);
    mb_language(‘uni’);
    mb_internal_encoding(‘UTF-8′);

  22. Thank you very much

    I need to write below line every time when I call mysql query through PHP.

    mysql_query(“SET character_set_results=utf8″, $this->dbLink);
    mb_language(‘uni’);
    mb_internal_encoding(‘UTF-8′);

  23. Hello!
    your website is so pretty and pretty useful too! Thanks a ton.
    I have a question regarding the below –
    //
    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 কীয়and then directly show your text.
    //
    currently my table has unicode values only, and i’m trying to compare/analyze it against the form field (input type text) .
    So …how do you “first convert them to universal code” in php?
    Please advise. Thanks in advance!

  24. @ Poongothai

    There is a PHP function for this. I used the function long ago. But now I cannot remember the name of the function.

  25. Thanks for the quick response.
    hmm, I think you are talking about utf_encode… it did not work for me . But I would have messed up somewhere- for this is the first and hopefully last php page that I code. I just tried mb_strtoupper into another variable, and it is working as expected. I know it is silly, but why question something which is working – so I let it be. :-)

  26. I am trying to launch odia (oriya) news website and I have installed wordpress and for content write in oriya using INDIC IME plugin. My problem is in windows 7 it is showing something and windows xp the oriya letter letters are different. As you have mentioned I tried that class but still not able to see same letters in both the system.

    the other problem is in windows xp system if font is not installed it is displayed box box…

    I hope you will help me out.

    Thanks in advance

    Regards

    Sibaram

  27. Thanks for this article, your instructions helped me to work with my language text (Kurdish).

  28. i’m new at web development and want to develop site in “marathi” i am using macromedia dreamweaver i am facing problem ” Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in C:\wamp\www\class.unicode.php on line 46″ plz reply.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>