i18n_yaml_generator 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +106 -0
- data/Rakefile +2 -0
- data/i18n_yaml_generator.gemspec +21 -0
- data/lib/generators/i18n_yaml/model/model_generator.rb +56 -0
- data/lib/generators/i18n_yaml/scaffold_controller/scaffold_controller_generator.rb +49 -0
- data/lib/i18n_yaml_generator.rb +18 -0
- data/lib/i18n_yaml_generator/helpers.rb +18 -0
- data/lib/i18n_yaml_generator/version.rb +3 -0
- metadata +66 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
# I18n yaml generator (for Rails 3)
|
2
|
+
|
3
|
+
## Description
|
4
|
+
|
5
|
+
Creates yaml locale files for models and views when using the model / scaffold_controller generators.
|
6
|
+
|
7
|
+
Locale files are generated for all locales listed in I18n.available_locales.
|
8
|
+
|
9
|
+
#### Currently support ORMs
|
10
|
+
|
11
|
+
* ActiveRecord
|
12
|
+
* ActiveModel
|
13
|
+
* Mongoid
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
Add the gem to Gemfile
|
18
|
+
|
19
|
+
gem 'i18n_yaml_generator'
|
20
|
+
|
21
|
+
Make sure you have something like this in application.rb:
|
22
|
+
|
23
|
+
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
|
24
|
+
|
25
|
+
## Configuration
|
26
|
+
|
27
|
+
Create config/initializers/i18n_yaml_generators.rb
|
28
|
+
|
29
|
+
MyApplication::Application.config.i18n_yaml_generator.tap do |g|
|
30
|
+
|
31
|
+
# Whether content in the generated locale files should be commented out (default: false)
|
32
|
+
g.commented = true
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
## Example
|
37
|
+
|
38
|
+
rails g scaffold Article title:string body:text
|
39
|
+
invoke active_record
|
40
|
+
create db/migrate/20110329004811_create_articles.rb
|
41
|
+
create app/models/article.rb
|
42
|
+
invoke test_unit
|
43
|
+
create test/unit/article_test.rb
|
44
|
+
create test/fixtures/articles.yml
|
45
|
+
invoke i18n_yaml
|
46
|
+
create config/locales/models/article.en.yml
|
47
|
+
route resources :articles
|
48
|
+
invoke scaffold_controller
|
49
|
+
create app/controllers/articles_controller.rb
|
50
|
+
invoke erb
|
51
|
+
create app/views/articles
|
52
|
+
create app/views/articles/index.html.erb
|
53
|
+
create app/views/articles/edit.html.erb
|
54
|
+
create app/views/articles/show.html.erb
|
55
|
+
create app/views/articles/new.html.erb
|
56
|
+
create app/views/articles/_form.html.erb
|
57
|
+
invoke test_unit
|
58
|
+
create test/functional/articles_controller_test.rb
|
59
|
+
invoke helper
|
60
|
+
create app/helpers/articles_helper.rb
|
61
|
+
invoke test_unit
|
62
|
+
create test/unit/helpers/articles_helper_test.rb
|
63
|
+
invoke i18n_yaml
|
64
|
+
create config/locales/views/articles.en.yml
|
65
|
+
invoke stylesheets
|
66
|
+
create public/stylesheets/scaffold.css
|
67
|
+
|
68
|
+
### config/locales/models/article.en.yml
|
69
|
+
|
70
|
+
---
|
71
|
+
en:
|
72
|
+
active_record:
|
73
|
+
models:
|
74
|
+
article: Article
|
75
|
+
attributes:
|
76
|
+
article:
|
77
|
+
title: Title
|
78
|
+
body: Body
|
79
|
+
errors:
|
80
|
+
article:
|
81
|
+
helpers:
|
82
|
+
article:
|
83
|
+
|
84
|
+
### config/locales/views/articles.en.yml
|
85
|
+
|
86
|
+
---
|
87
|
+
en:
|
88
|
+
articles:
|
89
|
+
index:
|
90
|
+
title: Articles
|
91
|
+
show:
|
92
|
+
title: Article
|
93
|
+
edit:
|
94
|
+
title: Editing Article
|
95
|
+
new:
|
96
|
+
title: New Article
|
97
|
+
flash:
|
98
|
+
articles:
|
99
|
+
create:
|
100
|
+
notice: "%{resource_name} was successfully created."
|
101
|
+
update:
|
102
|
+
notice: "%{resource_name} was successfully updated."
|
103
|
+
destroy:
|
104
|
+
notice: "%{resource_name} was successfully deleted."
|
105
|
+
alert: "%{resource_name} could not be deleted."
|
106
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "i18n_yaml_generator/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "i18n_yaml_generator"
|
7
|
+
s.version = I18nYamlGenerator::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Trym Skaar"]
|
10
|
+
s.email = ["ts@skalar.no"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Nuff said}
|
13
|
+
s.description = %q{Generates i18n yaml locale files for models and resources/views}
|
14
|
+
|
15
|
+
s.rubyforge_project = "i18n-yaml-generator"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module I18nYaml
|
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.i18n_yaml_generator.commented
|
6
|
+
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
|
7
|
+
|
8
|
+
include Rails::Generators::ResourceHelpers
|
9
|
+
include I18nYamlGenerator::Helpers
|
10
|
+
|
11
|
+
SUPPORTED_ORMS = %w(active_record active_model mongoid)
|
12
|
+
|
13
|
+
def model_i18n_yaml_file
|
14
|
+
current_orm = options.orm.to_s
|
15
|
+
|
16
|
+
unless SUPPORTED_ORMS.include? current_orm
|
17
|
+
say "Not creating translation file - '#{current_orm}' not supported"
|
18
|
+
return
|
19
|
+
end
|
20
|
+
|
21
|
+
config = Rails.application.config.i18n_yaml_generator
|
22
|
+
|
23
|
+
I18n.available_locales.each do |locale|
|
24
|
+
hash_for_yaml = {}
|
25
|
+
|
26
|
+
# Model name
|
27
|
+
hash_for_yaml.deep_merge! wrap_hash(human_name, [current_orm, 'models'] + i18n_scope.split('.'))
|
28
|
+
|
29
|
+
# Attributes
|
30
|
+
hash_for_yaml.deep_merge! wrap_hash(attributes_hash, [current_orm, 'attributes'] + i18n_scope.split('.'))
|
31
|
+
|
32
|
+
# Errors
|
33
|
+
hash_for_yaml.deep_merge! wrap_hash({ singular_name => nil }, [current_orm, 'errors'])
|
34
|
+
|
35
|
+
# Helpers
|
36
|
+
hash_for_yaml.deep_merge! wrap_hash({ singular_name => nil }, ['helpers'])
|
37
|
+
|
38
|
+
yaml_content = { locale.to_s => hash_for_yaml }.to_yaml
|
39
|
+
|
40
|
+
destination_path = File.join('config/locales/models', "#{file_path}.#{locale}.yml")
|
41
|
+
|
42
|
+
create_file destination_path, options.commented ? comment_yaml_body(yaml_content) : yaml_content
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
protected
|
47
|
+
|
48
|
+
def attributes_hash
|
49
|
+
attributes.inject({}) { |hsh, a| hsh.merge(a.name => a.human_name) }
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module I18nYaml
|
2
|
+
module Generators #:nodoc:
|
3
|
+
class ScaffoldControllerGenerator < Rails::Generators::NamedBase #:nodoc:
|
4
|
+
include Rails::Generators::ResourceHelpers
|
5
|
+
include I18nYamlGenerator::Helpers
|
6
|
+
class_option :commented, :default => Rails.application.config.i18n_yaml_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
|
47
|
+
|
48
|
+
|
49
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module I18nYamlGenerator
|
2
|
+
Configuration = Struct.new(:commented)
|
3
|
+
|
4
|
+
class Railtie < ::Rails::Railtie
|
5
|
+
config.i18n_yaml_generator = I18nYamlGenerator::Configuration.new(false)
|
6
|
+
|
7
|
+
generators do
|
8
|
+
require 'i18n_yaml_generator/helpers'
|
9
|
+
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
|
10
|
+
require 'rails/generators/rails/model/model_generator'
|
11
|
+
Rails::Generators::ModelGenerator.send(:hook_for, :i18n_yaml)
|
12
|
+
Rails::Generators::ModelGenerator.send(:class_option, :i18n_yaml, :default => 'i18n_yaml')
|
13
|
+
Rails::Generators::ScaffoldControllerGenerator.send(:hook_for, :i18n_yaml)
|
14
|
+
Rails::Generators::ScaffoldControllerGenerator.send(:class_option, :i18n_yaml, :default => 'i18n_yaml')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module I18nYamlGenerator
|
2
|
+
module Helpers
|
3
|
+
def wrap_hash(obj, scope)
|
4
|
+
hsh = nil
|
5
|
+
scope.reverse.each do |scope_node|
|
6
|
+
hsh = { scope_node => hsh || obj }
|
7
|
+
end
|
8
|
+
return hsh
|
9
|
+
end
|
10
|
+
|
11
|
+
def comment_yaml_body(string)
|
12
|
+
lines = string.lines.to_a
|
13
|
+
new_lines = lines[0..1] + lines[2..-1].collect{ |line| line.gsub(/^(.?)/, '#\1') }
|
14
|
+
new_lines.join
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: i18n_yaml_generator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Trym Skaar
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-05-02 00:00:00 +02:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Generates i18n yaml locale files for models and resources/views
|
18
|
+
email:
|
19
|
+
- ts@skalar.no
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
26
|
+
files:
|
27
|
+
- .gitignore
|
28
|
+
- Gemfile
|
29
|
+
- README.md
|
30
|
+
- Rakefile
|
31
|
+
- i18n_yaml_generator.gemspec
|
32
|
+
- lib/generators/i18n_yaml/model/model_generator.rb
|
33
|
+
- lib/generators/i18n_yaml/scaffold_controller/scaffold_controller_generator.rb
|
34
|
+
- lib/i18n_yaml_generator.rb
|
35
|
+
- lib/i18n_yaml_generator/helpers.rb
|
36
|
+
- lib/i18n_yaml_generator/version.rb
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: ""
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project: i18n-yaml-generator
|
61
|
+
rubygems_version: 1.6.2
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: Nuff said
|
65
|
+
test_files: []
|
66
|
+
|