i18n_attributes 0.1.4 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/.gitignore +6 -0
  2. data/CHANGELOG +8 -0
  3. data/README.markdown +44 -0
  4. data/i18n_attributes.gemspec +9 -0
  5. data/lib/generators/i18n_attributes/install/install_generator.rb +3 -10
  6. data/lib/generators/i18n_attributes/install/templates/i18n_attributes.rb +17 -0
  7. data/lib/generators/i18n_attributes/model/model_generator.rb +25 -6
  8. data/lib/generators/i18n_attributes/revise_model/revise_model_generator.rb +12 -7
  9. data/lib/i18n_attributes.rb +4 -0
  10. data/lib/i18n_attributes/configuration.rb +20 -2
  11. data/lib/i18n_attributes/generator_helpers.rb +72 -18
  12. data/lib/i18n_attributes/railtie.rb +2 -0
  13. data/lib/i18n_attributes/version.rb +3 -1
  14. data/spec/i18n_attributes/generator_helpers_spec.rb +103 -0
  15. data/spec/i18n_attributes_spec.rb +14 -0
  16. data/spec/rails3_0_app/.gitignore +4 -0
  17. data/spec/rails3_0_app/Gemfile +9 -0
  18. data/spec/rails3_0_app/Rakefile +7 -0
  19. data/spec/rails3_0_app/app/controllers/application_controller.rb +3 -0
  20. data/spec/rails3_0_app/app/helpers/application_helper.rb +2 -0
  21. data/spec/rails3_0_app/app/models/user.rb +2 -0
  22. data/spec/rails3_0_app/app/views/layouts/application.html.erb +14 -0
  23. data/spec/rails3_0_app/config.ru +4 -0
  24. data/spec/rails3_0_app/config/application.rb +44 -0
  25. data/spec/rails3_0_app/config/boot.rb +6 -0
  26. data/spec/rails3_0_app/config/database.yml +22 -0
  27. data/spec/rails3_0_app/config/environment.rb +5 -0
  28. data/spec/rails3_0_app/config/environments/development.rb +26 -0
  29. data/spec/rails3_0_app/config/environments/production.rb +49 -0
  30. data/spec/rails3_0_app/config/environments/test.rb +35 -0
  31. data/spec/rails3_0_app/config/initializers/backtrace_silencers.rb +7 -0
  32. data/spec/rails3_0_app/config/initializers/i18n_attributes.rb +15 -0
  33. data/spec/rails3_0_app/config/initializers/inflections.rb +10 -0
  34. data/spec/rails3_0_app/config/initializers/mime_types.rb +5 -0
  35. data/spec/rails3_0_app/config/initializers/secret_token.rb +7 -0
  36. data/spec/rails3_0_app/config/initializers/session_store.rb +8 -0
  37. data/spec/rails3_0_app/config/initializers/youdao_fanyi.rb +7 -0
  38. data/spec/rails3_0_app/config/locales/en.yml +5 -0
  39. data/spec/rails3_0_app/config/locales/model_en/user.yml +11 -0
  40. data/spec/rails3_0_app/config/locales/model_zh-CN/user.yml +11 -0
  41. data/spec/rails3_0_app/config/routes.rb +58 -0
  42. data/spec/rails3_0_app/config/youdao_fanyi_api_keys.yml +9 -0
  43. data/spec/rails3_0_app/db/migrate/20111202073515_create_users.rb +13 -0
  44. data/spec/rails3_0_app/db/schema.rb +22 -0
  45. data/spec/rails3_0_app/db/seeds.rb +7 -0
  46. data/spec/rails3_0_app/doc/README_FOR_APP +2 -0
  47. data/spec/rails3_0_app/lib/tasks/.gitkeep +0 -0
  48. data/spec/rails3_0_app/public/404.html +26 -0
  49. data/spec/rails3_0_app/public/422.html +26 -0
  50. data/spec/rails3_0_app/public/500.html +26 -0
  51. data/spec/rails3_0_app/public/favicon.ico +0 -0
  52. data/spec/rails3_0_app/public/images/rails.png +0 -0
  53. data/spec/rails3_0_app/public/index.html +239 -0
  54. data/spec/rails3_0_app/public/javascripts/.gitkeep +0 -0
  55. data/spec/rails3_0_app/public/javascripts/application.js +0 -0
  56. data/spec/rails3_0_app/public/robots.txt +5 -0
  57. data/spec/rails3_0_app/public/stylesheets/.gitkeep +0 -0
  58. data/spec/rails3_0_app/script/rails +6 -0
  59. data/spec/rails3_0_app/test/fixtures/users.yml +7 -0
  60. data/spec/rails3_0_app/test/unit/user_test.rb +8 -0
  61. data/spec/rails3_0_app/vendor/plugins/.gitkeep +0 -0
  62. data/spec/spec_helper.rb +7 -1
  63. metadata +177 -14
