Google API with JSON

Hi,

I have been using this since Delphi 6 when using the google translate API service
Below is a snippet of the bit I am interested in

  if Source=Autodetect then
    EncodedRequest:=Format(GoogleTranslateUrlAuto,[GoogleLanguageApiKey,GoogleLanguagesArr[Dest],HTTPEncode(Text)])
  else
    EncodedRequest:=Format(GoogleTranslateUrl,[GoogleLanguageApiKey,HTTPEncode(UTF8Encode(Text)),GoogleLanguagesArr[Source],GoogleLanguagesArr[Dest]]);

  XMLHTTPRequest := CoXMLHTTPRequest.Create;
  XMLHTTPRequest.open('GET', EncodedRequest, False, EmptyParam, EmptyParam);
  XMLHTTPRequest.send('');
  Response:=XMLHTTPRequest.responseText;

Open in new window


The new code can be found at
http://theroadtodelphi.wordpress.com/2010/12/04/using-the-google-translate-api-v2-labs-from-delphi/

I'm using this in XE3 now and the only bit that seems to have changed is
XMLHTTPRequest := CoXMLHTTP.Create;

Open in new window


But I am just getting ???? back now for things such as Japanese and Korean.
Has anyone else had this problem?
LVL 15
mikelittlewoodAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ArgentiCommented:
Please check if you are using the right encoding while sending and receiving string data.
Starting from v. 2010 (?) the default string type is UnicodeString, supporting multi-byte characters.

http://docwiki.embarcadero.com/RADStudio/XE3/en/String_Types
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sinisa VukSoftware architectCommented:
You trying to translate to Japanese and Korean? Do you have fonts installed that supports these...
0
mikelittlewoodAuthor Commented:
I'm trying to translate from Korean to English.
Yes I have all the fonts, like I said, it works in Delphi6 fine now on my PC.
The only difference in the coding is the creation of the XMLHTTPRequest object.
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

mikelittlewoodAuthor Commented:
Hi Argenti,

I haven't specified any encoding type, I've never had to before, why do I now?
Where in this function does it require extra encoding information, and what would that be in this case?

function Translate_DBXJSON(const Text:string;Source,Dest:TGoogleLanguages):string;
var
  XMLHTTPRequest: IXMLHTTPRequest;
  EncodedRequest: string;
  json          : TJSONObject;
  jPair         : TJSONPair;
  jValue        : TJSONValue;
  Response      : string;
begin
  Result:='';
  if Source=Autodetect then //buil the URI
    EncodedRequest:=Format(GoogleTranslateUrlAuto,[GoogleLanguageApiKey,GoogleLanguagesArr[Dest],HTTPEncode(Text)])
  else
    EncodedRequest:=Format(GoogleTranslateUrl,[GoogleLanguageApiKey,HTTPEncode(Text),GoogleLanguagesArr[Source],GoogleLanguagesArr[Dest]]);
 
  XMLHTTPRequest := CoXMLHTTP.Create;
  XMLHTTPRequest.open('GET', EncodedRequest, False, EmptyParam, EmptyParam); //make the request
  XMLHTTPRequest.send('');
  Response:=XMLHTTPRequest.responseText;
 
  if Response<>'' then
  begin
      json    := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(Response),0) as TJSONObject; //create a TJSONObject instance
    try
      jPair   := json.Get(0);
      if jPair.JsonString.value='error' then //if error in response
        Result := Format('Error Code %s message %s',[TJSONObject(jPair.JsonValue).Get(1).JsonValue.Value,TJSONObject(jPair.JsonValue).Get(2).JsonValue.Value])
      else //all ok, show the response,
      begin
        jValue := TJSONArray(TJSONObject(jPair.JsonValue).Get(0).JsonValue).Get(0);
        Result := TJSONObject(jValue).Get(0).JsonValue.Value;
      end;
    finally
       json.Free;
    end;
 
      Result:=HTMLDecode(Result);
  end;
end;

Open in new window

0
mikelittlewoodAuthor Commented:
Just thought I'd let people know that to fix it I altered these lines

  if Source=Autodetect then
    EncodedRequest:=Format(GoogleTranslateUrlAuto,[GoogleLanguageApiKey,GoogleLanguagesArr[Dest],HTTPEncode(UTF8Encode(Text))])
  else
    EncodedRequest:=Format(GoogleTranslateUrl,[GoogleLanguageApiKey,HTTPEncode(UTF8Encode(Text)),GoogleLanguagesArr[Source],GoogleLanguagesArr[Dest]]);

Open in new window

0
mikelittlewoodAuthor Commented:
Solved myself but this pointed me in the right direction
0
ArgentiCommented:
Glad to here that!  :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.