rails-i18n-generator 0.1.0

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: 0d5d7f80d22f4018485bd4f45be6faa1ea9e924f
4
+ data.tar.gz: e978b2fb3325bd8416d8bfe8803ee6cc41639664
5
+ SHA512:
6
+ metadata.gz: 041003595a94b1b438b0348e5eccae74ea528a6fbf5ce696f8f51fd11a925612f915625a2860be8985d68bf4c9fcf9fd1a765a5eb7790f22aef2500254ee5e17
7
+ data.tar.gz: 5cece0b986bc9ca27ab34dfaff5c0fd5babea441a5b9fef3870d7ad2d7150f27875e402d6bae3792640c1bdd63b253d71ac04f860cb16294419a599f9f19fdf8
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails-i18n-generator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 xiaohui
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # Rails::I18n::Generator
2
+
3
+ Creates yaml locale files for models and views when using the model / scaffold_controller generators.
4
+
5
+ Locale files are generated for all locales listed in I18n.available_locales.
6
+
7
+ ## Description
8
+
9
+ Creates yaml locale files for models and views when using the model / scaffold_controller generators.
10
+
11
+ Locale files are generated for all locales listed in I18n.available_locales.
12
+
13
+ #### Currently support ORMs
14
+
15
+ * ActiveRecord
16
+ * ActiveModel
17
+ * Mongoid
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ ```ruby
24
+ gem 'rails-i18n-generator'
25
+ ```
26
+
27
+ And then execute:
28
+
29
+ $ bundle
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install rails-i18n-generator
34
+
35
+ ## Usage
36
+
37
+ Make sure you have something like this in application.rb:
38
+
39
+ config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
40
+
41
+ Then below command will generate locale files properly
42
+
43
+ $ rails g scaffold Article title:string body:text
44
+ $ rails g model Article title:string body:text
45
+ $ rails g scaffold_controller Article
46
+ $ rails g rails_i18n:model Article title:string body:text
47
+ $ rails g rails_i18n:scaffold_controller Article
48
+
49
+ ## Configuration
50
+
51
+ Create config/initializers/rails_i18n_generators.rb
52
+
53
+ MyApplication::Application.config.rails_i18n_generator.tap do |g|
54
+
55
+ # Whether content in the generated locale files should be commented out (default: false)
56
+ g.commented = true
57
+
58
+ end
59
+
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it ( https://github.com/xiaohui-zhangxh/rails-i18n-generator/fork )
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,58 @@
1
+ module RailsI18n
2
+ module Generators #:nodoc:
3
+ class ModelGenerator < Rails::Generators::NamedBase #:nodoc:
4
+ class_option :orm, :required => true
5
+ class_option :commented, :default => Rails.application.config.rails_i18n_generator.commented
6
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
7
+
8
+ include Rails::Generators::ResourceHelpers
9
+ include Rails::I18n::Generator::Helpers
10
+
11
+ SUPPORTED_ORMS = {
12
+ "active_record" => "activerecord",
13
+ "active_model" => "activemodel",
14
+ "mongoid"=> "mongoid"
15
+ }
16
+
17
+ def model_i18n_yaml_file
18
+ current_orm = options.orm.to_s
19
+
20
+ unless SUPPORTED_ORMS.keys.include? current_orm
21
+ say "Not creating translation file - '#{current_orm}' not supported"
22
+ return
23
+ end
24
+
25
+ orm_i18n_key = SUPPORTED_ORMS[current_orm]
26
+
27
+ I18n.available_locales.each do |locale|
28
+ hash_for_yaml = {}
29
+
30
+ # Model name
31
+ hash_for_yaml.deep_merge! wrap_hash(human_name, [orm_i18n_key, 'models', i18n_scope.gsub('.', '/')])
32
+
33
+ # Attributes
34
+ hash_for_yaml.deep_merge! wrap_hash(attributes_hash, [orm_i18n_key, 'attributes', i18n_scope.gsub('.', '/')])
35
+
36
+ # Errors
37
+ hash_for_yaml.deep_merge! wrap_hash({ singular_name => nil }, [orm_i18n_key, 'errors', 'models'])
38
+
39
+ # Helpers
40
+ hash_for_yaml.deep_merge! wrap_hash({ singular_name => nil }, ['helpers'])
41
+
42
+ yaml_content = { locale.to_s => hash_for_yaml }.to_yaml
43
+
44
+ destination_path = File.join('config/locales/models', "#{file_path}.#{locale}.yml")
45
+
46
+ create_file destination_path, options.commented ? comment_yaml_body(yaml_content) : yaml_content
47
+ end
48
+ end
49
+
50
+ protected
51
+
52
+ def attributes_hash
53
+ attributes.inject({}) { |hsh, a| hsh.merge(a.name => a.human_name) }
54
+ end
55
+
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,46 @@
1
+ module RailsI18n
2
+ module Generators #:nodoc:
3
+ class ScaffoldControllerGenerator < Rails::Generators::NamedBase #:nodoc:
4
+ include Rails::Generators::ResourceHelpers
5
+ include Rails::I18n::Generator::Helpers
6
+ class_option :commented, :default => Rails.application.config.rails_i18n_generator.commented
7
+
8
+ def views_i18n_yaml_file
9
+ titles_hash = {
10
+ 'index' => { 'title' => "#{human_name.pluralize}" },
11
+ 'show' => { 'title' => human_name },
12
+ 'edit' => { 'title' => "Editing #{human_name}" },
13
+ 'new' => { 'title' => "New #{human_name}" }
14
+ }
15
+
16
+ flash_hash = {
17
+ 'create' => {
18
+ 'notice' => '%{resource_name} was successfully created.'
19
+ },
20
+ 'update' => {
21
+ 'notice' => '%{resource_name} was successfully updated.'
22
+ },
23
+ 'destroy' => {
24
+ 'notice' => '%{resource_name} was successfully deleted.',
25
+ 'alert' => '%{resource_name} could not be deleted.'
26
+ }
27
+ }
28
+
29
+ scope_array = controller_i18n_scope.split('.')
30
+
31
+ I18n.available_locales.each do |locale|
32
+ hash_for_yaml = {}
33
+
34
+ hash_for_yaml.deep_merge! wrap_hash(titles_hash, scope_array)
35
+ hash_for_yaml.deep_merge! wrap_hash(flash_hash, %w(flash) + scope_array)
36
+
37
+ yaml_content = { locale.to_s => hash_for_yaml }.to_yaml
38
+
39
+ destination_path = File.join('config/locales/views/', "#{controller_file_path}.#{locale}.yml")
40
+ create_file destination_path, options.commented ? comment_yaml_body(yaml_content) : yaml_content
41
+ end
42
+ end
43
+
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,27 @@
1
+ require "rails/i18n/generator/version"
2
+
3
+ module Rails
4
+ module I18n
5
+ module Generator
6
+ Configuration = Struct.new(:commented)
7
+
8
+ class Railtie < ::Rails::Railtie
9
+ config.rails_i18n_generator = Rails::I18n::Generator::Configuration.new(false)
10
+
11
+ generators do |app|
12
+ if ::Rails::VERSION::STRING >= '3.2'
13
+ Rails::Generators.configure!(app.config.generators)
14
+ end
15
+
16
+ require 'rails/i18n/generator/helpers'
17
+ require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
18
+ require 'rails/generators/rails/model/model_generator'
19
+ Rails::Generators::ModelGenerator.send(:hook_for, 'rails_i18n')
20
+ Rails::Generators::ModelGenerator.send(:class_option, 'rails_i18n', :default => 'rails_i18n')
21
+ Rails::Generators::ScaffoldControllerGenerator.send(:hook_for, 'rails_i18n')
22
+ Rails::Generators::ScaffoldControllerGenerator.send(:class_option, 'rails_i18n', :default => 'rails_i18n')
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,21 @@
1
+ module Rails
2
+ module I18n
3
+ module Generator
4
+ module Helpers
5
+ def wrap_hash(obj, scope)
6
+ hsh = nil
7
+ scope.reverse.each do |scope_node|
8
+ hsh = { scope_node => hsh || obj }
9
+ end
10
+ return hsh
11
+ end
12
+
13
+ def comment_yaml_body(string)
14
+ lines = string.lines.to_a
15
+ new_lines = lines[0..1] + lines[2..-1].collect{ |line| line.gsub(/^(.?)/, '#\1') }
16
+ new_lines.join
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ module Rails
2
+ module I18n
3
+ module Generator
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails/i18n/generator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails-i18n-generator"
8
+ spec.version = Rails::I18n::Generator::VERSION
9
+ spec.authors = ["xiaohui"]
10
+ spec.email = ["xiaohui@zhangxh.net"]
11
+ spec.summary = %q{Generate locales for models and controlers}
12
+ spec.description = %q{This gem is re-built from gem i18n_yaml_generator}
13
+ spec.homepage = "http://github.com/xiaohui-zhangxh/rails-i18n-generatorREADME.md"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "rails", "> 3.2.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-i18n-generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - xiaohui
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>'
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>'
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: This gem is re-built from gem i18n_yaml_generator
56
+ email:
57
+ - xiaohui@zhangxh.net
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/generators/rails_i18n/model/model_generator.rb
68
+ - lib/generators/rails_i18n/scaffold_controller/scaffold_controller_generator.rb
69
+ - lib/rails/i18n/generator.rb
70
+ - lib/rails/i18n/generator/helpers.rb
71
+ - lib/rails/i18n/generator/version.rb
72
+ - rails-i18n-generator.gemspec
73
+ homepage: http://github.com/xiaohui-zhangxh/rails-i18n-generatorREADME.md
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.4
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Generate locales for models and controlers
97
+ test_files: []