translate-rails3-plus 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -21,12 +21,13 @@ Rake Tasks
21
21
 
22
22
  In addition to the web UI this plugin adds the following rake tasks:
23
23
 
24
- translate:untranslated
25
- translate:missing
26
- translate:remove_obsolete_keys
27
- translate:merge_keys
28
- translate:google
29
- translate:changed
24
+ translate:untranslated
25
+ translate:missing
26
+ translate:remove_obsolete_keys
27
+ translate:merge_keys
28
+ translate:google
29
+ translate:changed
30
+ translate:duplicates
30
31
 
31
32
  The missing task shows you any I18n keys in your code that do not have translations in the YAML file for your default locale, i.e. config/locales/sv.yml.
32
33
 
@@ -37,6 +38,8 @@ The google task is used for auto translating from one locale to another using Go
37
38
 
38
39
  The changed rake task can show you between one YAML file to another which keys have had their texts changed.
39
40
 
41
+ The duplicates task can show you strings that are the same in both locales.
42
+
40
43
  Installation
41
44
  ------------
42
45
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.0.7
@@ -1,222 +1,217 @@
1
- class TranslateController < ActionController::Base
2
- # It seems users with active_record_store may get a "no :secret given" error if we don't disable csrf protection,
3
- skip_before_filter :verify_authenticity_token
4
-
5
- layout 'translate'
6
-
7
- before_filter :init_translations
8
- before_filter :set_locale
9
-
10
- # GET /translate
11
- def index
12
- initialize_keys
13
- filter_by_key_pattern
14
- filter_by_text_pattern
15
- filter_by_translated_text_pattern
16
- filter_by_translated_or_changed
17
- sort_keys
18
- paginate_keys
19
- @total_entries = @keys.size
20
- @page_title = page_title
21
- end
22
-
23
- # POST /translate
24
- def translate
25
- processed_parameters = process_array_parameters(params[:key])
26
- I18n.backend.store_translations(@to_locale, Translate::Keys.to_deep_hash(processed_parameters))
27
- Translate::Storage.new(@to_locale).write_to_file
28
- Translate::Log.new(@from_locale, @to_locale, params[:key].keys).write_to_file
29
- force_init_translations # Force reload from YAML file
30
- flash[:notice] = "Translations stored"
31
- redirect_to params.slice(:filter, :sort_by, :key_type, :key_pattern, :text_type, :text_pattern, :translated_text_type, :translated_text_pattern).merge({:action => :index})
32
- end
33
-
34
- # GET /translate/reload
35
- def reload
36
- Translate::Keys.files = nil
37
- redirect_to :action => 'index'
38
- end
39
-
40
- private
41
- def initialize_keys
42
- @files = Translate::Keys.files
43
- @keys = (@files.keys.map(&:to_s) + Translate::Keys.new.i18n_keys(@from_locale)).uniq
44
- @keys.reject! do |key|
45
- from_text = lookup(@from_locale, key)
46
- # When translating from one language to another, make sure there is a text to translate from.
47
- # The only supported formats are String and Array. We don't support other formats
48
- (@from_locale != @to_locale && !from_text.present?) || (from_text.present? && !from_text.is_a?(String) && !from_text.is_a?(Array))
49
- end
50
- end
51
-
52
- def page_title
53
- "Translate"
54
- end
55
-
56
- def lookup(locale, key)
57
- I18n.backend.send(:lookup, locale, key)
58
- end
59
- helper_method :lookup
60
-
61
- def from_locales
62
- # Attempt to get the list of locale from configuration
63
- from_loc = Rails.application.config.from_locales if Rails.application.config.respond_to?(:from_locales)
64
- return I18n.available_locales if from_loc.blank?
65
- raise StandardError, "from_locale expected to be an array" if from_loc.class != Array
66
- from_loc
67
- end
68
- helper_method :from_locales
69
-
70
- def to_locales
71
- to_loc = Rails.application.config.to_locales if Rails.application.config.respond_to?(:to_locales)
72
- return I18n.available_locales if to_loc.blank?
73
- raise StandardError, "to_locales expected to be an array" if to_loc.class != Array
74
- to_loc
75
- end
76
- helper_method :to_locales
77
-
78
- def filter_by_translated_or_changed
79
- params[:filter] ||= 'all'
80
- return if params[:filter] == 'all'
81
- @keys.reject! do |key|
82
- case params[:filter]
83
- when 'untranslated'
84
- lookup(@to_locale, key).present?
85
- when 'translated'
86
- lookup(@to_locale, key).blank?
87
- when 'changed'
88
- old_from_text(key).blank? || lookup(@from_locale, key) == old_from_text(key)
89
- else
90
- raise "Unknown filter '#{params[:filter]}'"
91
- end
92
- end
93
- end
94
-
95
- def filter_by_key_pattern
96
- return if params[:key_pattern].blank?
97
- @keys.reject! do |key|
98
- case params[:key_type]
99
- when "starts_with"
100
- !key.starts_with?(params[:key_pattern])
101
- when "contains"
102
- key.index(params[:key_pattern]).nil?
103
- else
104
- raise "Unknown key_type '#{params[:key_type]}'"
105
- end
106
- end
107
- end
108
-
109
- def filter_by_text_pattern
110
- return if params[:text_pattern].blank?
111
- @keys.reject! do |key|
112
- case params[:text_type]
113
- when 'contains'
114
- !lookup(@from_locale, key).present? || !lookup(@from_locale, key).to_s.downcase.index(params[:text_pattern].downcase)
115
- when 'equals'
116
- !lookup(@from_locale, key).present? || lookup(@from_locale, key).to_s.downcase != params[:text_pattern].downcase
117
- else
118
- raise "Unknown text_type '#{params[:text_type]}'"
119
- end
120
- end
121
- end
122
-
123
- def filter_by_translated_text_pattern
124
- return if params[:translated_text_pattern].blank?
125
- @keys.reject! do |key|
126
- case params[:translated_text_type]
127
- when 'contains' then
128
- !lookup(@to_locale, key).present? || !lookup(@to_locale, key).to_s.downcase.index(params[:translated_text_pattern].downcase)
129
- when 'equals' then
130
- !lookup(@to_locale, key).present? || lookup(@to_locale, key).to_s.downcase != params[:translated_text_pattern].downcase
131
- else
132
- raise "Unknown translated_text_type '#{params[:translated_text_type]}'"
133
- end
134
- end
135
- end
136
-
137
- def sort_keys
138
- params[:sort_by] ||= "key"
139
- case params[:sort_by]
140
- when "key"
141
- @keys.sort!
142
- when "text"
143
- @keys.sort! do |key1, key2|
144
- if lookup(@from_locale, key1).present? && lookup(@from_locale, key2).present?
145
- lookup(@from_locale, key1).to_s.downcase <=> lookup(@from_locale, key2).to_s.downcase
146
- elsif lookup(@from_locale, key1).present?
147
- -1
148
- else
149
- 1
150
- end
151
- end
152
- else
153
- raise "Unknown sort_by '#{params[:sort_by]}'"
154
- end
155
- end
156
-
157
- def paginate_keys
158
- params[:page] ||= 1
159
- @paginated_keys = @keys[offset, per_page]
160
- end
161
-
162
- def offset
163
- (params[:page].to_i - 1) * per_page
164
- end
165
-
166
- def per_page
167
- 50
168
- end
169
- helper_method :per_page
170
-
171
- def init_translations
172
- I18n.backend.send(:init_translations) unless I18n.backend.initialized?
173
- end
174
-
175
- def force_init_translations
176
- I18n.backend.send(:init_translations)
177
- end
178
-
179
- def default_locale
180
- I18n.default_locale
181
- end
182
-
183
- def default_to_locale
184
- :en
185
- end
186
-
187
- def set_locale
188
- session[:from_locale] ||= default_locale
189
- session[:to_locale] ||= default_to_locale
190
- session[:from_locale] = params[:from_locale] if params[:from_locale].present?
191
- session[:to_locale] = params[:to_locale] if params[:to_locale].present?
192
- @from_locale = session[:from_locale].to_sym
193
- @to_locale = session[:to_locale].to_sym
194
- end
195
-
196
- def old_from_text(key)
197
- return @old_from_text[key] if @old_from_text && @old_from_text[key]
198
- @old_from_text = {}
199
- text = key.split(".").inject(log_hash) do |hash, k|
200
- hash ? hash[k] : nil
201
- end
202
- @old_from_text[key] = text
203
- end
204
- helper_method :old_from_text
205
-
206
- def log_hash
207
- @log_hash ||= Translate::Log.new(@from_locale, @to_locale, {}).read
208
- end
209
-
210
- def process_array_parameters(parameter)
211
- reconstructed_hash = Hash.new
212
-
213
- parameter.each do |key, value|
214
- if value.is_a?(String)
215
- reconstructed_hash[key] = value
216
- elsif value.is_a?(Hash)
217
- reconstructed_hash[key] = Translate::Keys.arraylize(value)
218
- end
219
- end
220
- reconstructed_hash
221
- end
222
- end
1
+ class TranslateController < ActionController::Base
2
+ # It seems users with active_record_store may get a "no :secret given" error if we don't disable csrf protection,
3
+ skip_before_filter :verify_authenticity_token
4
+
5
+ layout 'translate'
6
+
7
+ before_filter :init_translations
8
+ before_filter :set_locale
9
+
10
+ # GET /translate
11
+ def index
12
+ initialize_keys
13
+ filter_by_key_pattern
14
+ filter_by_text_pattern
15
+ filter_by_translated_text_pattern
16
+ filter_by_translated_or_changed
17
+ sort_keys
18
+ paginate_keys
19
+ @total_entries = @keys.size
20
+ @page_title = page_title
21
+ end
22
+
23
+ # POST /translate
24
+ def translate
25
+ processed_parameters = process_array_parameters(params[:key])
26
+ I18n.backend.store_translations(@to_locale, Translate::Keys.to_deep_hash(processed_parameters))
27
+ Translate::Storage.new(@to_locale).write_to_file
28
+ Translate::Log.new(@from_locale, @to_locale, params[:key].keys).write_to_file
29
+ force_init_translations # Force reload from YAML file
30
+ flash[:notice] = "Translations stored"
31
+ redirect_to params.slice(:filter, :sort_by, :key_type, :key_pattern, :text_type, :text_pattern, :translated_text_type, :translated_text_pattern).merge({:action => :index})
32
+ end
33
+
34
+ # GET /translate/reload
35
+ def reload
36
+ Translate::Keys.files = nil
37
+ redirect_to :action => 'index'
38
+ end
39
+
40
+ private
41
+ def initialize_keys
42
+ @files = Translate::Keys.files
43
+ @keys = (@files.keys.map(&:to_s) + Translate::Keys.new.i18n_keys(@from_locale)).uniq
44
+ @keys.reject! do |key|
45
+ from_text = lookup(@from_locale, key)
46
+ # When translating from one language to another, make sure there is a text to translate from.
47
+ # The only supported formats are String and Array. We don't support other formats
48
+ (@from_locale != @to_locale && !from_text.present?) || (from_text.present? && !from_text.is_a?(String) && !from_text.is_a?(Array))
49
+ end
50
+ end
51
+
52
+ def page_title
53
+ "Translate"
54
+ end
55
+
56
+ def lookup(locale, key)
57
+ I18n.backend.send(:lookup, locale, key)
58
+ end
59
+ helper_method :lookup
60
+
61
+ def from_locales
62
+ # Attempt to get the list of locale from configuration
63
+ from_loc = Rails.application.config.from_locales if Rails.application.config.respond_to?(:from_locales)
64
+ return I18n.available_locales if from_loc.blank?
65
+ raise StandardError, "from_locale expected to be an array" if from_loc.class != Array
66
+ from_loc
67
+ end
68
+ helper_method :from_locales
69
+
70
+ def to_locales
71
+ to_loc = Rails.application.config.to_locales if Rails.application.config.respond_to?(:to_locales)
72
+ return I18n.available_locales if to_loc.blank?
73
+ raise StandardError, "to_locales expected to be an array" if to_loc.class != Array
74
+ to_loc
75
+ end
76
+ helper_method :to_locales
77
+
78
+ def filter_by_translated_or_changed
79
+ params[:filter] ||= 'all'
80
+ return if params[:filter] == 'all'
81
+ @keys.reject! do |key|
82
+ case params[:filter]
83
+ when 'untranslated'
84
+ lookup(@to_locale, key).present?
85
+ when 'translated'
86
+ lookup(@to_locale, key).blank?
87
+ when 'changed'
88
+ lookup(@from_locale, key).to_s == lookup(@to_locale, key).to_s
89
+ when 'list_changed'
90
+ fr = lookup(@from_locale, key).to_s.squish
91
+ to = lookup(@to_locale, key).to_s.squish
92
+ if fr.downcase != to.downcase
93
+ p '--'
94
+ p 'c:' + fr
95
+ p 'g:' + to
96
+ end
97
+ fr.downcase == to.downcase
98
+ else
99
+ raise "Unknown filter '#{params[:filter]}'"
100
+ end
101
+ end
102
+ end
103
+
104
+ def filter_by_key_pattern
105
+ return if params[:key_pattern].blank?
106
+ @keys.reject! do |key|
107
+ case params[:key_type]
108
+ when "starts_with"
109
+ !key.starts_with?(params[:key_pattern])
110
+ when "contains"
111
+ key.index(params[:key_pattern]).nil?
112
+ else
113
+ raise "Unknown key_type '#{params[:key_type]}'"
114
+ end
115
+ end
116
+ end
117
+
118
+ def filter_by_text_pattern
119
+ return if params[:text_pattern].blank?
120
+ @keys.reject! do |key|
121
+ case params[:text_type]
122
+ when 'contains'
123
+ !lookup(@from_locale, key).present? || !lookup(@from_locale, key).to_s.downcase.index(params[:text_pattern].downcase)
124
+ when 'equals'
125
+ !lookup(@from_locale, key).present? || lookup(@from_locale, key).to_s.downcase != params[:text_pattern].downcase
126
+ else
127
+ raise "Unknown text_type '#{params[:text_type]}'"
128
+ end
129
+ end
130
+ end
131
+
132
+ def filter_by_translated_text_pattern
133
+ return if params[:translated_text_pattern].blank?
134
+ @keys.reject! do |key|
135
+ case params[:translated_text_type]
136
+ when 'contains' then
137
+ !lookup(@to_locale, key).present? || !lookup(@to_locale, key).to_s.downcase.index(params[:translated_text_pattern].downcase)
138
+ when 'equals' then
139
+ !lookup(@to_locale, key).present? || lookup(@to_locale, key).to_s.downcase != params[:translated_text_pattern].downcase
140
+ else
141
+ raise "Unknown translated_text_type '#{params[:translated_text_type]}'"
142
+ end
143
+ end
144
+ end
145
+
146
+ def sort_keys
147
+ params[:sort_by] ||= "key"
148
+ case params[:sort_by]
149
+ when "key"
150
+ @keys.sort!
151
+ when "text"
152
+ @keys.sort! do |key1, key2|
153
+ if lookup(@from_locale, key1).present? && lookup(@from_locale, key2).present?
154
+ lookup(@from_locale, key1).to_s.downcase <=> lookup(@from_locale, key2).to_s.downcase
155
+ elsif lookup(@from_locale, key1).present?
156
+ -1
157
+ else
158
+ 1
159
+ end
160
+ end
161
+ else
162
+ raise "Unknown sort_by '#{params[:sort_by]}'"
163
+ end
164
+ end
165
+
166
+ def paginate_keys
167
+ params[:page] ||= 1
168
+ @paginated_keys = @keys[offset, per_page]
169
+ end
170
+
171
+ def offset
172
+ (params[:page].to_i - 1) * per_page
173
+ end
174
+
175
+ def per_page
176
+ 50
177
+ end
178
+ helper_method :per_page
179
+
180
+ def init_translations
181
+ I18n.backend.send(:init_translations) unless I18n.backend.initialized?
182
+ end
183
+
184
+ def force_init_translations
185
+ I18n.backend.send(:init_translations)
186
+ end
187
+
188
+ def default_locale
189
+ I18n.default_locale
190
+ end
191
+
192
+ def default_to_locale
193
+ :en
194
+ end
195
+
196
+ def set_locale
197
+ session[:from_locale] ||= default_locale
198
+ session[:to_locale] ||= default_to_locale
199
+ session[:from_locale] = params[:from_locale] if params[:from_locale].present?
200
+ session[:to_locale] = params[:to_locale] if params[:to_locale].present?
201
+ @from_locale = session[:from_locale].to_sym
202
+ @to_locale = session[:to_locale].to_sym
203
+ end
204
+
205
+ def process_array_parameters(parameter)
206
+ reconstructed_hash = Hash.new
207
+
208
+ parameter.each do |key, value|
209
+ if value.is_a?(String)
210
+ reconstructed_hash[key] = value
211
+ elsif value.is_a?(Hash)
212
+ reconstructed_hash[key] = Translate::Keys.arraylize(value)
213
+ end
214
+ end
215
+ reconstructed_hash
216
+ end
217
+ end
@@ -33,6 +33,27 @@ class Hash
33
33
  end
