i18n_generators 1.0.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ == 1.1.0
2
+
3
+ * enhancements:
4
+ * New translation engine (see: http://googlecode.blogspot.com/2011/05/spring-cleaning-for-some-of-our-apis.html) (thanks to agilekawabata-san!)
5
+
1
6
  == 1.0.3
2
7
 
3
8
  * bugfixes:
@@ -21,4 +21,6 @@ Gem::Specification.new do |s|
21
21
 
22
22
  s.extra_rdoc_files = ['README.rdoc']
23
23
  s.licenses = ['MIT']
24
+
25
+ s.add_runtime_dependency 'mechanize'
24
26
  end
@@ -1,27 +1,59 @@
1
1
  require 'open-uri'
2
- require 'json' if RUBY_VERSION >= '1.9'
3
2
 
4
3
  module I27r
4
+ class TranslationError < StandardError; end
5
+
6
+ module GoogleTranslate
7
+ def _translate(word, lang)
8
+ w = CGI.escape ActiveSupport::Inflector.humanize(word)
9
+ json = OpenURI.open_uri("http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=#{w}&langpair=en%7C#{lang}").read
10
+ result = if RUBY_VERSION >= '1.9'
11
+ require 'json'
12
+ ::JSON.parse json
13
+ else
14
+ ActiveSupport::JSON.decode(json)
15
+ end
16
+ if result['responseStatus'] == 200
17
+ result['responseData']['translatedText']
18
+ else
19
+ raise TranslationError.new result.inspect
20
+ end
21
+ end
22
+ end
23
+
24
+ module BabelFish
25
+ def _translate(word, lang)
26
+ require 'mechanize'
27
+ w = CGI.escape ActiveSupport::Inflector.humanize(word)
28
+
29
+ agent = Mechanize.new
30
+ url = "http://babelfish.yahoo.com/translate_txt?lp=en_#{lang}&trtext=#{w}"
31
+ page = agent.get(url)
32
+ page.search('#result div').text
33
+ end
34
+ end
35
+
5
36
  class Translator
37
+ include BabelFish
38
+
6
39
  def initialize(lang)
7
40
  @lang, @cache = lang, {}
8
41
  end
9
42
 
10
43
  def translate(word)
11
44
  return @cache[word] if @cache[word]
12
- begin
13
- w = CGI.escape ActiveSupport::Inflector.humanize(word)
14
- json = OpenURI.open_uri("http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=#{w}&langpair=en%7C#{@lang}").read
15
- result = if RUBY_VERSION >= '1.9'
16
- ::JSON.parse json
17
- else
18
- ActiveSupport::JSON.decode(json)
19
- end
20
- result['responseStatus'] == 200 ? (@cache[word] = result['responseData']['translatedText']) : word
21
- rescue => e
22
- puts %Q[failed to translate "#{word}" into "#{@lang}" language.]
45
+
46
+ translated = _translate word, @lang
47
+ if translated.blank? || (translated == word)
23
48
  word
49
+ else
50
+ @cache[word] = translated
51
+ translated
24
52
  end
53
+ rescue => e
54
+ Rails.logger.debug e
55
+ puts %Q[failed to translate "#{word}" into "#{@lang}" language.]
56
+ word
25
57
  end
26
58
  end
27
59
  end
@@ -1,3 +1,3 @@
1
1
  module I18nGenerators
2
- VERSION = '1.0.3'
2
+ VERSION = '1.1.0'
3
3
  end
metadata CHANGED
@@ -1,33 +1,36 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: i18n_generators
3
- version: !ruby/object:Gem::Version
4
- hash: 17
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 0
9
- - 3
10
- version: 1.0.3
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Akira Matsuda
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-06-24 00:00:00 Z
19
- dependencies: []
20
-
21
- description: A Rails generator plugin & gem that generates Rails I18n locale files for almost every known locale.
22
- email:
12
+ date: 2011-11-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mechanize
16
+ requirement: &70329611412400 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70329611412400
25
+ description: A Rails generator plugin & gem that generates Rails I18n locale files
26
+ for almost every known locale.
27
+ email:
23
28
  - ronnie@dio.jp
24
29
  executables: []
25
-
26
30
  extensions: []
27
-
28
- extra_rdoc_files:
31
+ extra_rdoc_files:
29
32
  - README.rdoc
30
- files:
33
+ files:
31
34
  - .gitignore
32
35
  - Gemfile
33
36
  - History.txt
@@ -82,39 +85,31 @@ files:
82
85
  - spec/translator_spec.rb
83
86
  - spec/yaml_spec.rb
84
87
  homepage: https://github.com/amatsuda/i18n_generators
85
- licenses:
88
+ licenses:
86
89
  - MIT
87
90
  post_install_message:
88
91
  rdoc_options: []
89
-
90
- require_paths:
92
+ require_paths:
91
93
  - lib
92
- required_ruby_version: !ruby/object:Gem::Requirement
94
+ required_ruby_version: !ruby/object:Gem::Requirement
93
95
  none: false
94
- requirements:
95
- - - ">="
96
- - !ruby/object:Gem::Version
97
- hash: 3
98
- segments:
99
- - 0
100
- version: "0"
101
- required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
101
  none: false
103
- requirements:
104
- - - ">="
105
- - !ruby/object:Gem::Version
106
- hash: 3
107
- segments:
108
- - 0
109
- version: "0"
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
110
106
  requirements: []
111
-
112
107
  rubyforge_project: i18n_generators
113
- rubygems_version: 1.8.5
108
+ rubygems_version: 1.8.10
114
109
  signing_key:
115
110
  specification_version: 3
116
111
  summary: Generates I18n locale files for Rails 3 and Rails 2
117
- test_files:
112
+ test_files:
118
113
  - spec/cldr_spec.rb
119
114
  - spec/data/cldr/ja.html
120
115
  - spec/data/yml/active_record/en-US.yml