Forum > Translations
google translate
(1/1)
ravkepar:
I made an subtitle tool, but i want to add a translate module in the program to translate subtitles - that are inside a stringgrid - between several langages.
I think the easiest way is using google translate, but it's hard to know how to do this.
Has anybody got an idea how to do this?
paweld:
https://forum.lazarus.freepascal.org/index.php/topic,50508.msg368740.html#msg368740
ravkepar:
Tx. This should be standard in Lazarus. ;)
Thaddy:
The google api's ARE standard in Freepascal.
jcmontherock:
I am using this procedure, translating one StringGrid cell to another one. Language from and to are in a combobox: cbboxArrayFrom and cboxArrayTo:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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;
Navigation
[0] Message Index