amatsuda-i18n_generators 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -7,39 +7,65 @@ This gem plugin generates a set of locale files for Rails 2.2 i18n feature.
7
7
 
8
8
  == FEATURES
9
9
 
10
- This gem/plugin provides following three script/generate commands.
10
+ This gem/plugin provides following four script/generate commands.
11
11
 
12
- === 1. Generate Locale Files for ActiveRecord/ActiveSupport/ActionPack
12
+ === 1. Generate I18n Scaffold
13
13
 
14
- % ./script/generate i18n_locales ja (de-AT, pt-BR, etc.)
14
+ % ./script/generate i18n_scaffold ja (de-AT, pt-BR, etc.)
15
15
 
16
- This will generate following locale files.
16
+ Generates I18n aware scaffold.
17
17
 
18
- config/initializers/i18n_config.rb
19
- config/locales/action_view_ja.yml
20
- config/locales/active_record_ja.yml
21
- config/locales/active_support_ja.yml
18
+ === 2. Generate Locale Files for ActiveRecord/ActiveSupport/ActionPack
22
19
 
23
- Basically, all the generated .yml files in config/locale directory are copied from en-US.yml files bundled inside Rails framework, and each attribute value is one-to-one localized/translated into the specified locale/language.
24
- Also, the generated config file adds the generated .yml files to the I18n load_path, and sets the application default locale to the specified locale.
20
+ % ./script/generate i18n_locale ja (de-AT, pt-BR, etc.)
25
21
 
26
- === 2. Generate Models/Attributes Translation Yaml File For All Models
22
+ or
27
23
 
28
- % ./script/generate i18n_models ja (de-AT, pt-BR, etc.)
24
+ % ./script/generate i18n --locale ja (de-AT, pt-BR, etc.)
25
+
26
+ * A. If The Locale File Exists In Rails-I18n Repository
27
+
28
+ Firstly, the generator checks here.
29
+ http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale
30
+ If the spacified .yml file exists in the repository, the generator downloads and puts the file into your config/locales directory.
31
+ Then the generater sets the application default locale to the specified locale.
32
+
33
+ This will generate following locale file.
34
+
35
+ config/locales/ja.yml
36
+
37
+ * B. Else
38
+
39
+ The generator copies the en.yml file (bundled inside Rails framework) into config/locales directory, and one-to-one localizes/translates each attribute value into the specified locale/language.
40
+ Then the generater sets the application default locale to the specified locale.
41
+
42
+ This will generate following locale file.
43
+
44
+ config/locales/ja.yml
45
+
46
+ === 3. Generate Translation YAML File For All Models/Attributes and views/**/*.erb
47
+
48
+ % ./script/generate i18n_translation ja (de-AT, pt-BR, etc.)
49
+
50
+ or
51
+
52
+ % ./script/generate i18n --translation ja (de-AT, pt-BR, etc.)
29
53
 
30
54
  This will generate following YAML file.
31
55
 
32
- config/locales/models_ja.yml
56
+ config/locales/translation_ja.yml
33
57
 
34
- This file contains definitions of all the AR model names and attributes in your app/models directory, so that you don't have to write the YAML skeleton manually or by copy/paste.
58
+ The generator scans your app/models directory, and generates a YAML file with all the AR model names and attributes so that you don't have to write the YAML skeleton manually or by copy/paste.
59
+ In addition, the generator scans all the *.erb files in your app/views/** directory, and picks all the keys for I18n.translate, then adds them to the YAML file.
35
60
  In addition, the generator tries to translate each of them into the specified language.
36
61
  The generator doesn't overwrite the existing value so that you can rerun the generator again and again just like Annotate Model plugin.
37
62
 
38
- === 3. Generate All
63
+ === 4. Generate All
39
64
 
40
65
  % ./script/generate i18n ja (de-AT, pt-BR, etc.)
41
66
 
42
- Generates all the i18n_locales and i18n_models files at once.
67
+ Executes 2 and 3 at once.
68
+
43
69
 
44
70
  == REQUIREMENTS:
45
71
 
data/Rakefile CHANGED
@@ -5,18 +5,19 @@ require 'rake/rdoctask'
5
5
  desc 'Default: run unit tests.'
6
6
  task :default => :test
7
7
 
8
- desc 'Test the i18n_generator plugin.'
8
+ desc 'Test the i18n_generators plugin.'
9
9
  Rake::TestTask.new(:test) do |t|
10
10
  t.libs << 'lib'
11
11
  t.pattern = 'test/**/*_test.rb'
