i18n_lazy_lookup_generator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,87 @@
1
+ # I18n lazy lookup generator (for Rails 3)
2
+
3
+ ## WTF lazy lookup?
4
+
5
+ + Rails has a way called "Lazy Lookup" in i18n.
6
+ + it gives us a convenient way to look up the locale inside views.
7
+
8
+ See when you have the following yaml:
9
+
10
+ es:
11
+ books:
12
+ index:
13
+ title: "Foo"
14
+
15
+ You can look up the books.index.title value inside
16
+
17
+ app/views/books/index.html.erb template like this (note the dot):
18
+ <%= t '.title' %>
19
+
20
+ Also, Rails [recommends](http://guides.rubyonrails.org/i18n.html#organization-of-locale-files) to organize of locale files:
21
+
22
+ |-views
23
+ |---books
24
+ |-----es.rb
25
+ |-----en.rb
26
+ |---users
27
+ |-----es.rb
28
+ |-----en.rb
29
+ |---navigation
30
+ |-----es.rb
31
+ |-----en.rb
32
+
33
+ ## Usage
34
+
35
+ `i18n_lazy_lookup:generate` generates files with controller name
36
+
37
+ $ rails g i18n_lazy_lookup:generate
38
+ create config/locales/views
39
+ create config/locales/views/comments
40
+ create config/locales/views/comments/en.yml
41
+ create config/locales/views/comments/ja.yml
42
+ create config/locales/views/posts
43
+ create config/locales/views/posts/en.yml
44
+ create config/locales/views/posts/ja.yml
45
+
46
+ If file exists, it leaves.
47
+
48
+ $ r g i18n_lazy_lookup:generate
49
+ exist config/locales/views
50
+ exist config/locales/views/comments
51
+ identical config/locales/views/comments/en.yml
52
+ identical config/locales/views/comments/ja.yml
53
+ exist config/locales/views/posts
54
+ identical config/locales/views/posts/en.yml
55
+ identical config/locales/views/posts/ja.yml
56
+
57
+ You can specify controller and action names with `i18n_lazy_lookup`
58
+
59
+ $ r g i18n_lazy_lookup foo index show
60
+ exist config/locales/views
61
+ create config/locales/views/foo
62
+ create config/locales/views/foo/en.yml
63
+ create config/locales/views/foo/ja.yml
64
+
65
+ YAML will be like as below `config/locales/views/foo/ja.yml`
66
+
67
+ ja:
68
+ foo:
69
+ index:
70
+ show:
71
+
72
+ The default locale is `en`, but you can overide it in `config/application.rb`
73
+
74
+ # Customize generators
75
+ config.generators do |g|
76
+ g.locales %w(en ja)
77
+ end
78
+
79
+ ## TODO
80
+
81
+ + Create controller and scaffold hooks.
82
+ + Add spec. also fixing code as well.
83
+ + Create inheritance file. (eg: default)
84
+
85
+ ## License
86
+
87
+ i18n_lazy_lookup_generator is released under the MIT license.
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/i18n_lazy_lookup_generator/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Kohei Hasegawa"]
6
+ gem.email = ["ameutau@gmail.com"]
7
+ gem.summary = %q{Generates I18n lazy lookup locale files for Rails 3}
8
+ gem.description = %q{A Rails generator plugin & gem that generates Rails I18n lazy lookup locale files.}
9
+ gem.homepage = 'https://github.com/banyan/i18n_lazy_lookup_generator'
10
+
11
+ gem.rubyforge_project = 'i18n_lazy_lookup_generator'
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "i18n_lazy_lookup_generator"
16
+ gem.require_paths = ["lib"]
17
+ gem.licenses = ['MIT']
18
+ gem.version = I18nLazyLookupGenerator::VERSION
19
+
20
+ gem.add_development_dependency "rspec", "~> 2.9"
21
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate i18n_lazy_lookup Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,53 @@
1
+ require 'find'
2
+
3
+ module I18nLazyLookup
4
+ module Generators
5
+ class GenerateGenerator < ::Rails::Generators::Base
6
+ source_root File.expand_path('../../templates', __FILE__)
7
+ class_option :locales, default: %w(en)
8
+ class_option :exclude_patterns, default: %w(layouts shared).join('|')
9
+
10
+ def manifest
11
+ empty_directory File.join(view_path)
12
+
13
+ controller_action_map = {}
14
+ Find.find(File.join('app/views')) do |path|
15
+ if path =~ /app\/views\/(.*)\/(.*)\.html\.[erb|haml]/
16
+ controller, action = $1, $2 # controller include namaspace as well
17
+ next if action =~ /^_/
18
+ next if controller =~ /#{options[:exclude_patterns]}/
19
+ if controller_action_map.key?(controller)
20
+ controller_action_map[controller] << action
21
+ else
22
+ controller_action_map[controller] = [action]
23
+ end
24
+ end
25
+ end
26
+
27
+ controller_action_map.each do |controller, actions|
28
+ if controller =~ /\//
29
+ namespace, controller = controller.split('/')
30
+ empty_directory File.join(view_path, namespace, controller)
31
+ else
32
+ empty_directory File.join(view_path, controller)
33
+ end
34
+ options[:locales].each do |locale|
35
+ @locale = locale
36
+ @controller = controller
37
+ @actions = actions
38
+ if namespace.nil?
39
+ template 'locale.erb', File.join(view_path, controller, "#{locale}.yml")
40
+ else
41
+ @namespace = namespace
42
+ template 'namespace_locale.erb', File.join(view_path, namespace, controller, "#{locale}.yml")
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ def view_path
49
+ 'config/locales/views'
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,29 @@
1
+ class I18nLazyLookupGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path('../templates', __FILE__)
3
+ argument :actions, type: :array, banner: "action action", require: true
4
+ class_option :locales, default: %w(en)
5
+
6
+ def create_view_files
7
+ create_dirs(controller, actions)
8
+ options[:locales].each do |locale|
9
+ @locale = locale
10
+ @controller = controller
11
+ @actions = actions
12
+ template 'locale.erb', File.join(view_path, controller, "#{locale}.yml")
13
+ end
14
+ end
15
+
16
+ private
17
+ def create_dirs(controller, actions)
18
+ empty_directory File.join(view_path)
19
+ empty_directory File.join(view_path, controller)
20
+ end
21
+
22
+ def view_path
23
+ 'config/locales/views'
24
+ end
25
+
26
+ def controller
27
+ file_name
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ <%= @locale %>:
2
+ <%= @controller %>:
3
+ <%- @actions.each do |action| -%>
4
+ <%= action %>:
5
+ <%- end -%>
@@ -0,0 +1,6 @@
1
+ <%= @locale %>:
2
+ <%= @namespace %>:
3
+ <%= @controller %>:
4
+ <%- @actions.each do |action| -%>
5
+ <%= action %>:
6
+ <%- end -%>
@@ -0,0 +1,3 @@
1
+ module I18nLazyLookupGenerator
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,8 @@
1
+ # Requires supporting files with custom matchers and macros, etc,
2
+ # in ./support/ and its subdirectories.
3
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
4
+
5
+ RSpec.configure do |config|
6
+ # config.mock_with :rr
7
+ config.mock_with :rspec
8
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n_lazy_lookup_generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kohei Hasegawa
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.9'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.9'
30
+ description: A Rails generator plugin & gem that generates Rails I18n lazy lookup
31
+ locale files.
32
+ email:
33
+ - ameutau@gmail.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .gitignore
39
+ - .rspec
40
+ - Gemfile
41
+ - README.md
42
+ - Rakefile
43
+ - i18n_lazy_lookup_generator.gemspec
44
+ - lib/generators/i18n_lazy_lookup/USAGE
45
+ - lib/generators/i18n_lazy_lookup/generate/generate_generator.rb
46
+ - lib/generators/i18n_lazy_lookup/i18n_lazy_lookup_generator.rb
47
+ - lib/generators/i18n_lazy_lookup/templates/locale.erb
48
+ - lib/generators/i18n_lazy_lookup/templates/namespace_locale.erb
49
+ - lib/i18n_lazy_lookup_generator/version.rb
50
+ - spec/spec_helper.rb
51
+ homepage: https://github.com/banyan/i18n_lazy_lookup_generator
52
+ licenses:
53
+ - MIT
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project: i18n_lazy_lookup_generator
72
+ rubygems_version: 1.8.23
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Generates I18n lazy lookup locale files for Rails 3
76
+ test_files:
77
+ - spec/spec_helper.rb