data/.gitignore CHANGED
@@ -2,3 +2,9 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+
6
+
7
+ spec/rails3_0_app/db/*.sqlite3
8
+ spec/rails3_0_app/log/*
9
+ spec/rails3_0_app/tmp/*
10
+
data/CHANGELOG CHANGED
@@ -1,3 +1,11 @@
1
+ == 0.1.6
2
+
3
+ * revise_model support mongoid orm
4
+
5
+ * add translator interface
6
+
7
+ * add generator_helper spec
8
+
1
9
  == 0.1.4
2
10
 
3
11
  * drop enums support
@@ -34,6 +34,8 @@ Run:
34
34
 
35
35
  ##Usage Example
36
36
 
37
+ ###Basic Usage
38
+
37
39
  When your generate post model, then hook invoke, create `config/locales/model_zh-CN/post.yml` file
38
40
 
39
41
  > rails g model post title:string
@@ -53,6 +55,48 @@ If your models has been created, you want generate model attributes i18n locale
53
55
  create config/locales/model_en/post.yml
54
56
  create config/locales/model_zh-CN/post.yml
55
57
 
58
+ ###Translate attribute
59
+
60
+ If you want translate attribute or model name
61
+
62
+ First, edit `config/initializers/i18n_attributes.rb` file, like this
63
+
64
+ if Rails.env.development?
65
+ I18nAttributes.configure do |config|
66
+ config.locales = [:en, :"zh-CN"]
67
+ config.translator = {
68
+ ##if use this, you mast install youdao_fanyi, see https://github.com/vkill/youdao_fanyi
69
+ :"zh-CN" => Proc.new{|str| YoudaoFanyi.t(str).first}
70
+ }
71
+ end
72
+ end
73
+
74
+ Then, install `youdao_fanyi`, see https://github.com/vkill/youdao_fanyi. you also use `to _lang` and more.
75
+
76
+ Last, run `rails g i18n_attributes:revise_model`, results like this
77
+
78
+ > rails g i18n_attributes:revise_model
79
+ create config/locales/model_en/user.yml
80
+ INFO translated attribute/model_name id
81
+ INFO translated attribute/model_name username
82
+ INFO translated attribute/model_name created_at
83
+ INFO translated attribute/model_name updated_at
84
+ INFO translated attribute/model_name User
85
+ create config/locales/model_zh-CN/user.yml
86
+
87
+ > cat config/locales/model_zh-CN/user.yml
88
+ ---
89
+ zh-CN:
90
+ activerecord:
91
+ models:
92
+ user: 用户
93
+ attributes:
94
+ user:
95
+ id: id
96
+ username: 用户名
97
+ created_at: created_at
98
+ updated_at: updated_at
99
+
56
100
 
57
101
  ##Copyright
58
102
 
@@ -22,7 +22,16 @@ Gem::Specification.new do |s|
22
22
  s.add_development_dependency "rake"
23
23
  s.add_development_dependency "rspec"
24
24
  s.add_development_dependency "guard-rspec"
25
+ s.add_development_dependency "pry"
26
+ s.add_development_dependency "rspec-rails"
27
+
28
+ #use in spec/rails3_0_app
29
+ s.add_development_dependency "rails", "~> 3.0"
30
+ s.add_development_dependency "sqlite3-ruby"
31
+ s.add_development_dependency "youdao_fanyi"
32
+
25
33
  s.add_dependency "rails", "~> 3.0"
34
+ s.add_dependency "activesupport", "~> 3.0"
26
35
  s.add_dependency "vkill_gems_methods", "~> 0.1.0"
27
36
  end
28
37
 
@@ -1,17 +1,10 @@
1
+ #encoding: utf-8
2
+
1
3
  class I18nAttributes::InstallGenerator < Rails::Generators::Base
2
4
  source_root File.expand_path('../templates', __FILE__)
3
5
 
4
6
  def create_initializer_file
5
- initializer 'i18n_attributes.rb' do
6
- %Q/
7
- if Rails.env.development?
8
- I18nAttributes.configure do |config|
9
- #more > I18n.available_locales
10
- config.locales = [:en, :"zh-CN"]
11
- end
12
- end
13
- /
14
- end
7
+ template "i18n_attributes.rb", 'config/initializers/i18n_attributes.rb'
15
8
  end
16
9
 
17
10
  def modify_application_config
@@ -0,0 +1,17 @@
1
+ #encoding: utf-8
2
+
3
+ if Rails.env.development?
4
+ I18nAttributes.configure do |config|
5
+ # more see https://github.com/svenfuchs/rails-i18n
6
+ config.locales = [:en, :"zh-CN"]
7
+
8
+ config.translator = {
9
+ ##if use this, you mast install youdao_fanyi, see https://github.com/vkill/youdao_fanyi
10
+ #:"zh-CN" => Proc.new{|str| YoudaoFanyi.t(str).first}
11
+
12
+ ##if use this, you mast install and config to_lang, see https://github.com/jimmycuadra/to_lang
13
+ #:"es" => Proc.new{|str| str.translate('es', :from => 'en') }
14
+ }
15
+ end
16
+ end
17
+
@@ -1,22 +1,30 @@
1
+ #encoding: utf-8
2
+
1
3
  class I18nAttributes::ModelGenerator < Rails::Generators::NamedBase
2
4
  source_root File.expand_path('../templates', __FILE__)
3
5
 
4
6
  class_option :orm, :required => true
5
7
  argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
6
8
 
7
- include I18nAttributes::GeneratorHelpers
9
+ include ::VkillGemsMethods::Rails::Generators::Base
8
10
 
9
- SUPPORTED_ORMS = %w(active_record active_model mongoid)
11
+ include ::I18nAttributes::GeneratorHelpers
10
12
 
11
- def create_model_i18n_file
13
+ SUPPORTED_ORMS = %w(active_model active_record mongoid)
12
14
 
13
- orm = options.orm.to_s
15
+ def create_model_i18n_file
14
16
 
15
- say_error "#{orm} [not found]" unless SUPPORTED_ORMS.include? orm
17
+ say_error "#{orm} [not found]" unless SUPPORTED_ORMS.include? options.orm.to_s
16
18
 
17
19
  I18nAttributes::Configuration.locales.each do |locale|
18
20
  create_file "config/locales/model_#{ locale }/#{ file_name }.yml",
19
- generate_yaml_file_data(locale, singular_name, human_name, attributes_hash, orm)
21
+ generate_yaml_file_data(
22
+ :locale => locale,
23
+ :singular_name => singular_name,
24
+ :human_name => human_name,
25
+ :attributes => attributes_hash(),
26
+ :model_i18n_scope => model_i18n_scope()
27
+ ){|word| say_info "translated attribute/model_name #{word}"}.yaml_file_data
20
28
  end
21
29
 
22
30
  end
@@ -25,5 +33,16 @@ class I18nAttributes::ModelGenerator < Rails::Generators::NamedBase
25
33
  def attributes_hash
26
34
  Hash[ attributes.map {|attribute| [attribute.name, attribute.type] } ]
27
35
  end
36
+
37
+ def model_i18n_scope
38
+ case options.orm.to_s
39
+ when "active_model"
40
+ :activemodel
41
+ when "active_record"
42
+ :activerecord
43
+ when "mongoid"
44
+ :mongoid
45
+ end
46
+ end
28
47
  end
29
48
 
@@ -1,3 +1,5 @@
1
+ #encoding: utf-8
2
+
1
3
  class I18nAttributes::ReviseModelGenerator < Rails::Generators::Base
2
4
  source_root File.expand_path('../templates', __FILE__)
3
5
 
@@ -6,21 +8,24 @@ class I18nAttributes::ReviseModelGenerator < Rails::Generators::Base
6
8
  include ::VkillGemsMethods::Rails::Generators::Base
7
9
  ::ActiveRecord::Base.send :include, ::VkillGemsMethods::ActiveRecord::Base
8
10
 
9
- include I18nAttributes::GeneratorHelpers
11
+ include ::I18nAttributes::GeneratorHelpers
10
12
 
11
- SUPPORTED_ORMS = %w(active_record)
13
+ SUPPORTED_ORMS = %w(active_model active_record mongoid)
12
14
 
13
15
  def create_model_i18n_file
14
16
 
15
- orm = options.orm.to_s
16
-
17
- say_error "#{orm} [not found]" unless SUPPORTED_ORMS.include? orm
17
+ say_error "#{orm} [not found]" unless SUPPORTED_ORMS.include? options.orm.to_s
18
18
 
19
19
  ::ActiveRecord::Base.models do |model, columns|
20
20
  ::I18nAttributes::Configuration.locales.each do |locale|
21
21
  create_file "config/locales/model_#{ locale }/#{ model.name.underscore }.yml",
22
- generate_yaml_file_data(locale, model.model_name.underscore, model.model_name, columns,
23
- orm, model.i18n_scope)
22
+ generate_yaml_file_data(
23
+ :locale => locale,
24
+ :singular_name => model.model_name.underscore,
25
+ :human_name => model.model_name,
26
+ :attributes => columns,
27
+ :model_i18n_scope => model.i18n_scope
28
+ ){|word| say_info "translated attribute/model_name #{word}"}.yaml_file_data
24
29
  end
25
30
  end
26
31
 
@@ -1,5 +1,9 @@
1
+ #encoding: utf-8
2
+ $KCODE = 'u' unless RUBY_VERSION >= "1.9"
3
+
1
4
  require "i18n_attributes/version"
2
5
 
6
+ require "yaml"
3
7
  require "vkill_gems_methods"
4
8
 
5
9
  require "i18n_attributes/configuration"
@@ -1,8 +1,26 @@
1
+ #encoding: utf-8
2
+ =begin
3
+ I18nAttributes.configure do |config|
4
+ # more see https://github.com/svenfuchs/rails-i18n
5
+ config.locales = [:en, :"zh-CN"]
6
+
7
+ config.translator = {
8
+ #you mast install youdao_fanyi, see https://github.com/vkill/youdao_fanyi
9
+ :"zh-CN" => Proc.new{|str| YoudaoFanyi.t(str).first}
10
+
11
+ ##you mast install and config to_lang, see https://github.com/jimmycuadra/to_lang
12
+ #:"es" => Proc.new{|str| str.translate('es', :from => 'en') }
13
+ }
14
+ end
15
+ =end
16
+
1
17
  module I18nAttributes
2
18
  Configuration = Struct.new(
3
- :locales
19
+ :locales,
20
+ :translator
4
21
  ).new(
5
- [:en]
22
+ [:en],
23
+ {}
6
24
  )
7
25
  end
8
26
 
@@ -1,30 +1,84 @@
1
- require "yaml"
1
+ #encoding: utf-8
2
2
 
3
3
  module I18nAttributes
4
4
  module GeneratorHelpers
5
5
 
6
- def generate_yaml_file_data(locale, singular_name, human_name, attributes, orm="active_record", model_i18n_scope="activerecord")
7
- YAML.dump_stream(
8
- {
9
- locale.to_s => {
10
- model_i18n_scope.to_s => {
11
- "models" => {
12
- singular_name => human_name.to_s
13
- },
14
- "attributes" => {
15
- singular_name => columns_hash(attributes)
6
+ def generate_yaml_file_data(*options, &block)
7
+ YamlFileData.new(*options, &block)
8
+ end
9
+
10
+ class YamlFileData
11
+
12
+ attr_reader :locale, :singular_name, :human_name, :attributes, :model_i18n_scope,
13
+ :locale_translator, :columns_hash, :yaml_file_data
14
+
15
+ def initialize(*options, &block)
16
+ options = options.extract_options!
17
+ return self if test = options.delete(:test) || false
18
+ locale = options.delete(:locale) || raise("locale is required.")
19
+ singular_name = options.delete(:singular_name) || raise("singular_name is required.")
20
+ human_name = options.delete(:human_name) || raise("human_name is required.")
21
+ attributes = options.delete(:attributes) || raise("attributes is required.")
22
+ model_i18n_scope = options.delete(:model_i18n_scope) || raise("model_i18n_scope is required.")
23
+ @locale = locale.to_s
24
+ @singular_name = singular_name.to_s
25
+ @human_name = human_name.to_s
26
+ @attributes = attributes.to_hash
27
+ @model_i18n_scope = model_i18n_scope.to_s
28
+
29
+
30
+ set_locale_translator(&block)
31
+ set_columns_hash(&block)
32
+ generate(&block)
33
+ @yaml_file_data
34
+ end
35
+
36
+ def generate(&block)
37
+ @yaml_file_data =
38
+ YAML.dump_stream(
39
+ {
40
+ locale => {
41
+ model_i18n_scope => {
42
+ "models" => {
43
+ singular_name => @locale_translator ? translate(human_name, &block) : human_name
44
+ },
45
+ "attributes" => {
46
+ singular_name => @columns_hash
47
+ }
48
+ }
16
49
  }
17
50
  }
18
- }
19
- }
20
- )
21
- end
51
+ )
52
+ end
53
+
54
+ def set_columns_hash(&block)
55
+ @columns_hash =
56
+ Hash[ @attributes.keys.map {|k|
57
+ [
58
+ k.to_s,
59
+ @locale_translator ? translate(k, &block) : k.to_s.humanize
60
+ ]
61
+ }
62
+ ]
63
+ end
22
64
 
23
- private
24
- def columns_hash(attributes)
25
- Hash[ attributes.keys.map {|k| [k.to_s, k.to_s.humanize]} ]
65
+ def set_locale_translator(&block)
66
+ translator = I18nAttributes::Configuration.translator
67
+ locale_translator = translator[@locale.to_sym] || translator[@locale.to_sym]
68
+ @locale_translator =
69
+ if !locale_translator.blank? and locale_translator.class == Proc
70
+ locale_translator
71
+ else
72
+ nil
73
+ end
26
74
  end
27
75
 
76
+ def translate(word, &block)
77
+ yield word.to_s if block
78
+ @locale_translator.call(word.to_s)
79
+ end
80
+ end
81
+
28
82
  end
29
83
  end
30
84
 
@@ -1,3 +1,5 @@
1
+ #encoding: utf-8
2
+
1
3
  module SeedUpgrade
2
4
  class Railtie < ::Rails::Railtie
3
5
  generators do
@@ -1,4 +1,6 @@
1
+ #encoding: utf-8
2
+
1
3
  module I18nAttributes
2
- VERSION = "0.1.4"
4
+ VERSION = "0.1.6"
3
5
  end
4
6
 
@@ -0,0 +1,103 @@
1
+ #encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe I18nAttributes::GeneratorHelpers::YamlFileData do
5
+
6
+ def reset_subject(*options, &block)
7
+ options = options.extract_options!
8
+ locale = options.delete(:locale) || :en
9
+ singular_name = options.delete(:singular_name) || :user
10
+ human_name = options.delete(:human_name) || :User
11
+ attributes = options.delete(:attributes) || {"username"=>:string}
12
+ model_i18n_scope = options.delete(:model_i18n_scope) || :activerecord
13
+ I18nAttributes::GeneratorHelpers::YamlFileData.new(
14
+ :locale => locale.to_s, :singular_name => singular_name.to_s, :human_name => human_name.to_s,
15
+ :attributes => attributes.to_hash, :model_i18n_scope => model_i18n_scope.to_s , &block)
16
+ end
17
+
18
+ it "instance var should set" do
19
+ subject = reset_subject()
20
+ subject.locale.should == 'en'
21
+ subject.singular_name.should == "user"
22
+ subject.human_name.should == "User"
23
+ subject.attributes.should == {"username"=>:string}
24
+ subject.model_i18n_scope.should == "activerecord"
25
+ end
26
+
27
+ context "test set_locale_translator" do
28
+ it "when translator don't defined, locale_translator is nil" do
29
+ I18nAttributes::Configuration.translator = {}
30
+ subject = reset_subject(:locale => :en)
31
+ subject.locale_translator.should be_nil
32
+ end
33
+ it "when translator defined, but locale is don't match reset_subject method defined locale, locale_translator is nil" do
34
+ I18nAttributes::Configuration.translator = {:"zh-CN" => Proc.new{|str| str.to_s}}
35
+ subject = reset_subject(:locale => :en)
36
+ subject.locale_translator.should be_nil
37
+ end
38
+ it "when translator defined, but is not Proc, locale_translator is nil" do
39
+ I18nAttributes::Configuration.translator = {:en => "test"}
40
+ subject = reset_subject(:locale => :en)
41
+ subject.locale_translator.should be_nil
42
+ end
43
+ it "when translator defined, and is a Proc, and locale match, locale_translator is Proc object" do
44
+ I18nAttributes::Configuration.translator = {:"zh-CN" => Proc.new{|str| str.to_s}}
45
+ subject = reset_subject(:locale => :"zh-CN")
46
+ subject.locale_translator.should be_kind_of(Proc)
47
+ end
48
+ end
49
+
50
+ context "test set_columns_hash" do
51
+ it "if not locale_translator, columns_hash.v should eq columns_hash.k.humanize" do
52
+ I18nAttributes::Configuration.translator = {}
53
+ subject = reset_subject(:attributes => {"username"=>:string})
54
+ subject.columns_hash['username'].should == "username".humanize
55
+ end
56
+
57
+ it "if locale_translator, columns_hash.v should eq locale_translator.call(columns_hash.k)" do
58
+ I18nAttributes::Configuration.translator = {:"zh-CN" => Proc.new{|str| str.to_s.humanize}}
59
+ subject = reset_subject(:locale => :"zh-CN", :attributes => {"username"=>:string})
60
+ subject.columns_hash['username'].should == Proc.new{|str| str.to_s.humanize}.call('username')
61
+ end
62
+ end
63
+
64
+ context "test generate" do
65
+ it "yaml_file_data is a String, is YAML.dump_stream" do
66
+ subject = reset_subject()
67
+ subject.yaml_file_data.should be_kind_of(String)
68
+ end
69
+ it "YAML.load_stream yaml_file_data" do
70
+ subject = reset_subject()
71
+ data = YAML.load_stream(subject.yaml_file_data).first
72
+ data['en']['activerecord']['attributes']['user']['username'].should == "Username"
73
+ end
74
+ end
75
+
76
+ it "test block" do
77
+ I18nAttributes::Configuration.translator = {:"zh-CN" => Proc.new{|str| str.to_s.humanize}}
78
+ block_results = []
79
+ reset_subject(:locale => :"zh-CN", :attributes => {"username"=>:string},
80
+ :human_name => :User) {|word| block_results << word}
81
+ block_results.should include('username')
82
+ block_results.should include('User')
83
+ end
84
+ end
85
+
86
+ describe I18nAttributes::GeneratorHelpers do
87
+ before {
88
+ class TestGeneratorHelpers
89
+ include I18nAttributes::GeneratorHelpers
90
+ end
91
+ }
92
+
93
+ it "should has generate_yaml_file_data" do
94
+ TestGeneratorHelpers.new.should respond_to(:generate_yaml_file_data)
95
+ end
96
+
97
+ it "generate_yaml_file_data return a instance of YamlFileData" do
98
+ TestGeneratorHelpers.new.generate_yaml_file_data(:test => true).should be_instance_of(
99
+ I18nAttributes::GeneratorHelpers::YamlFileData)
100
+ end
101
+
102
+ end
103
+