How to view foreign language(i.e. Bangla) characters in CakePHP?

Here's my code inside a .ctp file in CakePHP:

<div class="AccordionPanel">
        <div class="AccordionPanelTab">জন্ম নিবন্ধন</div>
        <div class="AccordionPanelContent">
            <?php echo $html->link(__('থানা', true), '/ReportBirthRegistrationStations/'); ?>
            <?php echo $html->link(__('অফিসার', true), '/ReportBirthRegistrationOfficers/'); ?>
         </div>
</div>

So it's pretty clear what I'm trying to do: I'm trying to write Bangla characters inside my view code in my CakePHP project, and I want to display them in my view page.

When I try to load the view page - weird, unreadable characters appear instead of Bangla characters.

I included this:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

the UTF-8 encoding at the top of the file, but still it didn't work.

I included the UTF-8 encoding in view layout files as well, but still facing the problem.

How can I view Bangla or any other foreign characters in CakePHP? IS there any support in CakePHP for Bangla?

My IDE is Netbeans 8.0.1.

My CakePHP version is 1.2.5, and PHP version is 5.2 [due to working in a very old project that is being maintained since 2008-09]

Edit - 1:

I've tried adding the following -

-J-Dfile.encoding=UTF-8

in my netbeans.conf file in etc folder of my Netbeans installation folder. By doing that, I wanted to make sure that my file is saved in UTF-8 encoding. But my problem is still not solved.

Edit - 2:

Forgot to mention one important point. Whatever I'm trying to display, has nothing to do with my database values. These are just labels, I just want to view these labels in Bangla on page load.

Upvotes: 3

Views: 1680

Answers (3)

Actually, the problem was in my default.ctp file in app/views/layouts folder.

The <meta http-equiv> tag there was not UTF-8 encoded, it was ISO-8859-1 encoded. Like the following:

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

This is Cake standard - the default.ctp view file in the app/views/layouts folder is shared throughout the entire application and it defines the default behavior and shared attributes of all view pages.

Since this is a very old project and the HTML version back then was HTML 4, the code was written this way. UTF-8 encoding is supported by HTML5. Check the following for more details: http://www.w3schools.com/tags/ref_charactersets.asp

I just had to change this to UTF-8 encoded and then it worked.

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

The problem had nothing to do with Cake version, nor with PHP version. Rather, it was all about the HTML version and corresponding code that created the confusion.

Upvotes: 1

Gerd K
Gerd K

Reputation: 1147

Your web server does probably not set the correct content type for the request.

You could try to set it manually in your AppController in the beforeRender();

header('Content-Type: text/html; charset=utf-8');

Also note that when you write something to the out stream in the controller before the headers are sent to the client, that also the encoding will be broken. (Happens often in debug mode)

Upvotes: 4

Len_D
Len_D

Reputation: 1420

Add this code to your ~/app/config/core.php file:

Configure::write('App.encoding', 'UTF-8');

And in your page section add:

 <?php echo $html->charset(); ?> 

Upvotes: 4

Related Questions