34
34
 
35
35
  namespace :translate do
36
+ desc "Show duplicate keys for locale LOCALE"
37
+ task :duplicates => :environment do
38
+ from_locale = I18n.default_locale
39
+ duplicates = Translate::Keys.new.duplicate_keys
40
+
41
+ messages = []
42
+ duplicates.each do |locale, keys|
43
+ keys.each do |key|
44
+ from_text = I18n.backend.send(:lookup, from_locale, key)
45
+ next if from_text.blank?
46
+ messages << "#{locale}.#{key} (#{from_locale}.#{key}='#{from_text}')"
47
+ end
48
+ end
49
+
50
+ if messages.present?
51
+ messages.each { |m| puts m }
52
+ else
53
+ puts "No duplicate keys"
54
+ end
55
+ end
56
+
36
57
  desc "Show untranslated keys for locale LOCALE"
37
58
  task :untranslated => :environment do
38
59
  from_locale = I18n.default_locale
@@ -42,6 +63,7 @@ namespace :translate do
42
63
  untranslated.each do |locale, keys|
43
64
  keys.each do |key|
44
65
  from_text = I18n.backend.send(:lookup, from_locale, key)
66
+ next if from_text.blank?
45
67
  messages << "#{locale}.#{key} (#{from_locale}.#{key}='#{from_text}')"