12
12
  t.verbose = true
13
13
  end
14
14
 
15
- desc 'Generate documentation for the i18n_generator plugin.'
15
+ desc 'Generate documentation for the i18n_generators plugin.'
16
16
  Rake::RDocTask.new(:rdoc) do |rdoc|
17
17
  rdoc.rdoc_dir = 'rdoc'
18
- rdoc.title = 'I18nGenerator'
18
+ rdoc.title = 'I18nGenerators'
19
19
  rdoc.options << '--line-numbers' << '--inline-source'
20
- rdoc.rdoc_files.include('README')
21
- rdoc.rdoc_files.include('lib/**/*.rb')
20
+ rdoc.rdoc_files.include('README.rdoc')
21
+ rdoc.rdoc_files.include('generators/**/*.rb')
22
22
  end
23
+
@@ -4,9 +4,15 @@ require 'rails_generator/commands'
4
4
  require 'gettext'
5
5
 
6
6
  class I18nGenerator < Rails::Generator::NamedBase
7
- attr_reader :locale_name, :cldr, :translator, :generate_models_only, :generate_locales_only
7
+ attr_reader :locale_name, :cldr, :translator, :generate_translation_only, :generate_locale_only
8
8
 
9
9
  def initialize(runtime_args, runtime_options = {})
10
+ if options[:scaffold]
11
+ #TODO invoke scaffold generator
12
+ puts 'please use generate i18n_scaffold command'
13
+ exit
14
+ end
15
+
10
16
  super
11
17
  unless name =~ /^[a-zA-Z]{2}([-_][a-zA-Z]{2})?$/
12
18
  puts 'ERROR: Wrong locale format. Please input in ?? or ??-?? format.'
@@ -16,10 +22,10 @@ class I18nGenerator < Rails::Generator::NamedBase
16
22
  GetText.bindtextdomain 'rails'
17
23
  GetText.locale = @locale_name
18
24
 
19
- unless self.generate_models_only
25
+ unless options[:generate_translation_only]
20
26
  @cldr = CldrDocument.new @locale_name
21
27
  end
22
- unless self.generate_locales_only
28
+ unless options[:generate_locale_only]
23
29
  lang = @locale_name.sub(/-.*$/, '')
24
30
  @translator = Translator.new lang
25
31
  end
@@ -28,7 +34,7 @@ class I18nGenerator < Rails::Generator::NamedBase
28
34
  def manifest
29
35
  record do |m|
30
36
  m.directory 'config/locales'
31
- unless self.generate_models_only
37
+ unless options[:generate_translation_only]
32
38
  m.generate_configuration
33
39
  if defined_in_rails_i18n_repository?
34
40
  m.fetch_from_rails_i18n_repository
@@ -38,12 +44,22 @@ class I18nGenerator < Rails::Generator::NamedBase
38
44
  m.action_view_yaml
39
45
  end
40
46
  end
41
- unless self.generate_locales_only
42
- m.models_yaml
47
+ unless options[:generate_locale_only]
48
+ m.translation_yaml
43
49
  end
44
50
  end
45
51
  end
46
52
 
53
+ protected
54
+ def add_options!(opt)
55
+ opt.separator ''
56
+ opt.separator 'Options:'
57
+ opt.on('--translation',
58
+ 'Generate translations for all models with their attributes and all translation keys in the view files.') {|v| options[:generate_translation_only] = v}
59
+ opt.on('--locale',
60
+ 'Generate locale files.') {|v| options[:generate_locale_only] = v}
61
+ end
62
+
47
63
  private
48
64
  def defined_in_rails_i18n_repository?
49
65
  begin
