From 598b6953278fb1d2346e17b14f084168e5e51512 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Tue, 10 May 2022 22:18:37 +0100 Subject: [PATCH] added translate --- README.MD | 2 ++ remmina.nim | 3 +- translate.nim | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 translate.nim diff --git a/README.MD b/README.MD index ea6e186..655310b 100644 --- a/README.MD +++ b/README.MD @@ -18,6 +18,8 @@ which are selectable options in dmenu. - `calculate` a calculator, utilising `qalculate` - inspired by [@fedops](https://codeberg.org/fedops/scripts) - `emoji` an emoji picker - `remmina` reads the files in your remmina config directory and allows you to connect to and edit them +- `translate` utilises libretranslate (you'll need and API key or your own instance) to translate test. Prefix the text with `en>de`, `de>en`, `en>fr`, etc. as you need. + - must be compiled with `-d:ssl` The next two do not work with `rofi` unless you have `alias dmenu=rofi` set, but they're pretty nice tools diff --git a/remmina.nim b/remmina.nim index 349ccca..a4f837a 100644 --- a/remmina.nim +++ b/remmina.nim @@ -1,6 +1,5 @@ import base -import lib/emojilist -import std/[re,os,osproc,tables,algorithm] +import std/[os,osproc,tables,algorithm] import configparser const REMMINA_DIR = getHomeDir() & ".local/share/remmina" diff --git a/translate.nim b/translate.nim new file mode 100644 index 0000000..a129878 --- /dev/null +++ b/translate.nim @@ -0,0 +1,78 @@ +import base +import std/[re,httpclient,json,strutils] + +const LIBRETRANSLATE_URL = "https://libretranslate.pussthecat.org/translate" +const PRIMARY = "en" +const SECONDARY = "de" +let LANG_RE = re"\w+>\w+" + +type + Request = object + source: string + target: string + q: string + format: string + +proc main(message: varargs[string] = @[]) + +proc newRequest(): Request = + return Request(source:SECONDARY, + target:PRIMARY, + q: "", + format: "text",) + +proc parseReq(req: string): Request = + var r = newRequest() + var langs = findAll(req,LANG_RE) + if len(langs) > 0: + let lang = langs[0] + langs = lang.split(re">") + let query = replace(req,LANG_RE,"") + if query != "": + r.source = langs[0].toLowerAscii() + r.target = langs[1].toLowerAscii() + r.q = query + else: + main() + else: + main("No language selections made. i.e. en>de, de>en, etc.") + return r + +proc answerTranslate(response: string, req: Request) = + let r = parseJson(response) + try: + let ans = r["translatedText"].getStr() + main(ans, req.source & " > " & req.target) + except: + main("Error : " & r["error"].getStr()) + +proc goTranslate(req: string) = + let r = parseReq(req) + if r.q != "": + var client = newHTTPClient() + client.headers = newHttpHeaders({"Content-Type":"application/json"}) + let body = %*r + let response = client.request(LIBRETRANSLATE_URL, httpMethod = HttpPost, body = $body) + if response.status != "": + answerTranslate(response.body, r) + + +proc main(message:varargs[string] = @[]) = + var info = newInfo("Translate") + info.selected_bg = green + var args: seq[string] = @[] + for msg in message: + if msg != "": + args.add(msg) + args.add("exit") + let output = outputData(info,args) + if output == "exit" or output == "": + return + elif output in message: + copyToClipboard(output) + else: + goTranslate(output) + return + +if isMainModule: + main()