46
68
  end
47
69
  end
@@ -26,6 +26,15 @@ class Translate::Keys
26
26
  Translate::Keys.to_shallow_hash(I18n.backend.send(:translations)[locale.to_sym]).keys.sort
27
27
  end
28
28
 
29
+ def duplicate_keys
30
+ Translate::Keys.translated_locales.inject({}) do |missing, locale|
31
+ missing[locale] = i18n_keys(I18n.default_locale).map do |key|
32
+ I18n.backend.send(:lookup, locale, key) == I18n.backend.send(:lookup, I18n.default_locale, key) ? key : nil
33
+ end.compact
34
+ missing
35
+ end
36
+ end
37
+
29
38
  def untranslated_keys
30
39
  Translate::Keys.translated_locales.inject({}) do |missing, locale|
31
40
  missing[locale] = i18n_keys(I18n.default_locale).map do |key|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: translate-rails3-plus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -16,7 +16,7 @@ date: 2012-07-09 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: ya2yaml
19
- requirement: &25088304 !ruby/object:Gem::Requirement
19
+ requirement: !ruby/object:Gem::Requirement
20
20
  none: false
21
21
  requirements:
22
22
  - - ~>
@@ -24,7 +24,12 @@ dependencies:
24
24
  version: '0.30'
25
25
  type: :runtime
26
26
  prerelease: false
27
- version_requirements: *25088304
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '0.30'
28
33
  description: ! 'This plugin provides a web interface for translating Rails I18n texts
29
34
 
30
35
  (requires Rails 3.0 or higher) from one locale to another.
@@ -101,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
106
  version: '0'
102
107
  requirements: []
103
108
  rubyforge_project:
104
- rubygems_version: 1.8.16
109
+ rubygems_version: 1.8.24
105
110
  signing_key:
106
111
  specification_version: 3
107
112
  summary: Newsdesk translate plugin for Rails 3