tli 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4b9966de4cb9e2ccf16508faf6c4c3a97716de52
4
+ data.tar.gz: 4ed76add32b44156e3e8349d522821c727493528
5
+ SHA512:
6
+ metadata.gz: df5cd7d25888649b8b4ec74e91131cd5de463a4b6c4644a1ce90bacc24ca33ddc749d01434efee91319a1f0e024a738fb30dc375eab79bd2e9c35c2f5833ae48
7
+ data.tar.gz: b93823f30b565d0f452bf2ee910e7ac4118e673a1141cddd7403e869544aa7411905b4ed9a29cdcbd909138f0cb81bfb5775dc390c8a67d9d9259a8f80d5c243
data/bin/tli ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/tli'
3
+
4
+ OPTIONS = {
5
+ '--source' => 'option_value',
6
+ '-s' => 'option_value',
7
+ '--target' => 'option_value',
8
+ '-t' => 'option_value',
9
+ '--backend' => 'option_value',
10
+ '-b' => 'option_value',
11
+ '--play' => 'option',
12
+ '-p' => 'option'
13
+ }
14
+
15
+
16
+ length = ARGV.length
17
+ params = Hash.new('')
18
+ index = 0
19
+ while index < length
20
+ arg = ARGV[index]
21
+ if OPTIONS.include?(arg)
22
+ if OPTIONS[arg] == 'option_value'
23
+ raise "#{arg} requires a value." if index + 1 >= length
24
+ params[arg] = ARGV[index+1]
25
+ index += 1
26
+ else
27
+ params[arg] = true
28
+ end
29
+ else
30
+ params[:text] << ' ' + arg
31
+ end
32
+ index += 1
33
+ end
34
+
35
+ puts Tli.translate(params[:text], params['--source'], params['--target'])
@@ -0,0 +1,12 @@
1
+ class TextDecoder
2
+ def self.decode(text, lang_code)
3
+ result = ''
4
+ case lang_code
5
+ when 'en', 'es', 'fr'
6
+ result = text.force_encoding('ISO-8859-1').encode('UTF-8')
7
+ when 'ru'
8
+ result = text.force_encoding('Windows-1251').encode('UTF-8')
9
+ end
10
+ result
11
+ end
12
+ end
data/lib/tli.rb ADDED
@@ -0,0 +1,7 @@
1
+ require_relative 'translators/google_translator.rb'
2
+ class Tli
3
+ def Tli.translate(text, source, target)
4
+ translator = GoogleTranslator.new
5
+ return translator.translate(text, source, target)
6
+ end
7
+ end
@@ -0,0 +1,46 @@
1
+ require 'rest_client'
2
+ require_relative 'translator'
3
+ require_relative '../text_decoder'
4
+
5
+ class GoogleTranslator < Translator
6
+ API_URL = 'http://translate.google.com/translate_a/t'
7
+ def get_langs
8
+ { 'af' => 'Afrikaans', 'sq' => 'Albanian', 'ar' => 'Arabic',
9
+ 'hy' => 'Armenian', 'az' => 'Azerbaijani', 'eu' => 'Basque',
10
+ 'be' => 'Belarusian', 'bg' => 'Bulgarian', 'ca' => 'Catalan',
11
+ 'hr' => 'Croatian', 'cs' => 'Czech', 'et' => 'Estonian',
12
+ 'da' => 'Danish', 'nl' => 'Dutch', 'en' => 'English',
13
+ 'tl' => 'Filipino', 'fi' => 'Finnish', 'fr' => 'French',
14
+ 'gl' => 'Galician', 'ht' => 'Haitian, Creole', 'is' => 'Icelandic',
15
+ 'ka' => 'Georgian', 'de' => 'German', 'el' => 'Greek',
16
+ 'iw' => 'Hebrew', 'hi' => 'Hindi', 'hu' => 'Hungarian',
17
+ 'id' => 'Indonesian', 'ga' => 'Irish', 'it' => 'Italian',
18
+ 'ko' => 'Korean', 'la' => 'Latin', 'lv' => 'Latvian',
19
+ 'mk' => 'Macedonian', 'ms' => 'Malay', 'mt' => 'Maltese',
20
+ 'fa' => 'Persian', 'pl' => 'Polish', 'pt' => 'Portuguese',
21
+ 'ro' => 'Romanian', 'sl' => 'Slovenian', 'th' => 'Thai',
22
+ 'ja' => 'Japanese', 'lt' => 'Lithuanian', 'no' => 'Norwegian',
23
+ 'vi' => 'Vietnamese', 'cy' => 'Welsh', 'yi' => 'Yiddish',
24
+ 'ru' => 'Russian', 'sr' => 'Serbian', 'sk' => 'Slovak',
25
+ 'es' => 'Spanish', 'sw' => 'Swahili', 'sv' => 'Swedish',
26
+ 'tr' => 'Turkish', 'uk' => 'Ukrainian', 'ur' => 'Urdu',
27
+ 'zh-TW' => 'Chinese, (Traditional)','zh-CN'=> 'Chinese, (Simplified)' }
28
+ end
29
+
30
+ def translate(text, source, target)
31
+ raise "Unknown language code '#{source}'" if !get_langs.include?(source)
32
+ raise "Unknown language code '#{target}'" if !get_langs.include?(target)
33
+ response = RestClient.get(API_URL, params: {client: 'p', text: text, sl: source, tl: target})
34
+ json = JSON.parse(TextDecoder.decode(response.to_s, target))
35
+ translation = ''
36
+ json['sentences'].each do |entry|
37
+ if entry.include?('trans')
38
+ translation = entry['trans']
39
+ break
40
+ end
41
+ end
42
+ translation
43
+ end
44
+ end
45
+
46
+
@@ -0,0 +1,4 @@
1
+ class Translator
2
+ def translate(text, source, target); end
3
+ def get_langs; end
4
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Rafael Rendón Pablo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: With tli you can translate text and find definitions using services like
14
+ Google Translate, Yandex Translate, Oxford dictionaries, etc. All in one.
15
+ email: rafaelrendonpablo@gmail.com
16
+ executables:
17
+ - tli
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - bin/tli
22
+ - lib/text_decoder.rb
23
+ - lib/tli.rb
24
+ - lib/translators/google_translator.rb
25
+ - lib/translators/translator.rb
26
+ homepage: https://github.com/rendon/tli
27
+ licenses:
28
+ - GNU GPL v3.0
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.4.3
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: TransLate It! A command line tool to translate text from (and to) almost
50
+ any language.
51
+ test_files: []