amatsuda-i18n_generators 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -11,25 +11,25 @@ This gem/plugin provides following three script/generate commands.
11
11
 
12
12
  === 1. Generate Locale Files for ActiveRecord/ActiveSupport/ActionPack
13
13
 
14
- % ./script/generate i18n_locales ja-JP (de-AT, pt-BR, etc.)
14
+ % ./script/generate i18n_locales ja (de-AT, pt-BR, etc.)
15
15
 
16
16
  This will generate following locale files.
17
17
 
18
18
  config/initializers/i18n_config.rb
19
- lib/locale/action_view_ja-JP.yml
20
- lib/locale/active_record_ja-JP.yml
21
- lib/locale/active_support_ja-JP.yml
19
+ config/locales/action_view_ja.yml
20
+ config/locales/active_record_ja.yml
21
+ config/locales/active_support_ja.yml
22
22
 
23
- Basically, all the generated .yml files in lib/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.
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
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.
25
25
 
26
26
  === 2. Generate Models/Attributes Translation Yaml File For All Models
27
27
 
28
- % ./script/generate i18n_models ja-JP (de-AT, pt-BR, etc.)
28
+ % ./script/generate i18n_models ja (de-AT, pt-BR, etc.)
29
29
 
30
30
  This will generate following YAML file.
31
31
 
32
- lib/locale/models_ja-JP.yml
32
+ config/locales/models_ja.yml
33
33
 
34
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.
35
35
  In addition, the generator tries to translate each of them into the specified language.
@@ -37,9 +37,9 @@ The generator doesn't overwrite the existing value so that you can rerun the gen
37
37
 
38
38
  === 3. Generate All
39
39
 
40
- % ./script/generate i18n ja-JP (de-AT, pt-BR, etc.)
40
+ % ./script/generate i18n ja (de-AT, pt-BR, etc.)
41
41
 
42
- Generates all the i18n_locales and i18N_models files at once.
42
+ Generates all the i18n_locales and i18n_models files at once.
43
43
 
44
44
  == REQUIREMENTS:
45
45
 
@@ -1,6 +1,6 @@
1
+ require 'rubygems'
1
2
  require 'rails_generator'
2
3
  require 'rails_generator/commands'
3
- require 'rubygems'
4
4
  require 'gettext'
5
5
 
6
6
  class I18nGenerator < Rails::Generator::NamedBase
@@ -8,11 +8,11 @@ class I18nGenerator < Rails::Generator::NamedBase
8
8
 
9
9
  def initialize(runtime_args, runtime_options = {})
10
10
  super
11
- unless name =~ /[a-zA-Z]{2}[-_][a-zA-Z]{2}/
12
- puts 'ERROR: Wrong locale format. Please input in ??-?? format.'
11
+ unless name =~ /^[a-zA-Z]{2}([-_][a-zA-Z]{2})?$/
12
+ puts 'ERROR: Wrong locale format. Please input in ?? or ??-?? format.'
13
13
  exit
14
14
  end
15
- @locale_name = "#{name[0..1].downcase}-#{name[3..4].upcase}"
15
+ @locale_name = name.length == 5 ? "#{name[0..1].downcase}-#{name[3..4].upcase}" : "#{name[0..1].downcase}"
16
16
  GetText.bindtextdomain 'rails'
17
17
  GetText.locale = @locale_name
18
18
 
@@ -27,9 +27,9 @@ class I18nGenerator < Rails::Generator::NamedBase
27
27
 
28
28
  def manifest
29
29
  record do |m|
30
- m.directory 'lib/locale'
30
+ m.directory 'config/locales'
31
31
  unless self.generate_models_only
32
- m.template 'i18n:i18n_config.rb', 'config/initializers/i18n_config.rb', :assigns => {:locale_name => @locale_name}
32
+ m.generate_configuration
33
33
  m.active_support_yaml
34
34
  m.active_record_yaml
35
35
  m.action_view_yaml
@@ -1,6 +1,6 @@
1
+ require 'rubygems'
1
2
  require 'rails_generator'
2
3
  require 'rails_generator/commands'
3
- require 'rubygems'
4
4
  require 'gettext'