@@ -54,7 +70,7 @@ class I18nGenerator < Rails::Generator::NamedBase
54
70
  end
55
71
  end
56
72
 
57
- require File.join(File.dirname(__FILE__), '../i18n_locales/i18n_locales_command')
58
- require File.join(File.dirname(__FILE__), '../i18n_models/i18n_models_command')
73
+ require File.join(File.dirname(__FILE__), '../i18n_locale/i18n_locale_command')
74
+ require File.join(File.dirname(__FILE__), '../i18n_translation/i18n_translation_command')
59
75
  Rails::Generator::Commands::Create.send :include, I18nGenerator::Generator::Commands::Create
60
76
 
@@ -13,4 +13,7 @@
13
13
  <% end -%>
14
14
  <% end -%>
15
15
  <% end -%>
16
+ <% translations.each do |key, value| -%>
17
+ <%= "#{key}: #{value}" %>
18
+ <% end -%>
16
19
 
File without changes
@@ -4,7 +4,7 @@ require 'rails_generator/commands'
4
4
  require 'gettext'
5
5
  require File.join(File.dirname(__FILE__), 'lib/yaml')
6
6
  require File.join(File.dirname(__FILE__), 'lib/cldr')
7
- include I18nLocalesGeneratorModule
7
+ include I18nLocaleGeneratorModule
8
8
 
9
9
  module I18nGenerator::Generator
10
10
  module Commands #:nodoc:
