sh Yandex.Translate shell脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh Yandex.Translate shell脚本相关的知识,希望对你有一定的参考价值。

#!/bin/bash

# Dependencies:
# ------------
# xclip — read/write to clipboard
# jq    — parse JSON

apiKey="" # Yandex API key, see here https://tech.yandex.ru/keys/get/?service=trnsl
firstLang=$1
lastLang=$2
text=$(xclip -o)

if [[ -z "$firstLang" ]]; then
    firstLang='en'
fi

if [[ -z "$firstLang" ]]; then
    lastLang='ru'
fi

if [[ -z "$text" ]]; then
    # If clipboard is empty
    notify-send Ooops... -i dialog-error "Nothing to translate."
else
    # Sending request and parse JSON string
    request=$(curl -sb -H "Accept: application/json" "https://translate.yandex.net/api/v1.5/tr.json/translate?key=$apiKey&text=$text&lang=$firstLang-$lastLang" | jq '.text[0]')
    # Remove double quotes from a string
    request=${request//\"}
    # Sending notifications
    notify-send Translate: -i edit-paste "$request"
    # Copy the result to clipboard
    echo "$request" | xclip -selection clipboard
fi

# How to use:
# ------------
# To use this script you need to assign it to run on hot keys. For example:
# ~/yandex-translate.sh en ru
# or:
# ~/yandex-translate.sh ru en
# Enjoy!

以上是关于sh Yandex.Translate shell脚本的主要内容,如果未能解决你的问题,请参考以下文章

VueJS 异步检测语言与 Yandex Translate API

终端依赖者福利:终端也能实现翻译功能了

执行shell脚本三种方法的区别:(sh、exec、source)

shell 脚本各种执行方式(source ./*.sh, . ./*.sh, ./*.sh)的区别

怎么用一个命令使多个shell脚本并行执行? 例如三个脚本 run1.sh,run2.sh

shell 一次按顺序执行多个脚本