Skip to content

Commit

Permalink
Moved from PHP to Ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
thang-nm committed Jul 29, 2022
1 parent 968956e commit 4bf58a2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Google-Translate.popclipext/Config.plist
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<key>Image File</key>
<string>google-translate.png</string>
<key>Script Interpreter</key>
<string>/usr/bin/php</string>
<string>/usr/bin/ruby</string>
<key>Shell Script File</key>
<string>google-translate.php</string>
<string>google-translate.rb</string>
<key>Title</key>
<dict>
<key>en</key>
Expand Down
21 changes: 21 additions & 0 deletions Google-Translate.popclipext/google-translate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'net/http'
require 'json'

text = ENV['POPCLIP_TEXT']
modifiers = ENV['POPCLIP_MODIFIER_FLAGS']
tl = ENV[modifiers == 1048576 ? 'POPCLIP_OPTION_TLC' : 'POPCLIP_OPTION_TL']
match = tl.match(/\((.*)\)/)
tl = match[1]

uri = URI('https://translate.googleapis.com/translate_a/single')
params = {
:client => 'gtx',
:sl => 'auto',
:tl => tl,
:dt => 't',
:q => text.unicode_normalize
}
uri.query = URI.encode_www_form(params)
response = Net::HTTP.get(uri)
json = JSON.parse(response)
puts json[0].reduce('') { |str, line| str + line[0] }

0 comments on commit 4bf58a2

Please sign in to comment.