Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Translate the specific controls in the dialog one by one
#10
Had some time to look at this again. I changed the translate subfunction to use c#(CsScript) in qm2 now for the translation. Much easier to handle the Json array returned using c#.
Function Trans2 
Trigger K     Help - how to add the trigger to the macro
Code:
Copy      Help
out
Acc ac.FromMouse 
str name = ac.Name
if(ac.Role =18)
,ret
if(empty(name))
,int h = child(mouse)
,if h=0
,,h = win(mouse)
,_s.getwintext(h)
,if _s.len=0
,,_s.getsel
,,if(_s.len =0)
,,,out "nothing selected"
,,,ret

,name=_s
out name
str d
sub.GetTranslation(name "auto" "zh_cn" d);;change values  for other Languages
out d
OnScreenDisplay d -1 0 20 "" 24 0xFF0000 4|32 "TosdHwnd"

#sub GetTranslation
function ~sourceText ~sourceLang ~targetLang ~&tl

CsScript x
x.SetOptions("references=System.Web.Extensions.dll;System.Runtime.Serialization;Microsoft.CSharp.dll;System.Core.dll")
x.AddCode("")
tl=x.Call("Example.TranslateText" sourceText sourceLang targetLang)

#ret
using System.Net.Http;
using System.Collections;
using System.Web.Script.Serialization;
using System;
using System.Collections.Generic;

public class Example
{
,public static string TranslateText(string input , string sLang ,string tLang)
,{
,,string url = String.Format("https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}", sLang, tLang, Uri.EscapeUriString(input));
,,HttpClient httpClient = new HttpClient();
,,string result = httpClient.GetStringAsync(url).Result;
,,var jsonData = new JavaScriptSerializer().Deserialize<List<dynamic>>(result);
,,var translationItems = jsonData[0];
,,string translation = "";
,,foreach (object item in translationItems)
,,{
,,,IEnumerable translationLineObject = item as IEnumerable;
,,,IEnumerator translationLineString = translationLineObject.GetEnumerator();
,,,translationLineString.MoveNext();
,,,translation += string.Format(" {0}", Convert.ToString(translationLineString.Current));
,,}
,,if (translation.Length > 1) { translation = translation.Substring(1); };
,,return translation;
,}
}


Messages In This Thread
RE: Translate the specific controls in the dialog one by one - by Kevin - 08-03-2022, 03:35 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)