Sign in Join
Wiki copytemporary

Using Google Translate/fr: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Using Google Translate/fr

OldNewLine
1+## Vue d'ensemble
2+
3+Il existe au moins deux manières d'accéder à Google Translate:
4+
5+- Une API gratuite mais limitée (https://translate.googleapis.com/translate_a/). Limitation : 100 requêtes/heure (à confirmer).
6+- Une API authentifiée avec OAuth2 (https://translation.googleapis.com/language/translate/v2).
7+
8+Pour plus d'informations sur l'API authentifiée avec OAuth2, veuillez consulter: [Google Translate Getting Started](https://developers.google.com/translate/v2/getting_started)
9+
10+Voici un exemple d'utilisation de l'API gratuite :
11+
12+## Informations sur les paramètres de l' API
13+
14+D'après une réponse sur Stack Overflow ([What is the meaning of Google Translate query params?](https://stackoverflow.com/a/29537590/8167)) voici une liste des paramètres de l'appel HTTP GET:
15+
16+- sl - code de la langue source (auto pour la détection automatique)
17+- tl - langue de traduction
18+- q - texte source / mot
19+- ie - encodage d'entrée (une supposition)
20+- oe - encodage de sortie (une supposition)
21+- dt - peut être inclus plusieurs fois et spécifie ce qu'il faut retourner dans la réponse
22+- dj - réponse JSON avec des noms au lieu de tableaux (dj=1)
23+
24+Voici quelques valeurs pour dt:
25+
26+- t - traduction du texte source
27+- at - traductions alternatives
28+- rm - transcription / translittération des textes sources et traduits
29+- bd - dictionnaire, au cas où le texte source est un mot (vous obtenez des traductions avec des articles, des traductions inversées, etc.)
30+- md - définitions du texte source, si c'est un mot
31+- ss - synonymes du texte source, s'il s'agit d'un mot
32+- ex - exemples
33+- rw - Voir aussi la liste
34+
35+## Se connecter à Google Translate
36+
37+Cette fonction renverra la réponse JSON.
38+
39+``` pascal
40+uses
41+ {...}, fpjson, fphttpclient, opensslsockets, {...}
42+
43+function CallGoogleTranslate(AURL: String): TJSONStringType;
44+var
45+ client: TFPHTTPClient;
46+ doc: TStringList;
47+begin
48+ Result:= EmptyStr;
49+ doc:=TStringList.Create;
50+ client:=TFPHTTPClient.Create(nil);
51+ try
52+ client.Get(AURL,doc);
53+ Result:=doc.Text;
54+ finally
55+ doc.Free;
56+ client.Free;
57+ end;
58+end;
59+```
60+
61+## Analyser la réponse basée sur le tableau JSON
62+
63+``` pascal
64+uses
65+ {...}, fpjson, jsonparser, HTTPDefs, {...}
66+
67+const
68+ cArrayShortLanguages: Array [0..7] of String = (
69+ 'auto',
70+ 'en',
71+ 'pt',
72+ 'pl',
73+ 'fr',
74+ 'es',
75+ 'it',
76+ 'ru'
77+ );
78+
79+procedure ParseArraysTranslate;
80+var
81+ URL: String;
82+ Index: integer;
83+ strResponse: TJSONStringType;
84+ jdResponse, jdTranslation, jdTranslationArray: TJSONData;
85+ jaTranslation, jaTranslationArray: TJSONArray;
86+begin
87+
88+ URL:='https://translate.googleapis.com/translate_a/single?client=gtx'
89+ +'&q='+HTTPEncode({** METTRE LE TEXTE À TRADUIRE ICI **})
90+ +'&sl='+cArrayShortLanguages[0] // Détection automatique
91+ +'&tl='+cArrayShortLanguages[1] // Anglais
92+ +'&dt=t'
93+ +'&ie=UTF-8&oe=UTF-8'
94+ ;
95+
96+ strResponse:= CallGoogleTranslate(URL);
97+ try
98+ jdResponse:= GetJSON(strResponse);
99+
100+ jdTranslation:= jdResponse.FindPath('[0]');
101+ if (jdTranslation <> nil) and (jdTranslation.JSONType = jtArray) then
102+ begin
103+ jaTranslation:= TJSONArray(jdTranslation);
104+ for index:= 0 to Pred(jaTranslation.Count) do
105+ begin
106+ jdTranslationArray:= jaTranslation[Index];
107+ if (jdTranslationArray <> nil) and (jdTranslationArray.JSONType = jtArray) then
108+ begin
109+ jaTranslationArray:= TJSONArray(jdTranslationArray);
110+ WriteLN(Trim(jaTranslationArray[0].AsString));
111+ end;
112+ end;
113+ end;
114+ finally
115+ jdResponse.Free;
116+ end;
117+end;
118+```
119+
120+## Analyser la réponse basée sur l'objet JSON
121+
122+``` pascal
123+uses
124+ {...}, fpjson, jsonparser, HTTPDefs, {...}
125+
126+const
127+ cArrayShortLanguages: Array [0..7] of String = (
128+ 'auto',
129+ 'en',
130+ 'pt',
131+ 'pl',
132+ 'fr',
133+ 'es',
134+ 'it',
135+ 'ru'
136+ );
137+ cJSONSentences = 'sentences';
138+ cJSONTranslation = 'trans';
139+ cJSONSource = 'src';
140+
141+procedure ParseObjectTranslate;
142+var
143+ URL: String;
144+ Index: integer;
145+ strResponse: TJSONStringType;
146+ jdResponse: TJSONData;
147+ joTranslation, joSentence: TJSONObject;
148+ jaSentencesArray: TJSONArray;
149+begin
150+ Application.ProcessMessages;
151+ URL:='https://translate.googleapis.com/translate_a/single?client=gtx'
152+ +'&q='+HTTPEncode({** METTRE LE TEXTE À TRADUIRE ICI **})
153+ +'&sl='+cArrayShortLanguages[0] // Détection automatique
154+ +'&tl='+cArrayShortLanguages[1] // Anglais
155+ +'&dt=t&dj=1' // dj=1 rend la réponse un objet JSON
156+ +'&ie=UTF-8&oe=UTF-8'
157+ ;
158+
159+ strResponse:= CallGoogleTranslate(URL);
160+ try
161+ jdResponse:= GetJSON(strResponse);
162+
163+ if (jdResponse <> nil) and (jdResponse.JSONType = jtObject) then
164+ begin
165+ joTranslation:= TJSONObject(jdResponse);
166+ jaSentencesArray:= TJSONArray(joTranslation.FindPath(cJSONSentences));
167+ for Index:=0 to Pred(jaSentencesArray.Count) do
168+ begin
169+ joSentence:= TJSONObject(jaSentencesArray[Index]);
170+ WriteLN(Trim(joSentence.Get(cJSONTranslation,'')));
171+ end;
172+ end;
173+ finally
174+ jdResponse.Free;
175+ end;
176+end;
177+```
178+
179+## Liens externes
180+
181+[Test Google Translate](https://github.com/gcarreno/TestGoogleTranslate) - Un référentiel [GitHub](https://github.com) qui illustre cet exemple.
182+
183+---
184+*From the Free Pascal wiki, page "Using Google Translate/fr", as it stood on 2021-12-17 after a change by Jurassic pork, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history