i18n-generators 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/.gitignore +2 -0
  2. data/Gemfile +3 -0
  3. data/History.txt +143 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.rdoc +74 -0
  6. data/Rakefile +2 -0
  7. data/generators/i18n/USAGE +11 -0
  8. data/generators/i18n/i18n_generator.rb +88 -0
  9. data/generators/i18n/lib/yaml.rb +187 -0
  10. data/generators/i18n/templates/base.yml +0 -0
  11. data/generators/i18n/templates/i18n_config.rb +4 -0
  12. data/generators/i18n/templates/translation.yml +1 -0
  13. data/generators/i18n_locale/USAGE +10 -0
  14. data/generators/i18n_locale/i18n_locale_command.rb +141 -0
  15. data/generators/i18n_locale/i18n_locale_generator.rb +8 -0
  16. data/generators/i18n_locale/lib/cldr.rb +138 -0
  17. data/generators/i18n_scaffold/i18n_scaffold_generator.rb +2 -0
  18. data/generators/i18n_scaffold/templates/controller.rb +85 -0
  19. data/generators/i18n_scaffold/templates/functional_test.rb +45 -0
  20. data/generators/i18n_scaffold/templates/helper.rb +2 -0
  21. data/generators/i18n_scaffold/templates/helper_test.rb +4 -0
  22. data/generators/i18n_scaffold/templates/layout.html.erb +17 -0
  23. data/generators/i18n_scaffold/templates/style.css +54 -0
  24. data/generators/i18n_scaffold/templates/view_edit.html.erb +18 -0
  25. data/generators/i18n_scaffold/templates/view_index.html.erb +24 -0
  26. data/generators/i18n_scaffold/templates/view_new.html.erb +17 -0
  27. data/generators/i18n_scaffold/templates/view_show.html.erb +10 -0
  28. data/generators/i18n_translation/USAGE +8 -0
  29. data/generators/i18n_translation/i18n_translation_command.rb +124 -0
  30. data/generators/i18n_translation/i18n_translation_generator.rb +8 -0
  31. data/generators/i18n_translation/lib/erb_executer.rb +30 -0
  32. data/generators/i18n_translation/lib/recording_backend.rb +15 -0
  33. data/generators/i18n_translation/lib/through_ryoku.rb +7 -0
  34. data/generators/i18n_translation/lib/translator.rb +27 -0
  35. data/i18n-generators.gemspec +26 -0
  36. data/lib/generators/i18n/all/USAGE +12 -0
  37. data/lib/generators/i18n/all/i18n_generator.rb +19 -0
  38. data/lib/generators/i18n/locale/USAGE +10 -0
  39. data/lib/generators/i18n/locale/locale_generator.rb +49 -0
  40. data/lib/generators/i18n/translation/USAGE +8 -0
  41. data/lib/generators/i18n/translation/lib/translator.rb +59 -0
  42. data/lib/generators/i18n/translation/lib/yaml.rb +137 -0
  43. data/lib/generators/i18n/translation/lib/yaml_waml.rb +35 -0
  44. data/lib/generators/i18n/translation/translation_generator.rb +108 -0
  45. data/lib/i18n_generators/version.rb +3 -0
  46. data/spec/cldr_spec.rb +54 -0
  47. data/spec/data/cldr/ja.html +3112 -0
  48. data/spec/data/yml/active_record/en-US.yml +54 -0
  49. data/spec/i18n_locale_command_spec.rb +63 -0
  50. data/spec/i18n_translation_command_spec.rb +24 -0
  51. data/spec/spec_helper.rb +10 -0
  52. data/spec/translator_spec.rb +46 -0
  53. data/spec/yaml_spec.rb +123 -0
  54. metadata +122 -0
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ pkg
2
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
data/History.txt ADDED
@@ -0,0 +1,143 @@
1
+ == 1.1.1
2
+
3
+ * enchancements:
4
+ * Add scoped generator instead separate ones
5
+
6
+ == 1.1.0
7
+
8
+ * enhancements:
9
+ * New translation engine (see: http://googlecode.blogspot.com/2011/05/spring-cleaning-for-some-of-our-apis.html) (thanks to agilekawabata-san!)
10
+
11
+ == 1.0.3
12
+
13
+ * bugfixes:
14
+ * #4 Translation generator recursive loop in ruby 1.9 + MS-Windows (memarko)
15
+
16
+ * enhancements:
17
+ * Some Rails-3-ish cleanups
18
+
19
+ == 1.0.2
20
+
21
+ * bugfixes:
22
+ * Use https to request GH
23
+
24
+ == 1.0.1
25
+
26
+ * major enhancements:
27
+ * Bundle yaml_waml.rb to avoid depending on yaml_waml gem (thanks to kakutani-san!)
28
+
29
+ == 1.0.0
30
+
31
+ * major enhancements:
32
+ * Rails 3
33
+ * enhancements:
34
+ * Fixing deprectaed syntax {{error}} with %{error} for i18n_scaffold (moro)
35
+
36
+ == 0.8.1
37
+
38
+ * bugfixes:
39
+ * Fix for pluralized translations (scambra)
40
+
41
+ == 0.8.0
42
+
43
+ * major enhancements:
44
+ * Gemify
45
+
46
+ == 0.7.0
47
+
48
+ * major enhancements:
49
+ * Fix gettext dependency version
50
+ * enhancements:
51
+ * Add more error checks when loading models
52
+ * Load gettext only when using it
53
+
54
+ == 0.6.0
55
+
56
+ * bugfixes:
57
+ * Could not load cldr data with short locale names
58
+ * major enhancements:
59
+ * Include AR association names in model attributes translation
60
+ * Add 'generated' comment mark (#g) for generated lines in YAML (thanks to t-wada-senpai, Shimura-san, Toyofuku-san, Sezutsu-san and oukayuka-san!)
61
+ * Add --include-timestamps option and change default bahaviour not to include timestamp columns in translation YAML
62
+ * Add blank line between each blocks (thanks to Sawayanagi-san!)
63
+ * enhancements:
64
+ * Avoid generating blank locale file
65
+ * Change GitHub fetch URL from blob/master/...?raw=true to raw/master
66
+
67
+ == 0.5.0
68
+
69
+ * bugfixes:
70
+ * Could not fetch the locale files via GitHub (GitHub changed the URL?)
71
+ * major enhancements:
72
+ * Ruby 1.9 ready!
73
+ * Never connect to CLDR site when the locale file found in Sven's rails-i18n repository
74
+ * enhancements:
75
+ * Use Ruby 1.9 built-in JSON parser if Ruby 1.9
76
+
77
+ == 0.4.1
78
+
79
+ * bugfixes:
80
+ * i18n_translation abnormally ends when loading a view containing "-%>" (thanks to babie-san!)
81
+
82
+ == 0.4.0
83
+
84
+ * major enhancements:
85
+ * translation generator now preserves any existing extra lines in the YAML file (thanks to Iain Hecker-san!)
86
+
87
+ == 0.3.4
88
+
89
+ * enhancements:
90
+ * Reduce unnecessary request to CLDR site
91
+
92
+ == 0.3.3
93
+
94
+ * bugfixes:
95
+ * Did not work on Ruby 1.8.6 (thanks to akm-san!)
96
+
97
+ == 0.3.2
98
+
99
+ * bugfixes:
100
+ * Trim mode in ERB didn't work on some platforms (thanks to valda-san!)
101
+
102
+ == 0.3.1
103
+
104
+ * enhancements:
105
+ * Translate all keys simultaneously using thread
106
+ * Translations with :scope is going to be yamlized with proper hierarchy
107
+
108
+ == 0.3.0
109
+
110
+ * major enhancements:
111
+ * Renamed the generators
112
+ i18n_locales -> i18n_locale (i18n --locale)
113
+ i18n_models -> i18n_translation (i18n --translation)
114
+ * Handle all translate keys which appear in view files (thanks to PanosJee-san!)
115
+
116
+ == 0.2.0
117
+
118
+ * major enhancements:
119
+ * Implemented i18n_scaffold generator (thanks to Iain Hecker-san!)
120
+
121
+ * enhancements:
122
+ * i18n_models generator can now deal with non AR classes which has content_columns (thanks to Iain Hecker-san!)
123
+
124
+ == 0.1.0
125
+
126
+ * major enhancements:
127
+ * Fetch locale YAML file from rails-i18n project repository first
128
+
129
+ * bugfixes:
130
+ * Could not recognize model names with multipie words (thanks to makoto-san!)
131
+
132
+ == 0.0.8
133
+
134
+ * major enhancements:
135
+ * Lots of changes for this commit by DHH http://github.com/rails/rails/commit/d9b92ee11b33fed5c7a94a91415fa846705f7dd3
136
+
137
+ == 0.0.4
138
+
139
+ * Gemified on GitHub
140
+
141
+ == 0.0.1
142
+
143
+ * Birthday!
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008-2011 Akira Matsuda
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,74 @@
1
+ = I18n generators
2
+
3
+ This gem plugin generates a set of locale files for Rails i18n feature.
4
+ Supports Rails 3.1, 3.0.x, 2.3.x and 2.2.x.
5
+
6
+ * http://github.com/amatsuda/i18n_generators
7
+ * http://rubygems.org/gems/i18n_generators
8
+
9
+
10
+ == FEATURES
11
+
12
+ This gem/plugin provides following generate commands.
13
+
14
+ === 1. Generate Locale Files for ActiveRecord/ActiveSupport/ActionPack
15
+
16
+ rails g i18n:locale ja (de-AT, pt-BR, etc.)
17
+
18
+ Downloads the .yml file for the specified locale from the official rails-i18n repository:
19
+ https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale
20
+
21
+ Then the generater sets the application default locale to the specified locale.
22
+
23
+ This will generate following locale file.
24
+
25
+ config/locales/ja.yml
26
+
27
+ === 2. Generate Translation YAML File For All Models/Attributes
28
+
29
+ rails g i18n:translation ja (de-AT, pt-BR, etc.)
30
+
31
+ This will generate following YAML file.
32
+
33
+ config/locales/translation.ja.yml
34
+
35
+ The generator scans your app/models directory, and generates a YAML file
36
+ with all the AR model names and attributes so that you don't have to write
37
+ the YAML skeleton manually or by copy/paste. In addition, the generatortries
38
+ to translate each of them into the specified language. The generator doesn't
39
+ overwrite the existing value so that you can rerun the generator again and again.
40
+
41
+ === 3. Generate All
42
+
43
+ rails g i18n:all ja # (de-AT, pt-BR, etc.)
44
+
45
+ Executes 1 and 2 at once.
46
+
47
+
48
+ == REQUIREMENTS:
49
+
50
+ * Ruby (>= 1.8.7)
51
+ * Ruby on Rails (3.1 or 3.0 or 2.3 or 2.2)
52
+
53
+
54
+ == INSTALL:
55
+
56
+ * As a gem
57
+
58
+ gem install i18n_generators # install orginal version so far
59
+
60
+ * With Bundler
61
+ Add
62
+
63
+ gem 'i18n_generators' # as above
64
+
65
+ to your Gemfile, then `bundle`. Restricting the group to :development would be
66
+ a good idea, since the generator would probably be used only in development mode.
67
+
68
+ * As a Rails plugin
69
+
70
+ rails plugin install git://github.com/hauleth/i18n-generators.git
71
+
72
+
73
+ Copyright (c) 2008 - 2011 Akira Matsuda (fixed by Łukasz Niemier), released
74
+ under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,11 @@
1
+ Description:
2
+ Generates a config file and locale files and a model/attributes translation file for Rails 2.x i18n.
3
+
4
+ Example:
5
+ ./script/generate i18n locale_name (ja, de-AT, etc.)
6
+
7
+ This will create:
8
+ config/locales/ja.yml
9
+ config/locales/translation_ja.yml
10
+ And update:
11
+ config/environment.rb
@@ -0,0 +1,88 @@
1
+ require 'net/https'
2
+ require 'rubygems'
3
+ require 'rails_generator'
4
+ require 'rails_generator/commands'
5
+
6
+ class I18nGenerator < Rails::Generator::NamedBase
7
+ attr_reader :locale_name, :cldr, :translator, :generate_translation_only, :generate_locale_only, :include_timestamps
8
+
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
+
16
+ super
17
+ unless name =~ /^[a-zA-Z]{2}([-_][a-zA-Z]{2})?$/
18
+ puts 'ERROR: Wrong locale format. Please input in ?? or ??-?? format.'
19
+ exit
20
+ end
21
+ @locale_name = name.length == 5 ? "#{name[0..1].downcase}-#{name[3..4].upcase}" : "#{name[0..1].downcase}"
22
+
23
+ unless options[:generate_translation_only]
24
+ gem 'gettext', '<2'
25
+ require 'gettext'
26
+ @cldr = CldrDocument.new @locale_name
27
+ GetText.bindtextdomain 'rails'
28
+ GetText.locale = @locale_name
29
+ end
30
+ unless options[:generate_locale_only]
31
+ lang = @locale_name.sub(/-.*$/, '')
32
+ @translator = Translator.new lang
33
+ end
34
+ @include_timestamps = true if options[:include_timestamps]
35
+ end
36
+
37
+ def manifest
38
+ record do |m|
39
+ m.directory 'config/locales'
40
+ unless options[:generate_translation_only]
41
+ logger.debug 'updating environment.rb ...'
42
+ m.generate_configuration
43
+ if defined_in_rails_i18n_repository?
44
+ logger.debug "fetching #{locale_name}.yml from rails-i18n repository..."
45
+ m.fetch_from_rails_i18n_repository
46
+ else
47
+ logger.debug "generating #{locale_name} YAML files for Rails..."
48
+ m.active_support_yaml
49
+ m.active_record_yaml
50
+ m.action_view_yaml
51
+ end
52
+ end
53
+ unless options[:generate_locale_only]
54
+ m.translation_yaml
55
+ end
56
+ end
57
+ end
58
+
59
+ protected
60
+ def add_options!(opt)
61
+ opt.separator ''
62
+ opt.separator 'Options:'
63
+ opt.on('--translation',
64
+ 'Generate translations for all models with their attributes and all translation keys in the view files.') {|v| options[:generate_translation_only] = v}
65
+ opt.on('--locale', 'Generate locale files.') {|v| options[:generate_locale_only] = v}
66
+ opt.on('--include-timestamps', 'Include timestamp columns in the YAML translation.') {|v| options[:include_timestamps] = v}
67
+ opt.on('--include-timestamp', 'Include timestamp columns in the YAML translation.') {|v| options[:include_timestamps] = v}
68
+ end
69
+
70
+ private
71
+ def defined_in_rails_i18n_repository?
72
+ begin
73
+ uri = URI.parse "https://github.com/svenfuchs/rails-i18n/raw/master/rails/locale/#{locale_name}.yml"
74
+ http = Net::HTTP.new uri.host, uri.port
75
+ http.use_ssl = true
76
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
77
+ req = Net::HTTP::Get.new uri.request_uri
78
+ res = http.request req
79
+ res.code == '200'
80
+ rescue
81
+ false
82
+ end
83
+ end
84
+ end
85
+
86
+ require File.join(File.dirname(__FILE__), '../i18n_locale/i18n_locale_command')
87
+ require File.join(File.dirname(__FILE__), '../i18n_translation/i18n_translation_command')
88
+ Rails::Generator::Commands::Create.send :include, I18nGenerator::Generator::Commands::Create
@@ -0,0 +1,187 @@
1
+ require 'yaml'
2
+
3
+ module I18nLocaleGeneratorModule
4
+ class Node
5
+ attr_reader :document, :indent_level
6
+ attr_accessor :line
7
+
8
+ def initialize(parent, line_index, text)
9
+ @document, @line, @text = parent.document, line_index, text.to_s
10
+ @text =~ /(^\s*)/
11
+ @indent_level = $1.nil? ? 0 : $1.size
12
+ @yaml = YAML.load(@text.to_s + ' ')
13
+ end
14
+
15
+ def parent
16
+ @parent ||= document.parent_of self
17
+ end
18
+
19
+ def children
20
+ @children ||= document.children_of(self)
21
+ end
22
+ alias :nodes :children
23
+
24
+ def [](node_name)
25
+ if node = nodes.detect {|n| n.key.to_s == node_name.to_s}
26
+ node
27
+ else
28
+ nodes.add "#{' ' * (@indent_level + 2)}#{node_name}: "
29
+ nodes.last
30
+ end
31
+ end
32
+
33
+ def key
34
+ @yaml.is_a?(Hash) ? @yaml.keys.first : nil
35
+ end
36
+
37
+ def value
38
+ @yaml.is_a?(Hash) ? @yaml.values.first : nil
39
+ end
40
+
41
+ def value=(val)
42
+ if @yaml[self.key] != val
43
+ @yaml[self.key] = val
44
+ @changed = true
45
+ end
46
+ end
47
+
48
+ def text
49
+ if @changed
50
+ v = if self.value.is_a?(Array)
51
+ "[#{self.value * ', '}]"
52
+ else
53
+ %Q["#{self.value}"]
54
+ end
55
+ "#{' ' * self.indent_level}#{self.key}: #{v} #g" # g is for 'generated'!
56
+ else
57
+ @text
58
+ end
59
+ end
60
+ alias :to_s :text
61
+
62
+ def changed?
63
+ @changed
64
+ end
65
+
66
+ def is_blank_or_comment?
67
+ @text.sub(/#.*$/, '').gsub(/\s/, '').empty?
68
+ end
69
+
70
+ def path
71
+ @path ||= "#{self.parent.path}/#{self.key}"
72
+ end
73
+
74
+ def descendant_nodes(&block)
75
+ yield self if self.value
76
+ self.children.each {|child| child.descendant_nodes(&block)} if self.children
77
+ end
78
+
79
+ def <=>(other)
80
+ self.line <=> other.line
81
+ end
82
+ end
83
+
84
+ class YamlDocument < Node
85
+ attr_accessor :lines
86
+ alias :nodes :lines
87
+
88
+ def initialize(yml_path, locale_name)
89
+ @locale_name, @lines, @current_line, @indent_level = locale_name, Nodes.new(self), -1, -2
90
+ if File.exists? yml_path
91
+ File.open(yml_path) do |file|
92
+ file.each_with_index do |line_text, i|
93
+ n = Node.new(self, i, line_text.chomp)
94
+ @lines << ((((n.key == 'en-US') || (n.key == 'en')) && n.value.blank?) ? Node.new(self, i, "#{locale_name}:") : n)
95
+ end
96
+ @lines.delete_at(-1) if @lines[-1].text.blank?
97
+ end
98
+ end
99
+ end
100
+
101
+ def next
102
+ return false if @lines.size == 0
103
+ @current_line += 1
104
+ return false if @current_line >= @lines.size
105
+ @lines[@current_line].is_blank_or_comment? ? self.next : @lines[@current_line]
106
+ end
107
+
108
+ def prev
109
+ return false if @current_line == 0
110
+ @current_line -= 1
111
+ @lines[@current_line].is_blank_or_comment? ? self.prev : @lines[@current_line]
112
+ end
113
+
114
+ def parent_of(child)
115
+ @current_line = child.line
116
+ while n = self.prev
117
+ return n if n.indent_level == child.indent_level - 2
118
+ end
119
+ self
120
+ end
121
+
122
+ def children_of(parent)
123
+ nodes = Nodes.new(parent)
124
+ @current_line = parent.line
125
+ while n = self.next
126
+ if n.indent_level < parent.indent_level + 2
127
+ break
128
+ elsif n.indent_level == parent.indent_level + 2
129
+ nodes << n
130
+ end
131
+ end
132
+ nodes
133
+ end
134
+
135
+ def document
136
+ self
137
+ end
138
+
139
+ def path
140
+ ''
141
+ end
142
+
143
+ def line
144
+ @current_line
145
+ end
146
+
147
+ def to_s(add_blank_line = false)
148
+ previous_indent_level = 0
149
+ @lines.inject('') do |ret, n|
150
+ ret << "\n" if add_blank_line && (n.indent_level < previous_indent_level) && !n.text.blank? && !ret.ends_with?("\n\n")
151
+ previous_indent_level = n.indent_level
152
+ ret << (n.text ? n.text.rstrip : '') + "\n"
153
+ end
154
+ end
155
+ end
156
+
157
+ class Nodes < Array
158
+ def initialize(parent)
159
+ super()
160
+ @parent = parent
161
+ end
162
+
163
+ def [](index)
164
+ if index.is_a?(String) || index.is_a?(Symbol)
165
+ return self.detect {|node| node.key == index} || add(index)
166
+ end
167
+ super
168
+ end
169
+
170
+ def last_leaf
171
+ c = @parent
172
+ loop do
173
+ return c if c.children.blank?
174
+ c = c.children.last
175
+ end
176
+ end
177
+
178
+ def add(node_name)
179
+ target_line = self.last_leaf.line + 1
180
+ @parent.document.nodes.each {|n| n.line += 1 if n.line >= target_line}
181
+ node = Node.new(@parent, target_line, node_name)
182
+ @parent.document.lines << node
183
+ @parent.document.lines.sort!
184
+ self << node unless @parent.is_a? YamlDocument
185
+ end
186
+ end
187
+ end
File without changes
@@ -0,0 +1,4 @@
1
+ I18n.load_path += Dir.glob("#{RAILS_ROOT}/lib/locale/*.yml")
2
+
3
+ I18n.default_locale = '<%= locale_name %>'
4
+ I18n.locale = '<%= locale_name %>'
@@ -0,0 +1 @@
1
+ <%= translations %>
@@ -0,0 +1,10 @@
1
+ Description:
2
+ Generates a config file and locale files for Rails 2.x i18n.
3
+
4
+ Example:
5
+ ./script/generate i18n locale_name (ja, de-AT, etc.)
6
+
7
+ This will create:
8
+ config/locales/ja.yml
9
+ And update:
10
+ config/environment.rb