5
5
  require File.join(File.dirname(__FILE__), 'lib/yaml')
6
6
  require File.join(File.dirname(__FILE__), 'lib/cldr')
@@ -9,6 +9,18 @@ include I18nLocalesGeneratorModule
9
9
  module I18nGenerator::Generator
10
10
  module Commands #:nodoc:
11
11
  module Create
12
+ def generate_configuration
13
+ if Rails.configuration.respond_to? :i18n # Edge
14
+ # edit environment.rb file
15
+ environment = add_locale_config File.read(File.join(Rails.root, 'config/environment.rb'))
16
+ File.open File.join(Rails.root, 'config/environment.rb'), 'w' do |f|
17
+ f.puts environment
18
+ end
19
+ else
20
+ m.template 'i18n:i18n_config.rb', 'config/initializers/i18n_config.rb', :assigns => {:locale_name => locale_name}
21
+ end
22
+ end
23
+
12
24
  def active_support_yaml
13
25
  open_yaml('active_support') do |yaml|
14
26
  yaml[locale_name].descendant_nodes do |node|
@@ -62,11 +74,29 @@ module I18nGenerator::Generator
62
74
  end
63
75
 
64
76
  private
77
+ def add_locale_config(environment_contents)
78
+ (arr = environment_contents.split("\n")).each_with_index do |l, i|
79
+ if l =~ /^\s*config\.i18n\.default_locale = /
80
+ arr[i] = " config.i18n.default_locale = '#{locale_name}'"
81
+ return arr.join("\n")
82
+ end
83
+ end
84
+ arr.each_with_index do |l, i|
85
+ if l =~ /^\s*#?\s*config\.i18n\.default_locale = /
86
+ arr[i] = " config.i18n.default_locale = '#{locale_name}'"
87
+ return arr.join("\n")
88
+ end
89
+ end
90
+ # hope you're all using Ruby >= 1.8.7...
91
+ end_row = arr.respond_to?(:rindex) ? arr.rindex {|l| l =~ /^\s*end\s*/} : arr.size - 1
92
+ ((arr[0...end_row] << " config.i18n.default_locale = '#{locale_name}'") + arr[end_row..-1]).join("\n")
93
+ end
94
+
65
95
  def open_yaml(filename_base)
66
- original_yml = I18n.load_path.detect {|lp| lp =~ /\/lib\/#{filename_base}\/locale\/en-US\.yml$/}
96
+ original_yml = I18n.load_path.detect {|lp| lp =~ /\/lib\/#{filename_base}\/locale\/(en|en-US)\.yml$/}
67
97
  doc = YamlDocument.new(original_yml, locale_name)
68
98
  yield doc
69
- file('i18n:base.yml', "lib/locale/#{filename_base}_#{locale_name}.yml") do |f|
99
+ file('i18n:base.yml', "config/locales/#{filename_base}_#{locale_name}.yml") do |f|
70
100
  doc.to_s
71
101
  end
72
102
  end
@@ -6,7 +6,7 @@ module I18nLocalesGeneratorModule
6
6
  @locale_name, @nodes = locale_name, []
7
7
  File.read(yml_path).each_with_index do |line_text, i|
8
8
  n = Node.new(self, i, line_text.chomp)
9
- @nodes << (n.key == 'en-US' ? Node.new(self, i, "#{locale_name}:") : n)
9
+ @nodes << (((n.key == 'en-US') || (n.key == 'en')) ? Node.new(self, i, "#{locale_name}:") : n)
10
10
  end
11
11
  end
12
12
 
@@ -49,7 +49,7 @@ END
49
49
  end
50
50
 
51
51
  def generate_yaml(locale_name, models)
52
- template 'i18n:models.yml', "lib/locale/models_#{locale_name}.yml", :assigns => {:locale_name => locale_name, :models => models}
52
+ template 'i18n:models.yml', "config/locales/models_#{locale_name}.yml", :assigns => {:locale_name => locale_name, :models => models}
53
53
  end
54
54
  end
55
55
  end
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.0.7
4
+ version: 0.0.8
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-17 00:00:00 -08:00
12
+ date: 2008-11-21 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency