Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Translate the specific controls in the dialog one by one
#12
Quote:
Code:
Copy      Help
// script "Translate UI element text.cs" /// Gets UI element text and translates with Google Cloud Translation API. Displays the result as a tooltip.
/// Edit the langTo string. Optionally edit langFrom.
/// By default gets text of UI element from mouse (uses elm functions). If script command line contains "copy", gets the selected text (uses the clipboard).
/// Hotkey trigger example: hk["F4"] = o => script.run(@"Translate UI element text.cs");
/// Hotkey trigger example for translating the selected text: hk["Ctrl+F4"] = o => script.run(@"Translate UI element text.cs", "copy");
/// Bad: Uses a Google translation service URL that is undocumented and may stop working in the future. Good: don't need authentication, API key.
/// If the used URL will stop working or does not work well, try the official API instead. Also there are libraries.
/// Not tested: max allowed text length. It is passed in URL. The script does not limit the text. Not tested: try POST request instead of GET.

/*/ ifRunning restart; /*/

string langFrom = "en", langTo = "es"; //edit these strings
bool debug = !true; //if true, prints the source and translated texts and errors

if (debug) print.clear();
try {
    string text;
    if (args.Contains("copy")) clipboard.tryCopy(out text); else text = _GetMouseElementText();
    if (text == null) return;
    if (debug) print.it(text);
    var s = _Translate(text, langFrom, langTo); if (s == null) return;
    if (debug) print.it($"<><c green>{s}<>");
    osdText.showText(s, xy: PopupXY.Mouse);
}
catch (Exception e1) { if (debug) print.it(e1); }

string _GetMouseElementText() {
    var e = elm.fromMouse();
    
    var name = e.Name;
    if (name.NE() || !name.RxIsMatch(@"(?i)[a-z]")) name = null;
    
    if (name != null && name.Contains('_') && e.WndContainer.ClassNameIs("HwndWrapper[*")) name = name.Replace("_", ""); //remove WPF accelerator prefix char
    
    var help = e.Help; //tooltip
    if (help.NE() || help == name || !help.RxIsMatch(@"(?i)[a-z]")) help = null;
    
    var val = e.Value; //textbox text
    if (val.NE() || val == name || val == help || !val.RxIsMatch(@"(?i)[a-z]")) val = null;
    
    if (name == null && help == null && val == null) return null;
    if ((help ?? val) == null) return name;
    var sb = new StringBuilder(name);
    if (help != null) sb.Append(sb.Length > 0 ? "\n" : null).Append("Help: ").Append(help);
    if (val != null) sb.Append(sb.Length > 0 ? "\n" : null).Append("Text: ").Append(val);
    return sb.ToString();
}

string _Translate(string sourceText, string sourceLang, string targetLang) {
    var url = internet.urlAppend("https://translate.googleapis.com/translate_a/single", "client=gtx", "sl=" + sourceLang, "tl=" + targetLang, "dt=t", "q=" + sourceText);
    var r = internet.http.Get(url);
    if (!r.IsSuccessStatusCode) {
        if (debug) print.it($"translate.googleapis.com error: {(int)r.StatusCode} {r.ReasonPhrase}");
        return null;
    }
    
    var sb = new StringBuilder();
    foreach (var v in r.Json()[0].AsArray()) sb.Append((string)v[0]);
    return sb.ToString();
}

How to improve the above code
Use a hotkey to implement the above two functions
e.g:
window: Limited to QM3 only
Hotkey: Press the ctrl key twice in a row
then: If there is selected text under the mouse pointer, the selected text is translated, otherwise the text of the control is translated


Messages In This Thread
RE: Translate the specific controls in the dialog one by one - by Davider - 08-14-2022, 10:19 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)