amatsuda-i18n_generators 0.0.8 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/generators/i18n/i18n_generator.rb +16 -3
- data/generators/i18n_locales/i18n_locales_command.rb +10 -2
- data/generators/i18n_models/i18n_models_command.rb +1 -1
- data/generators/i18n_models/lib/translator.rb +1 -1
- data/spec/i18n_locales_command_spec.rb +62 -0
- data/spec/translator_spec.rb +44 -0
- metadata +4 -2
@@ -30,15 +30,28 @@ class I18nGenerator < Rails::Generator::NamedBase
|
|
30
30
|
m.directory 'config/locales'
|
31
31
|
unless self.generate_models_only
|
32
32
|
m.generate_configuration
|
33
|
-
|
34
|
-
|
35
|
-
|
33
|
+
if defined_in_rails_i18n_repository?
|
34
|
+
m.fetch_from_rails_i18n_repository
|
35
|
+
else
|
36
|
+
m.active_support_yaml
|
37
|
+
m.active_record_yaml
|
38
|
+
m.action_view_yaml
|
39
|
+
end
|
36
40
|
end
|
37
41
|
unless self.generate_locales_only
|
38
42
|
m.models_yaml
|
39
43
|
end
|
40
44
|
end
|
41
45
|
end
|
46
|
+
|
47
|
+
private
|
48
|
+
def defined_in_rails_i18n_repository?
|
49
|
+
begin
|
50
|
+
OpenURI.open_uri("http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale/#{locale_name}.yml").status == %w[200 OK]
|
51
|
+
rescue
|
52
|
+
false
|
53
|
+
end
|
54
|
+
end
|
42
55
|
end
|
43
56
|
|
44
57
|
require File.join(File.dirname(__FILE__), '../i18n_locales/i18n_locales_command')
|
@@ -10,14 +10,22 @@ module I18nGenerator::Generator
|
|
10
10
|
module Commands #:nodoc:
|
11
11
|
module Create
|
12
12
|
def generate_configuration
|
13
|
-
if
|
13
|
+
return if I18n.default_locale == locale_name
|
14
|
+
if Rails.configuration.respond_to? :i18n # >= 2.2.2
|
14
15
|
# edit environment.rb file
|
15
16
|
environment = add_locale_config File.read(File.join(Rails.root, 'config/environment.rb'))
|
16
17
|
File.open File.join(Rails.root, 'config/environment.rb'), 'w' do |f|
|
17
18
|
f.puts environment
|
18
19
|
end
|
20
|
+
puts " update config/environment.rb"
|
19
21
|
else
|
20
|
-
|
22
|
+
template 'i18n:i18n_config.rb', 'config/initializers/i18n_config.rb', :assigns => {:locale_name => locale_name}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def fetch_from_rails_i18n_repository
|
27
|
+
file('i18n:base.yml', "config/locales/#{locale_name}.yml") do |f|
|
28
|
+
OpenURI.open_uri("http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale/#{locale_name}.yml?raw=true").read
|
21
29
|
end
|
22
30
|
end
|
23
31
|
|
@@ -10,7 +10,7 @@ module I18nGenerator::Generator
|
|
10
10
|
I18n.locale = locale_name
|
11
11
|
models = model_filenames.map do |model_name|
|
12
12
|
model = begin
|
13
|
-
m = model_name.
|
13
|
+
m = model_name.camelize.constantize
|
14
14
|
next unless m.ancestors.include? ActiveRecord::Base
|
15
15
|
m
|
16
16
|
rescue
|
@@ -12,7 +12,7 @@ module I18nModelsGeneratorModule
|
|
12
12
|
w = CGI.escape ActiveSupport::Inflector.humanize(word)
|
13
13
|
json = OpenURI.open_uri("http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=#{w}&langpair=en%7C#{@lang}").read
|
14
14
|
result = ActiveSupport::JSON.decode(json)
|
15
|
-
|
15
|
+
result['responseStatus'] == 200 ? (@cache[word] = result['responseData']['translatedText']) : word
|
16
16
|
rescue => e
|
17
17
|
puts %Q[failed to translate "#{word}" into "#{@lang}" language.]
|
18
18
|
word
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '/../generators/i18n_locales/i18n_locales_command')
|
2
|
+
|
3
|
+
describe I18nGenerator::Generator::Commands::Create do
|
4
|
+
before do
|
5
|
+
(@command = Object.new).extend I18nGenerator::Generator::Commands::Create
|
6
|
+
@command.stub!(:locale_name).and_return('ja')
|
7
|
+
end
|
8
|
+
|
9
|
+
describe 'add_locale_config' do
|
10
|
+
describe 'when i18n.default_locale is configured in environment.rb' do
|
11
|
+
before do
|
12
|
+
@config = "
|
13
|
+
Rails::Initializer.run do |config|
|
14
|
+
config.i18n.default_locale = :de
|
15
|
+
end"
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'rewrites the existing default_locale to locale_name value' do
|
19
|
+
@command.send(:add_locale_config, @config).should == "
|
20
|
+
Rails::Initializer.run do |config|
|
21
|
+
config.i18n.default_locale = 'ja'
|
22
|
+
end"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'when i18n.default_locale config is commented in environment.rb' do
|
27
|
+
before do
|
28
|
+
@config = "
|
29
|
+
Rails::Initializer.run do |config|
|
30
|
+
# config.i18n.default_locale = :de
|
31
|
+
end"
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'uncomments the existing commented i18n config and sets locale_name value' do
|
35
|
+
@command.send(:add_locale_config, @config).should == "
|
36
|
+
Rails::Initializer.run do |config|
|
37
|
+
config.i18n.default_locale = 'ja'
|
38
|
+
end"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe 'when i18n.default_locale is not written in environment.rb' do
|
43
|
+
before do
|
44
|
+
@config = "
|
45
|
+
Rails::Initializer.run do |config|
|
46
|
+
something goes here.
|
47
|
+
bla bla bla...
|
48
|
+
end"
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'adds the default_locale config inside the config block and sets locale_name value' do
|
52
|
+
@command.send(:add_locale_config, @config).should == "
|
53
|
+
Rails::Initializer.run do |config|
|
54
|
+
something goes here.
|
55
|
+
bla bla bla...
|
56
|
+
config.i18n.default_locale = 'ja'
|
57
|
+
end"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
$KCODE = 'U'
|
2
|
+
|
3
|
+
require File.join(File.dirname(__FILE__), '../generators/i18n_models/lib/translator')
|
4
|
+
include I18nModelsGeneratorModule
|
5
|
+
|
6
|
+
describe Translator do
|
7
|
+
before(:each) do
|
8
|
+
@translator = Translator.new 'ja'
|
9
|
+
end
|
10
|
+
|
11
|
+
describe 'when successfully translated' do
|
12
|
+
before do
|
13
|
+
res_200 = mock('res_200')
|
14
|
+
res_200.stub!(:read).and_return('{"responseData": {"translatedText":"こんにちは"}, "responseDetails": null, "responseStatus": 200}')
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'returns translated text' do
|
18
|
+
@translator.translate('hello').should == 'こんにちは'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'when translation failed with error code' do
|
23
|
+
before do
|
24
|
+
res_500 = mock('res_500')
|
25
|
+
res_500.stub!(:read).and_return('{"responseData": {"translatedText":"こんにちは?"}, "responseDetails": null, "responseStatus": 500}')
|
26
|
+
OpenURI.stub!(:open_uri).and_return(res_500)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'returns the original text' do
|
30
|
+
@translator.translate('hello').should == 'hello'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe 'when translation raised an error' do
|
35
|
+
before do
|
36
|
+
OpenURI.stub!(:open_uri).and_raise('ERROR!')
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'returns the original text' do
|
40
|
+
@translator.translate('hello').should == 'hello'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: amatsuda-i18n_generators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Akira Matsuda
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-11-
|
12
|
+
date: 2008-11-26 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -50,6 +50,8 @@ files:
|
|
50
50
|
- spec/cldr_spec.rb
|
51
51
|
- spec/data/cldr/ja.html
|
52
52
|
- spec/data/yml/active_record/en-US.yml
|
53
|
+
- spec/i18n_locales_command_spec.rb
|
54
|
+
- spec/translator_spec.rb
|
53
55
|
- spec/yaml_spec.rb
|
54
56
|
has_rdoc: false
|
55
57
|
homepage: http://github.com/amatsuda/i18n_generators/
|