@@ -104,7 +104,7 @@ module I18nGenerator::Generator
104
104
  original_yml = I18n.load_path.detect {|lp| lp =~ /\/lib\/#{filename_base}\/locale\/(en|en-US)\.yml$/}
105
105
  doc = YamlDocument.new(original_yml, locale_name)
106
106
  yield doc
107
- file('i18n:base.yml', "config/locales/#{filename_base}_#{locale_name}.yml") do |f|
107
+ file('i18n:base.yml', "config/locale/#{filename_base}_#{locale_name}.yml") do |f|
108
108
  doc.to_s
109
109
  end
110
110
  end
@@ -1,9 +1,9 @@
1
1
  require File.join(File.dirname(__FILE__), '../i18n/i18n_generator')
2
2
 
3
- class I18nModelsGenerator < I18nGenerator
3
+ class I18nLocaleGenerator < I18nGenerator
4
4
  def initialize(runtime_args, runtime_options = {})
5
- @generate_models_only = true
6
5
  super
6
+ options[:generate_locale_only] = true
7
7
  end
8
8
  end
9
9
 
@@ -2,7 +2,7 @@ $KCODE = 'U'
2
2
 
3
3
  require 'open-uri'
4
4
 
5
- module I18nLocalesGeneratorModule
5
+ module I18nLocaleGeneratorModule
6
6
  class CldrDocument
7
7
  def initialize(locale_name)
8
8
  @locale_name = locale_name
@@ -1,6 +1,6 @@
1
1
  require 'yaml'
2
2
 
3
- module I18nLocalesGeneratorModule
3
+ module I18nLocaleGeneratorModule
4
4
  class YamlDocument
5
5
  def initialize(yml_path, locale_name)
6
6
  @locale_name, @nodes = locale_name, []
File without changes
@@ -1,12 +1,14 @@
1
1
  require 'rails_generator'
2
2
  require 'rails_generator/commands'
3
3
  require File.join(File.dirname(__FILE__), 'lib/translator')
4
- include I18nModelsGeneratorModule
4
+ require File.join(File.dirname(__FILE__), 'lib/recording_backend')
5
+ require File.join(File.dirname(__FILE__), 'lib/erb_executer')
6
+ include I18nTranslationGeneratorModule
5
7
 
6
8
  module I18nGenerator::Generator
7
9
  module Commands #:nodoc:
8
10
  module Create
9
- def models_yaml
11
+ def translation_yaml
10
12
  I18n.locale = locale_name
11
13
  models = model_filenames.map do |model_name|
12
14
  model = begin
@@ -38,7 +40,16 @@ END
38
40
  end
39
41
  model
40
42
  end.compact
41
- generate_yaml(locale_name, models)
43
+ # pick all translated keywords from view files
44
+ I18n.backend = RecordingBackend.new
45
+
46
+ Dir["#{RAILS_ROOT}/app/views/**/*.erb"].each do |f|
47
+ ErbExecuter.new.exec_erb f
48
+ end
49
+ keys_in_view = I18n.backend.keys
50
+ keys_in_view -= models.map {|m| m.english_name.to_sym}
51
+ keys_in_view -= models.inject([]) {|a, m| a + m.content_columns.map {|c| "#{m.english_name}.#{c.name}".to_sym}}
52
+ generate_yaml(locale_name, models, keys_in_view.inject({}) {|h, k| h[k] = translator.translate(k); h})
42
53
  end
43
54
 
44
55
  private
@@ -48,8 +59,8 @@ END
48
59
  end
49
60
  end
50
61
 
51
- def generate_yaml(locale_name, models)
52
- template 'i18n:models.yml', "config/locales/models_#{locale_name}.yml", :assigns => {:locale_name => locale_name, :models => models}
62
+ def generate_yaml(locale_name, models, translations)
63
+ template 'i18n:translation.yml', "config/locales/translation_#{locale_name}.yml", :assigns => {:locale_name => locale_name, :models => models, :translations => translations}
53
64
  end
54
65
  end
55
66
  end
@@ -1,9 +1,9 @@
1
1
  require File.join(File.dirname(__FILE__), '../i18n/i18n_generator')
2
2
 
3
- class I18nLocalesGenerator < I18nGenerator
3
+ class I18nTranslationGenerator < I18nGenerator
4
4
  def initialize(runtime_args, runtime_options = {})
5
- @generate_locales_only = true
6
5
  super
6
+ options[:generate_translation_only] = true
7
7
  end
8
8
  end
9
9
 
@@ -0,0 +1,30 @@
1
+ require "#{File.dirname(__FILE__)}/through_ryoku"
2
+
3
+ module I18nTranslationGeneratorModule
4
+ class ErbExecuter
5
+ def exec_erb(filename)
6
+ begin
7
+ # ERB.new(File.read(f)).result
8
+ (m = Module.new).module_eval <<-EOS
9
+ class Executer
10
+ extend ERB::DefMethod
11
+ include ActionView::Helpers::TranslationHelper
12
+ include ThroughRyoku
13
+ nil.class_eval do
14
+ def method_missing(method, *args, &block); nil; end
15
+ end
16
+ def_erb_method 'execute', '#{filename}'
17
+ end
18
+ EOS
19
+ m.const_get('Executer').new.execute { }
20
+ nil.class_eval do
21
+ undef :method_missing
22
+ end
23
+ rescue => e
24
+ p e
25
+ # do nothing
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,15 @@
1
+ module I18nTranslationGeneratorModule
2
+ class RecordingBackend
3
+ attr_reader :keys
4
+
5
+ def initialize
6
+ @keys = []
7
+ end
8
+
9
+ def translate(locale, key, options = {})
10
+ @keys << key.to_sym
11
+ end
12
+ alias :t :translate
13
+ end
14
+ end
15
+
@@ -0,0 +1,6 @@
1
+ module ThroughRyoku
2
+ def method_missing(method, *args, &block)
3
+ nil
4
+ end
5
+ end
6
+
@@ -1,6 +1,6 @@
1
1
  require 'open-uri'
2
2
 
3
- module I18nModelsGeneratorModule
3
+ module I18nTranslationGeneratorModule
4
4
  class Translator
5
5
  def initialize(lang)
6
6
  @lang, @cache = lang, {}
data/spec/cldr_spec.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  $KCODE = 'U'
2
2
 
3
- require File.join(File.dirname(__FILE__), '../generators/i18n_locales/lib/cldr')
4
- include I18nLocalesGeneratorModule
3
+ require File.join(File.dirname(__FILE__), '../generators/i18n_locale/lib/cldr')
4
+ include I18nLocaleGeneratorModule
5
5
 
6
6
  describe CldrDocument do
7
7
  before do
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '/../generators/i18n_locales/i18n_locales_command')
1
+ require File.join(File.dirname(__FILE__), '/../generators/i18n_locale/i18n_locale_command')
2
2
 
