A
A
Alexander Nikolaevich2016-11-17 18:53:27
Delphi
Alexander Nikolaevich, 2016-11-17 18:53:27

Why is the translated word displayed as an encoding?

I wrote a program that translates words from Russian into English using Yandex translator.
But if I divide the answer into lines after the colon, then the translated word is displayed as an encoding,
for example, hello becomes %D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82.
How to fix it ?
I write in Delphi 2010.
If you translate from English into Russian, then it translates normally.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, IdBaseComponent,
  IdCookieManager, StdCtrls, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack,
  IdSSL, IdSSLOpenSSL;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Memo1: TMemo;
    Button1: TButton;
    IdCookieManager1: TIdCookieManager;
    IdHTTP1: TIdHTTP;
    IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
    Memo2: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
 Http : TidHttp;
 CM : TidCookieManager;
 post,stok: TStringList;
 str,str2:string;
 StrPage, UserID, UserName : String;
 i ,p: integer;
begin

 Http := TIdHTTP.Create(Self);
 post := TStringList.Create;
  stok := TStringList.Create;

 Http.Request.Host:='translate.yandex.ru';
 Http.Request.UserAgent:='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36';
 Http.Request.Accept:='text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
 Http.Request.AcceptLanguage:='ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4';
 Http.Request.AcceptCharSet:='utf-8,windows-1251;q=0.7,*;q=0.7';
 Http.Request.Referer:='https://www.yandex.ru/';

post.Add('key=');
post.Add('text='+Edit1.text);
post.Add('lang=ru-en');
Memo1.Lines.Text := IdHTTP1.Post('https://translate.yandex.net/api/v1.5/tr.json/translate ?', post);
   str:=IdHTTP1.Post('https://translate.yandex.net/api/v1.5/tr.json/translate ?', post);

   p:=1;

  while (length(str)+1 > p)  do begin

      if (str[p]= ':') or (p=length(str))  then
      begin
      stok.Add(str2);
      str2:='';
       end
      else        begin
      str2:=str2+str[p];
         end;

       p:=p+1;

  end;

    memo2.Lines.Text:=stok.Text;

end;
end.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2016-11-17
@Sumor

Actually a normal answer: English letters in utf8 are just English letters, and Russian letters in utf8 are encoded in hexadecimal characters, in this case they were returned to you with the prefix%:
D09F - П
D180 - р
D0B8 - and
D0B2 - in
D0B5 - e
D182 - t

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question