diff options
Diffstat (limited to 'translate-widget')
-rw-r--r-- | translate-widget/README.MD | 20 | ||||
-rw-r--r-- | translate-widget/translate.lua | 17 |
2 files changed, 24 insertions, 13 deletions
diff --git a/translate-widget/README.MD b/translate-widget/README.MD index 2c783a0..a82fda7 100644 --- a/translate-widget/README.MD +++ b/translate-widget/README.MD @@ -1,6 +1,6 @@ # Translate Widget -This widget allows quickly translate words or phrases without opening a browser - just using Awesome. To provide direction of the translation add the 2 letters code of the source and target languages at the end of the phrase, for example _hello enfr_ will translate _hello_ from English to French. This widget is based on [Yandex.Translate API](https://tech.yandex.com/translate/). +This widget allows quickly translate words or phrases without opening a browser - just using Awesome. To provide direction of the translation add the 2 letters code of the source and target languages at the end of the phrase, for example _hello enfr_ will translate _hello_ from English to French. This widget is based on [Watson Language Translator](https://www.ibm.com/watson/services/language-translator/) from IBM. ![demo](./demo.gif) @@ -13,7 +13,8 @@ This widget allows quickly translate words or phrases without opening a browser ## Installation 1. Clone repo under **~/.config/awesome/** -1. Get an [API key](https://translate.yandex.com/developers/keys) +1. Create an IBM Cloud API key at [cloud.ibm.com/iam/apikeys](https://cloud.ibm.com/iam/apikeys) +1. Copy a service URL by going to [resource list](https://cloud.ibm.com/resources), then under "Services" select "Language Translator" option, and then copy URL from the "Credentials" section 1. Require widget in **rc.lua**: ```lua @@ -23,8 +24,15 @@ This widget allows quickly translate words or phrases without opening a browser 1. Add a shortcut to run translate prompt: ```lua - awful.key({ modkey }, "c", - function() translate.show_translate_prompt('<api-key>') end, - { description = "run translate prompt", group = "launcher" }), - ``` + awful.key({ modkey }, "c", function() + translate.launch{api_key = '<api-key>', url = 'url'} + end, { description = "run translate prompt", group = "launcher" }) + ``` + + + + + + + diff --git a/translate-widget/translate.lua b/translate-widget/translate.lua index 9352ac9..1499333 100644 --- a/translate-widget/translate.lua +++ b/translate-widget/translate.lua @@ -16,7 +16,8 @@ local wibox = require("wibox") local gears = require("gears") local gfs = require("gears.filesystem") -local TRANSLATE_CMD = [[bash -c 'curl -s -u "apikey:%s" -H "Content-Type: application/json" -d '\''{"text": ["%s"], "model_id":"%s"}'\'' "%s/v3/translate?version=2018-05-01"']] +local TRANSLATE_CMD = [[bash -c 'curl -s -u "apikey:%s" -H "Content-Type: application/json"]] + ..[[ -d '\''{"text": ["%s"], "model_id":"%s"}'\'' "%s/v3/translate?version=2018-05-01"']] local ICON = os.getenv("HOME") .. '/.config/awesome/awesome-wm-widgets/translate-widget/gnome-translate.svg' --- Returns two values - string to translate and direction: @@ -82,12 +83,14 @@ local function translate(to_translate, lang, api_key, url) { { id = 'src', - markup = '<b>' .. lang:sub(1,2) .. '</b>: <span color="#FFFFFF"> ' .. to_translate .. '</span>', + markup = '<b>' .. lang:sub(1,2) .. '</b>: <span color="#FFFFFF"> ' + .. to_translate .. '</span>', widget = wibox.widget.textbox }, { id = 'res', - markup = '<b>' .. lang:sub(4) .. '</b>: <span color="#FFFFFF"> ' .. resp.translations[1].translation .. '</span>', + markup = '<b>' .. lang:sub(4) .. '</b>: <span color="#FFFFFF"> ' + .. resp.translations[1].translation .. '</span>', widget = wibox.widget.textbox }, id = 'text', @@ -110,11 +113,11 @@ local function translate(to_translate, lang, api_key, url) w:buttons( awful.util.table.join( awful.button({}, 1, function() - awful.spawn.with_shell("echo '" .. resp.translations[1].translation .. "' | xclip -selection clipboard") + spawn.with_shell("echo '" .. resp.translations[1].translation .. "' | xclip -selection clipboard") w.visible = false end), awful.button({}, 3, function() - awful.spawn.with_shell("echo '" .. to_translate .."' | xclip -selection clipboard") + spawn.with_shell("echo '" .. to_translate .."' | xclip -selection clipboard") w.visible = false end) ) @@ -159,9 +162,9 @@ input_widget:setup{ widget = wibox.container.margin } -local function launch(args) +local function launch(user_args) - local args = args or {} + local args = user_args or {} local api_key = args.api_key local url = args.url |