3
3
  describe I18nGenerator::Generator::Commands::Create do
4
4
  before do
@@ -1,7 +1,7 @@
1
1
  $KCODE = 'U'
2
2
 
3
- require File.join(File.dirname(__FILE__), '../generators/i18n_models/lib/translator')
4
- include I18nModelsGeneratorModule
3
+ require File.join(File.dirname(__FILE__), '../generators/i18n_translation/lib/translator')
4
+ include I18nTranslationGeneratorModule
5
5
 
6
6
  describe Translator do
7
7
  before(:each) do
data/spec/yaml_spec.rb CHANGED
@@ -1,5 +1,5 @@
1
- require File.join(File.dirname(__FILE__), '/../generators/i18n_locales/lib/yaml')
2
- include I18nLocalesGeneratorModule
1
+ require File.join(File.dirname(__FILE__), '/../generators/i18n_locale/lib/yaml')
2
+ include I18nLocaleGeneratorModule
3
3
 
4
4
  describe 'Yaml' do
5
5
  before do
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.2.0
4
+ version: 0.3.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-29 00:00:00 -08:00
12
+ date: 2008-12-07 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -35,15 +35,14 @@ files:
35
35
  - Rakefile
36
36
  - generators/i18n/USAGE
37
37
  - generators/i18n/i18n_generator.rb
38
- - generators/i18n_locales/USAGE
39
- - generators/i18n_locales/i18n_locales_command.rb
40
- - generators/i18n_locales/i18n_locales_generator.rb
41
- - generators/i18n_locales/lib/cldr.rb
42
- - generators/i18n_locales/lib/yaml.rb
43
- - generators/i18n_models/USAGE
44
- - generators/i18n_models/i18n_models_command.rb
45
- - generators/i18n_models/i18n_models_generator.rb
46
- - generators/i18n_models/lib/translator.rb
38
+ - generators/i18n/templates/base.yml
39
+ - generators/i18n/templates/i18n_config.rb
40
+ - generators/i18n/templates/translation.yml
41
+ - generators/i18n_locale/USAGE
42
+ - generators/i18n_locale/i18n_locale_command.rb
43
+ - generators/i18n_locale/i18n_locale_generator.rb
44
+ - generators/i18n_locale/lib/cldr.rb
45
+ - generators/i18n_locale/lib/yaml.rb
47
46
  - generators/i18n_scaffold/i18n_scaffold_generator.rb
48
47
  - generators/i18n_scaffold/templates/controller.rb
49
48
  - generators/i18n_scaffold/templates/functional_test.rb
@@ -55,13 +54,17 @@ files:
55
54
  - generators/i18n_scaffold/templates/view_index.html.erb
56
55
  - generators/i18n_scaffold/templates/view_new.html.erb
57
56
  - generators/i18n_scaffold/templates/view_show.html.erb
58
- - generators/i18n/templates/base.yml
59
- - generators/i18n/templates/i18n_config.rb
60
- - generators/i18n/templates/models.yml
57
+ - generators/i18n_translation/USAGE
58
+ - generators/i18n_translation/i18n_translation_command.rb
59
+ - generators/i18n_translation/i18n_translation_generator.rb
60
+ - generators/i18n_translation/lib/erb_executer.rb
61
+ - generators/i18n_translation/lib/recording_backend.rb
62
+ - generators/i18n_translation/lib/through_ryoku.rb
63
+ - generators/i18n_translation/lib/translator.rb
61
64
  - spec/cldr_spec.rb
62
65
  - spec/data/cldr/ja.html
63
66
  - spec/data/yml/active_record/en-US.yml
64
- - spec/i18n_locales_command_spec.rb
67
+ - spec/i18n_locale_command_spec.rb
65
68
  - spec/translator_spec.rb
66
69
  - spec/yaml_spec.rb
67
70
  has_rdoc: false