procedure TForm1.btnAskTranslateClick(Sender: TObject);
var
URL: String;
Index: integer;
strResponse: TJSONStringType;
jdResponse, jdTranslation, jdTranslationArray: TJSONData;
jaTranslation, jaTranslationArray: TJSONArray;
sTextFrom: String;
sTextTo: String;
begin
sTextFrom := '';
sTextFrom := StringGrid1.Cells[_OrigText, StringGrid1.Row];
Application.ProcessMessages;
try
if Length(sTextFrom) = 0 then
begin
Memo1.Append('Need something to translate');
exit;
end;
if (cboxArrayTo.ItemIndex <= 0) and (cboxArrayFrom.ItemIndex <= 0) then
begin
Memo1.Append('Cannot have language detection on both sides');
exit;
end;
URL:='https://translate.googleapis.com/translate_a/single?client=gtx'
+'&q='+HTTPEncode(sTextFrom)
+'&sl='+cArrayShortLanguages[cboxArrayFrom.ItemIndex]
+'&tl='+cArrayShortLanguages[cboxArrayTo.ItemIndex]
+'&dt=t'
+'&ie=UTF-8&oe=UTF-8'
;
//Memo1.Append(URL);
strResponse:= CallGoogleTranslate(URL);
try
jdResponse:= GetJSON(strResponse);
//memArraysTo.Append(jdResponse.FormatJSON); exit;
jdTranslation:= jdResponse.FindPath('[0]');
if (jdTranslation <> nil) and (jdTranslation.JSONType = jtArray) then
begin
jaTranslation:= TJSONArray(jdTranslation);
for index:= 0 to Pred(jaTranslation.Count) do
begin
jdTranslationArray:= jaTranslation[Index];
if (jdTranslationArray <> nil) and (jdTranslationArray.JSONType = jtArray) then
begin
jaTranslationArray:= TJSONArray(jdTranslationArray);
StringGrid1.BeginUpdate;
StringGrid1.Cells[_TRANSLATION, StringGrid1.Row] := Trim(jaTranslationArray[0].AsString);
sTextTo := StringGrid1.Cells[_TRANSLATION, StringGrid1.Row];
StringGrid1ValidateEntry(Sender, _TRANSLATION, StringGrid1.Row, '', sTextTo);
StringGrid1.EndUpdate;
end;
end;
end;
finally
jdResponse.Free;
end;
finally
Application.ProcessMessages;
end;
end;