Skip to content

Instantly share code, notes, and snippets.

@journey-ad
Last active April 11, 2018 08:41
Show Gist options
  • Save journey-ad/9aa27287c339a561a3566f71c006017a to your computer and use it in GitHub Desktop.
Save journey-ad/9aa27287c339a561a3566f71c006017a to your computer and use it in GitHub Desktop.
fish shell 的自定义函数,用于翻译文本
function t
set pid "debc40e19e8f3675ee1f93b480ec3104"
set key "51b3cc5bb7d97d0c02e8bbd8fbbd84cd"
set text $argv[1]
set lang "auto"
echo -n $text | grep -Pq "[\p{Han}]"
if test $status -eq 0
echo -n $text | grep -Pq "[\p{Hiragana}\p{Katakana}]"
if test $status -eq 0
set transto zh-CHS
else
set transto en
end
else
set transto zh-CHS
end
for i in (echo $argv | sed 's|--*|\\'\n'|g' | sed 's/[ \t]*$//g' | grep -v '^$')
echo $i | read -l option value
switch $option
case l lang
set lang $value
case o transto
set transto $value
end
end
set text (echo $text | tr -d '\n')
if [ $text ]
set salt (date +%s)
set str1 "$pid""$text""$salt""$key"
set sign (echo -n $str1 | md5sum | cut -d ' ' -f1)
export GREP_COLOR="1;34"
printf (curl -s "http://fanyi.sogou.com/reventondc/api/sogouTranslate" -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json" -d "q=$text&from=$lang&to=$transto&pid=$pid&salt=$salt&sign=$sign&charset=utf8") | grep --color=auto -oP '(?<="translation":")[^"]*'
export GREP_COLOR=""
else
echo "t text -l language(auto zh-CHS zh-CHT en ja ko pt de es ru fr ...) -o transto"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment