Difference between revisions of "Using Google Translate"
From Lazarus wiki
Jump to navigationJump to searchm (Fixed syntax highlighting) |
(Updated information on Google Translate params and better examples using unit fpjson) |
||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
+ | {{Using_Google_Translate}} | ||
+ | |||
+ | __TOC__ | ||
+ | |||
==Overview== | ==Overview== | ||
− | Google Translate | + | There are at least two way to access Google Translate: |
+ | * A free but metered way. | ||
+ | * Using the OAuth2 authenticated API. | ||
− | + | This is a usage example of the former. | |
− | + | For information on the latter please consult: [https://developers.google.com/translate/v2/getting_started Google Translate Getting Started] | |
− | + | ==Info on the params== | |
+ | According to an answer on Stack Overflow ([https://stackoverflow.com/a/29537590/8167 What is the meaning of Google Translate query params?]) here's a list of the params to the HTTP GET call: | ||
+ | * sl - source language code (auto for auto detection) | ||
+ | * tl - translation language | ||
+ | * q - source text / word | ||
+ | * ie - input encoding (a guess) | ||
+ | * oe - output encoding (a guess) | ||
+ | * dt - may be included more than once and specifies what to return in the reply | ||
+ | * dj - JSON response with names instead of only arrays (dj=1) | ||
+ | |||
+ | Here are some value for dt: | ||
+ | * t - translation of source text | ||
+ | * at - alternate translations | ||
+ | * rm - transcription / transliteration of source and translated texts | ||
+ | * bd - dictionary, in case source text is one word (you get translations with articles, reverse translations, etc.) | ||
+ | * md - definitions of source text, if it's one word | ||
+ | * ss - synonyms of source text, if it's one word | ||
+ | * ex - examples | ||
+ | * rw - See also list | ||
+ | |||
+ | |||
+ | ==Contacting Google== | ||
+ | This function will return the JSON response. | ||
<syntaxhighlight lang="pascal"> | <syntaxhighlight lang="pascal"> | ||
uses | uses | ||
− | + | {...}, fpjson, fphttpclient, opensslsockets, {...} | |
− | function | + | function CallGoogleTranslate(AURL: String): TJSONStringType; |
var | var | ||
client: TFPHTTPClient; | client: TFPHTTPClient; | ||
doc: TStringList; | doc: TStringList; | ||
begin | begin | ||
− | Result:= | + | Result:= EmptyStr; |
doc:=TStringList.Create; | doc:=TStringList.Create; | ||
client:=TFPHTTPClient.Create(nil); | client:=TFPHTTPClient.Create(nil); | ||
try | try | ||
− | client.Get( | + | client.Get(AURL,doc); |
− | Result:=doc | + | Result:=doc.Text; |
− | |||
finally | finally | ||
doc.Free; | doc.Free; | ||
Line 29: | Line 56: | ||
end; | end; | ||
end; | end; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==Parsing the JSON Array based response== | ||
+ | <syntaxhighlight lang="pascal"> | ||
+ | uses | ||
+ | {...}, fpjson, jsonparser, HTTPDefs, {...} | ||
+ | |||
+ | const | ||
+ | cArrayShortLanguages: Array [0..7] of String = ( | ||
+ | 'auto', | ||
+ | 'en', | ||
+ | 'pt', | ||
+ | 'pl', | ||
+ | 'fr', | ||
+ | 'es', | ||
+ | 'it', | ||
+ | 'ru' | ||
+ | ); | ||
− | + | procedure ParseArraysTranslate; | |
− | |||
var | var | ||
− | |||
URL: String; | URL: String; | ||
− | + | Index: integer; | |
− | + | strResponse: TJSONStringType; | |
− | + | jdResponse, jdTranslation, jdTranslationArray: TJSONData; | |
− | + | jaTranslation, jaTranslationArray: TJSONArray; | |
− | |||
− | |||
begin | begin | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | URL:='https://translate.googleapis.com/translate_a/single?client=gtx' | |
− | URL:=' | + | +'&q='+HTTPEncode({** PUT TEXT TO TRANSLATE HERE **}) |
− | +'& | + | +'&sl='+cArrayShortLanguages[0] // Auto Detect |
− | +'&sl='+ | + | +'&tl='+cArrayShortLanguages[1] // English |
− | +'&tl='+ | + | +'&dt=t' |
− | |||
− | +'& | ||
+'&ie=UTF-8&oe=UTF-8' | +'&ie=UTF-8&oe=UTF-8' | ||
; | ; | ||
+ | |||
+ | strResponse:= CallGoogleTranslate(URL); | ||
try | try | ||
− | + | jdResponse:= GetJSON(strResponse); | |
− | + | ||
− | + | 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 | begin | ||
− | + | jaTranslationArray:= TJSONArray(jdTranslationArray); | |
− | + | WriteLN(Trim(jaTranslationArray[0].AsString)); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end; | end; | ||
end; | end; | ||
− | + | end; | |
− | |||
finally | finally | ||
− | + | jdResponse.Free; | |
end; | end; | ||
− | |||
end; | end; | ||
+ | </syntaxhighlight> | ||
− | + | ==Parsing the JSON Object based response== | |
− | procedure | + | <syntaxhighlight lang="pascal"> |
+ | uses | ||
+ | {...}, fpjson, jsonparser, HTTPDefs, {...} | ||
+ | |||
+ | const | ||
+ | cArrayShortLanguages: Array [0..7] of String = ( | ||
+ | 'auto', | ||
+ | 'en', | ||
+ | 'pt', | ||
+ | 'pl', | ||
+ | 'fr', | ||
+ | 'es', | ||
+ | 'it', | ||
+ | 'ru' | ||
+ | ); | ||
+ | cJSONSentences = 'sentences'; | ||
+ | cJSONTranslation = 'trans'; | ||
+ | cJSONSource = 'src'; | ||
+ | |||
+ | procedure ParseObjectTranslate; | ||
var | var | ||
− | + | URL: String; | |
− | + | Index: integer; | |
+ | strResponse: TJSONStringType; | ||
+ | jdResponse: TJSONData; | ||
+ | joTranslation, joSentence: TJSONObject; | ||
+ | jaSentencesArray: TJSONArray; | ||
begin | begin | ||
− | + | Application.ProcessMessages; | |
− | + | URL:='https://translate.googleapis.com/translate_a/single?client=gtx' | |
− | + | +'&q='+HTTPEncode({** PUT TEXT TO TRANSLATE HERE **}) | |
− | + | +'&sl='+cArrayShortLanguages[0] // Auto Detect | |
− | + | +'&tl='+cArrayShortLanguages[1] // English | |
− | + | +'&dt=t&dj=1' // dj=1 makes the response be a JSON Object | |
+ | +'&ie=UTF-8&oe=UTF-8' | ||
+ | ; | ||
+ | |||
+ | strResponse:= CallGoogleTranslate(URL); | ||
+ | try | ||
+ | jdResponse:= GetJSON(strResponse); | ||
+ | |||
+ | if (jdResponse <> nil) and (jdResponse.JSONType = jtObject) then | ||
+ | begin | ||
+ | joTranslation:= TJSONObject(jdResponse); | ||
+ | jaSentencesArray:= TJSONArray(joTranslation.FindPath(cJSONSentences)); | ||
+ | for Index:=0 to Pred(jaSentencesArray.Count) do | ||
+ | begin | ||
+ | joSentence:= TJSONObject(jaSentencesArray[Index]); | ||
+ | WriteLN(Trim(joSentence.Get(cJSONTranslation,''))); | ||
+ | end; | ||
+ | end; | ||
+ | finally | ||
+ | jdResponse.Free; | ||
+ | end; | ||
end; | end; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ==External Links== | |
− | [ | + | [https://github.com/gcarreno/TestGoogleTranslate Test Google Translate] - A [https://github.com GitHub] repository which demonstrates this example. |
− | [ |
Latest revision as of 07:13, 16 December 2021
│
English (en) │
français (fr) │
português (pt) │
Overview
There are at least two way to access Google Translate:
- A free but metered way.
- Using the OAuth2 authenticated API.
This is a usage example of the former.
For information on the latter please consult: Google Translate Getting Started
Info on the params
According to an answer on Stack Overflow (What is the meaning of Google Translate query params?) here's a list of the params to the HTTP GET call:
- sl - source language code (auto for auto detection)
- tl - translation language
- q - source text / word
- ie - input encoding (a guess)
- oe - output encoding (a guess)
- dt - may be included more than once and specifies what to return in the reply
- dj - JSON response with names instead of only arrays (dj=1)
Here are some value for dt:
- t - translation of source text
- at - alternate translations
- rm - transcription / transliteration of source and translated texts
- bd - dictionary, in case source text is one word (you get translations with articles, reverse translations, etc.)
- md - definitions of source text, if it's one word
- ss - synonyms of source text, if it's one word
- ex - examples
- rw - See also list
Contacting Google
This function will return the JSON response.
uses
{...}, fpjson, fphttpclient, opensslsockets, {...}
function CallGoogleTranslate(AURL: String): TJSONStringType;
var
client: TFPHTTPClient;
doc: TStringList;
begin
Result:= EmptyStr;
doc:=TStringList.Create;
client:=TFPHTTPClient.Create(nil);
try
client.Get(AURL,doc);
Result:=doc.Text;
finally
doc.Free;
client.Free;
end;
end;
Parsing the JSON Array based response
uses
{...}, fpjson, jsonparser, HTTPDefs, {...}
const
cArrayShortLanguages: Array [0..7] of String = (
'auto',
'en',
'pt',
'pl',
'fr',
'es',
'it',
'ru'
);
procedure ParseArraysTranslate;
var
URL: String;
Index: integer;
strResponse: TJSONStringType;
jdResponse, jdTranslation, jdTranslationArray: TJSONData;
jaTranslation, jaTranslationArray: TJSONArray;
begin
URL:='https://translate.googleapis.com/translate_a/single?client=gtx'
+'&q='+HTTPEncode({** PUT TEXT TO TRANSLATE HERE **})
+'&sl='+cArrayShortLanguages[0] // Auto Detect
+'&tl='+cArrayShortLanguages[1] // English
+'&dt=t'
+'&ie=UTF-8&oe=UTF-8'
;
strResponse:= CallGoogleTranslate(URL);
try
jdResponse:= GetJSON(strResponse);
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);
WriteLN(Trim(jaTranslationArray[0].AsString));
end;
end;
end;
finally
jdResponse.Free;
end;
end;
Parsing the JSON Object based response
uses
{...}, fpjson, jsonparser, HTTPDefs, {...}
const
cArrayShortLanguages: Array [0..7] of String = (
'auto',
'en',
'pt',
'pl',
'fr',
'es',
'it',
'ru'
);
cJSONSentences = 'sentences';
cJSONTranslation = 'trans';
cJSONSource = 'src';
procedure ParseObjectTranslate;
var
URL: String;
Index: integer;
strResponse: TJSONStringType;
jdResponse: TJSONData;
joTranslation, joSentence: TJSONObject;
jaSentencesArray: TJSONArray;
begin
Application.ProcessMessages;
URL:='https://translate.googleapis.com/translate_a/single?client=gtx'
+'&q='+HTTPEncode({** PUT TEXT TO TRANSLATE HERE **})
+'&sl='+cArrayShortLanguages[0] // Auto Detect
+'&tl='+cArrayShortLanguages[1] // English
+'&dt=t&dj=1' // dj=1 makes the response be a JSON Object
+'&ie=UTF-8&oe=UTF-8'
;
strResponse:= CallGoogleTranslate(URL);
try
jdResponse:= GetJSON(strResponse);
if (jdResponse <> nil) and (jdResponse.JSONType = jtObject) then
begin
joTranslation:= TJSONObject(jdResponse);
jaSentencesArray:= TJSONArray(joTranslation.FindPath(cJSONSentences));
for Index:=0 to Pred(jaSentencesArray.Count) do
begin
joSentence:= TJSONObject(jaSentencesArray[Index]);
WriteLN(Trim(joSentence.Get(cJSONTranslation,'')));
end;
end;
finally
jdResponse.Free;
end;
end;
External Links
Test Google Translate - A GitHub repository which demonstrates this example.