Multi-Language Support

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
rozvinbm_jp
Forum Commoner
Posts: 43
Joined: Thu Jun 14, 2007 8:36 pm
Location: Fuji-shi, Shizuoka-ken, Japan

Multi-Language Support

Post by rozvinbm_jp »

Please tell everybody on how to support different languages using AJAX. I've been searching this for a long time in many forums in the internet but they did not solved or post the definite solutions.

Here's the printscreen: (please do copy and paste if not displayed here.)

output via php and html (http://anime.geocities.jp/rozvinbm_jp/shops.JPG)
Image

output via php, html and processed with Ajax (http://anime.geocities.jp/rozvinbm_jp/shopsajax.JPG)
Image


In addition, here is my code to explicitly defined the encoding:

Code: Select all

<!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=shift_jis">
......

Code: Select all

var xmlHttp;
var m_placeholder;

function executeProcess(serverscriptfile, placeholder, posts) {
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
    m_placeholder = placeholder;
    var url=serverscriptfile;
    url=url+"?sid="+Math.random();
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=Shift-JIS'");
    xmlHttp.send(posts);
}

function stateChanged() {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        document.getElementById(m_placeholder).innerHTML=xmlHttp.responseText;
    }
}

function GetXmlHttpObject() {
    var xmlHttp=null;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        //Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

try sending a header in your ajax response:

Code: Select all

header('Content-Type: text/html; charset=shift_jis');
(or is UTF-8 supports those characters, which I imagine it does:)

Code: Select all

header('Content-Type: text/html; charset=utf-8');
Also, you may be limited to the charset of the document since it's ajax. If you need to switch charsets you may have to reload the entire document with the new charset declared. If you are mixing chasets, maybe UTF-8 is the way to go?
rozvinbm_jp
Forum Commoner
Posts: 43
Joined: Thu Jun 14, 2007 8:36 pm
Location: Fuji-shi, Shizuoka-ken, Japan

Post by rozvinbm_jp »

Thanks a lot. Here's the result

It is SOLVED using the below php code:

Code: Select all

header('Content-Type: text/html; charset=shift_jis');
But using the below php code was worst because during the very first load of the page (before calling the ajax) became garbage too. Any comments please.

Code: Select all

header('Content-Type: text/html; charset=utf-8');
I changed the HTML encoding and PHP header function strign value to charset UTF-8

I have no plan to support other language for now, It is only Japanese Language.
rozvinbm_jp
Forum Commoner
Posts: 43
Joined: Thu Jun 14, 2007 8:36 pm
Location: Fuji-shi, Shizuoka-ken, Japan

still garbage output after GetXmlHttpObject.send(posts);

Post by rozvinbm_jp »

When the AJAX calls the php file, I've noticed the data in the $_POST are garbage data.

I think the problem is during GetXmlHttpObject execution to past the Posted data to php file.

Please teach me to set the charset in AJAX supporting Japanese charset.

Currently, I am using the following settings:

Code: Select all

xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=shift_jis'");

Code: Select all

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

Code: Select all

<meta http-equiv="Content-Type" content="text/html; charset=shift_jis">
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I'll bet it's your PHP config that's messing up the charset of the AJAX response.

You can collect the response with an output buffer and run:

Code: Select all

echo mb_convert_encoding($buffer, "SJIS");
before sending the AJAX result.

Or, if you have access to your php.ini check that your "internal_encoding" is set to SJIS. You may also want to set html_input and html_output the same way.

Let me know if that helps!
rozvinbm_jp
Forum Commoner
Posts: 43
Joined: Thu Jun 14, 2007 8:36 pm
Location: Fuji-shi, Shizuoka-ken, Japan

Post by rozvinbm_jp »

1) I don't know how to get the field value from HTML to PHP.
I cannot test the below code:

Code: Select all

echo mb_convert_encoding($buffer, 'SJIS');
2)setting in php.ini:
mbstring.internal_encoding = SJIS
mbstring.http_input = SJIS
mbstring.http_output = SJIS
then restart the webserver.

problem still (garbage output by checking the $_POST['fieldname']).
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Ah, I think I understand your problem now.

From http://swik.net/Development?popular :
Normal forms are sent using the encoding of the parent page. Thus a SJIS encoded page will default to sending form content encoded in SJIS. Ajax submitted forms on the other hand will be sent as UTF-8. If for some strange reason, UTF-8 is not the character set of choice for the server, this will require a solution such as the server recognizing and translating UTF-8 responses to a desired character encoding.
Try converting the $_POST['fieldname'] to SJIS like so:

Code: Select all

$_POST['fieldname'] = mb_convert_encoding($_POST['fieldname'], "SJIS");
also, be aware of: http://www.php.net/mb_string#id3215097 ... maybe the encoding_translation directive could be useful?
rozvinbm_jp
Forum Commoner
Posts: 43
Joined: Thu Jun 14, 2007 8:36 pm
Location: Fuji-shi, Shizuoka-ken, Japan

Post by rozvinbm_jp »

First, I added the mbstring function.

Code: Select all

$shopname = mb_convert_encoding($_POST['shopname'], "SJIS");
But it occurred error "Undefined function". The mbstring functions are not installed.

I set the php.ini according to php.net documentation

extension=php_mbstring.dll
mbstring settings for SJIS


but it turns to garbage at the very first loading of the page (AJAX is not yet processed).
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

double check that the mb_string functions are actually loaded by doing a php_info()
rozvinbm_jp
Forum Commoner
Posts: 43
Joined: Thu Jun 14, 2007 8:36 pm
Location: Fuji-shi, Shizuoka-ken, Japan

Post by rozvinbm_jp »

I cannot install the mbstring functions.
But I found iconv function.. it works well.

here's the php code:

Code: Select all

$str = iconv("UTF-8", "SJIS", $str);
I do the above code upon calling the php file by the XMLHttpRequest.

I have new problem when I release from the debugging season (on production). I will post new topic because it is not related in encoding.

Thank you very much.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

oh right - iconv :-)

I immediately thought of mb_string since it was designed for multi-byte Japanese charsets. If iconv does the trick for you more power to you!
thewebdrivers
Forum Commoner
Posts: 41
Joined: Fri Aug 17, 2007 3:32 pm
Location: india
Contact:

Post by thewebdrivers »

i think iconv is php version dependent. no?
Post Reply