translate-rails3-plus 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 42b678b901b95cd6c25dd2c2f2a11ebb606b89b1
4
- data.tar.gz: 734f59c9c3c34f72c71dbcce03117f01436d7462
3
+ metadata.gz: 351f597b8e252731a6245fb1fc6d3a376f6df7d1
4
+ data.tar.gz: b956f7170d82fbd4bc968a4acfb9b4945f518ec6
5
5
  SHA512:
6
- metadata.gz: e4c7828fdf5f21b97b4213811f6568fc0409acbeaa3a14c3906b50238651f84c5ab3b4b41b6dd5feed66f1f0cffcec63c459bbaf4317b2fa189cdae9dcfd73d5
7
- data.tar.gz: 59e00689c9ff138538c0433f010275aee8a5211e1c1f2fc8fba809b48aaf5c760f466de38ce0442f8312ca35b9d4e1c898973654ab5c09c504b449929876b944
6
+ metadata.gz: aa1e8e3f7d557f42b36416f920bcd6861679e4e262f27ef7091b651bd8d2b473e79a7054927125d57ed6955aa999aedc0b074e8fe5fdbe0ac5f8d55700cfe053
7
+ data.tar.gz: 5426601577628ea4541823bfe07af25164376f85bb1e4ca012c0e706dc022ffbfbde2f0bf822a11dd5b69696a484287d7baf759d500f4ae06bc3a9ebc0dcd0ce
data/MIT-LICENSE CHANGED
@@ -1,20 +1,20 @@
1
- Copyright (c) 2009 [name of plugin creator]
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.
1
+ Copyright (c) 2009 [name of plugin creator]
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.md CHANGED
@@ -1,89 +1,89 @@
1
- Translate
2
- =========
3
-
4
- This plugin provides a web interface for translating Rails I18n texts (requires Rails 3.0 or higher) from one locale to another. The plugin has been tested only with the simple I18n backend that ships with Rails. I18n texts are read from and written to YAML files under config/locales.
5
-
6
- To translate to a new locale you need to add a YAML file for that locale that contains the locale as the top key and at least one translation.
7
-
8
- Please note that there are certain I18n keys that map to Array objects rather than strings and those are currently not dealt with by the translation UI. This means that Rails built in keys such as date.day_names need to be translated manually directly in the YAML file.
9
-
10
- To get the translation UI to write the YAML files in UTF8 you need to install the ya2yaml gem.
11
-
12
- The translation UI finds all I18n keys by extracting them from I18n lookups in your application source code. In addition it adds all :en and default locale keys from the I18n backend.
13
-
14
- Strings in the UI can have an "Auto Translate" link (if configured, see below),
15
- which will send the original text to translation API and will input the returned
16
- translation into the form field for further clean up and review prior to saving.
17
-
18
-
19
- Rake Tasks
20
- ----------
21
-
22
- In addition to the web UI this plugin adds the following rake tasks:
23
-
24
- translate:untranslated
25
- translate:missing
26
- translate:remove_obsolete_keys
27
- translate:merge_keys
28
- translate:google
29
- translate:changed
30
- translate:duplicates
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.
33
-
34
- The merge_keys task is supposed to be used in conjunction with Sven Fuch's Rails I18n TextMate bundle (http://github.com/svenfuchs/rails-i18n/tree/master). Texts and keys extracted with the TextMate bundle end up in the temporary file log/translations.yml. When you run the merge_keys rake task the keys are moved over to the corresponding I18n locale file, i.e. config/locales/sv.yml. The merge_keys task also checks for overwrites of existing keys by warning you that one of your extracted keys already exists with a different translation.
35
-
36
- The google task is used for auto translating from one locale to another using Google Translate.
37
- * Note: this task is currently broken, as Google is now charging for the Google Translate service.
38
-
39
- The changed rake task can show you between one YAML file to another which keys have had their texts changed.
40
-
41
- The duplicates task can show you strings that are the same in both locales.
42
-
43
- Installation
44
- ------------
45
-
46
- Add to your Gemfile:
47
-
48
- gem 'translate-rails3', :require => 'translate', :group => :development
49
-
50
- Now visit /translate in your web browser to start translating.
51
-
52
- Configuration
53
- -------------
54
-
55
- (Optional) You can configure from_locales and to_locales explicitly through your environments/development.rb by adding
56
-
57
- config.from_locales = [:en]
58
- config.to_locales = [:ja, :es, :fr]
59
-
60
- Where [:en] and [:ja, :es, :fr] could be replaced by locale list of your choice.
61
-
62
- (Optional) You can bring back "Auto Translate" support by specifying Bing AppId or
63
- Google API Key in config/initializers/translate.rb with:
64
-
65
- Translate.app_id = 'myappid'
66
-
67
- or
68
-
69
- Translate.api_key = 'mysecretkey'
70
-
71
-
72
- Dependencies
73
- ------------
74
-
75
- - Rails 3.0 or higher
76
- - The ya2yaml gem if you want your YAML files written in UTF8 encoding.
77
-
78
- Authors
79
- -------
80
-
81
- - Peter Marklund (programming)
82
- - Joakim Westerlund (web design)
83
- - Milan Novota (initial Rails 3 support)
84
- - Roman Shterenzon (Rails 3 cleanup and gem packaging)
85
- - Ichiro Yamamoto
86
-
87
- Many thanks to http://newsdesk.se for sponsoring the development of this plugin.
88
-
89
- Copyright (c) 2009 Peter Marklund, released under the MIT license
1
+ Translate
2
+ =========
3
+
4
+ This plugin provides a web interface for translating Rails I18n texts (requires Rails 3.0 or higher) from one locale to another. The plugin has been tested only with the simple I18n backend that ships with Rails. I18n texts are read from and written to YAML files under config/locales.
5
+
6
+ To translate to a new locale you need to add a YAML file for that locale that contains the locale as the top key and at least one translation.
7
+
8
+ Please note that there are certain I18n keys that map to Array objects rather than strings and those are currently not dealt with by the translation UI. This means that Rails built in keys such as date.day_names need to be translated manually directly in the YAML file.
9
+
10
+ To get the translation UI to write the YAML files in UTF8 you need to install the ya2yaml gem.
11
+
12
+ The translation UI finds all I18n keys by extracting them from I18n lookups in your application source code. In addition it adds all :en and default locale keys from the I18n backend.
13
+
14
+ Strings in the UI can have an "Auto Translate" link (if configured, see below),
15
+ which will send the original text to translation API and will input the returned
16
+ translation into the form field for further clean up and review prior to saving.
17
+
18
+
19
+ Rake Tasks
20
+ ----------
21
+
22
+ In addition to the web UI this plugin adds the following rake tasks:
23
+
24
+ translate:untranslated
25
+ translate:missing
26
+ translate:remove_obsolete_keys
27
+ translate:merge_keys
28
+ translate:google
29
+ translate:changed
30
+ translate:duplicates
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.
33
+
34
+ The merge_keys task is supposed to be used in conjunction with Sven Fuch's Rails I18n TextMate bundle (http://github.com/svenfuchs/rails-i18n/tree/master). Texts and keys extracted with the TextMate bundle end up in the temporary file log/translations.yml. When you run the merge_keys rake task the keys are moved over to the corresponding I18n locale file, i.e. config/locales/sv.yml. The merge_keys task also checks for overwrites of existing keys by warning you that one of your extracted keys already exists with a different translation.
35
+
36
+ The google task is used for auto translating from one locale to another using Google Translate.
37
+ * Note: this task is currently broken, as Google is now charging for the Google Translate service.
38
+
39
+ The changed rake task can show you between one YAML file to another which keys have had their texts changed.
40
+
41
+ The duplicates task can show you strings that are the same in both locales.
42
+
43
+ Installation
44
+ ------------
45
+
46
+ Add to your Gemfile:
47
+
48
+ gem 'translate-rails3', :require => 'translate', :group => :development
49
+
50
+ Now visit /translate in your web browser to start translating.
51
+
52
+ Configuration
53
+ -------------
54
+
55
+ (Optional) You can configure from_locales and to_locales explicitly through your environments/development.rb by adding
56
+
57
+ config.from_locales = [:en]
58
+ config.to_locales = [:ja, :es, :fr]
59
+
60
+ Where [:en] and [:ja, :es, :fr] could be replaced by locale list of your choice.
61
+
62
+ (Optional) You can bring back "Auto Translate" support by specifying Bing AppId or
63
+ Google API Key in config/initializers/translate.rb with:
64
+
65
+ Translate.app_id = 'myappid'
66
+
67
+ or
68
+
69
+ Translate.api_key = 'mysecretkey'
70
+
71
+
72
+ Dependencies
73
+ ------------
74
+
75
+ - Rails 3.0 or higher
76
+ - The ya2yaml gem if you want your YAML files written in UTF8 encoding.
77
+
78
+ Authors
79
+ -------
80
+
81
+ - Peter Marklund (programming)
82
+ - Joakim Westerlund (web design)
83
+ - Milan Novota (initial Rails 3 support)
84
+ - Roman Shterenzon (Rails 3 cleanup and gem packaging)
85
+ - Ichiro Yamamoto
86
+
87
+ Many thanks to http://newsdesk.se for sponsoring the development of this plugin.
88
+
89
+ Copyright (c) 2009 Peter Marklund, released under the MIT license
data/Rakefile CHANGED
@@ -1,35 +1,35 @@
1
- require 'rspec/core/rake_task'
2
- require "bundler/gem_tasks"
3
-
4
- desc 'Default: run specs.'
5
- task :default => :spec
6
-
7
- desc 'Run the specs'
8
- RSpec::Core::RakeTask.new do |t|
9
- t.rspec_opts = ['--color --format progress']
10
- end
11
-
12
- begin
13
- require 'jeweler'
14
- Jeweler::Tasks.new do |gem|
15
- gem.name = 'translate-rails3-plus'
16
- gem.summary = %Q{Newsdesk translate plugin for Rails 3}
17
- gem.description = <<EOF
18
- This plugin provides a web interface for translating Rails I18n texts
19
- (requires Rails 3.0 or higher) from one locale to another.
20
- The plugin has been tested only with the simple I18n backend that ships
21
- with Rails.
22
- I18n texts are read from and written to YAML files under config/locales.
23
-
24
- This gem is a fork of https://github.com/romanbsd/translate.
25
- From the original https://github.com/mynewsdesk/translate
26
- which also includes work from this fork: https://github.com/milann/translate
27
- EOF
28
- gem.email = 'gsmedley@kanayo.com'
29
- gem.homepage = 'https://github.com/gsmedley/translate'
30
- gem.authors = ['Peter Marklund', 'Milan Novota', 'Roman Shterenzon', 'Garth Smedley']
31
- gem.add_dependency 'ya2yaml', '~> 0.30' # For UTF-8 support in YAML
32
- end
33
- Jeweler::GemcutterTasks.new
34
- rescue LoadError
35
- end
1
+ require 'rspec/core/rake_task'
2
+ require "bundler/gem_tasks"
3
+
4
+ desc 'Default: run specs.'
5
+ task :default => :spec
6
+
7
+ desc 'Run the specs'
8
+ RSpec::Core::RakeTask.new do |t|
9
+ t.rspec_opts = ['--color --format progress']
10
+ end
11
+
12
+ begin
13
+ require 'jeweler'
14
+ Jeweler::Tasks.new do |gem|
15
+ gem.name = 'translate-rails3-plus'
16
+ gem.summary = %Q{Newsdesk translate plugin for Rails 3}
17
+ gem.description = <<EOF
18
+ This plugin provides a web interface for translating Rails I18n texts
19
+ (requires Rails 3.0 or higher) from one locale to another.
20
+ The plugin has been tested only with the simple I18n backend that ships
21
+ with Rails.
22
+ I18n texts are read from and written to YAML files under config/locales.
23
+
24
+ This gem is a fork of https://github.com/romanbsd/translate.
25
+ From the original https://github.com/mynewsdesk/translate
26
+ which also includes work from this fork: https://github.com/milann/translate
27
+ EOF
28
+ gem.email = 'gsmedley@kanayo.com'
29
+ gem.homepage = 'https://github.com/gsmedley/translate'
30
+ gem.authors = ['Peter Marklund', 'Milan Novota', 'Roman Shterenzon', 'Garth Smedley']
31
+ gem.add_dependency 'ya2yaml', '~> 0.30' # For UTF-8 support in YAML
32
+ end
33
+ Jeweler::GemcutterTasks.new
34
+ rescue LoadError
35
+ end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.11
1
+ 0.0.12
@@ -1,218 +1,218 @@
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_action :verify_authenticity_token, raise: false
4
-
5
- layout 'translate'
6
-
7
- before_action :init_translations
8
- before_action :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
- old_params = params.slice(:filter, :sort_by, :key_type, :key_pattern, :text_type, :text_pattern, :translated_text_type, :translated_text_pattern)
32
- redirect_to( {:action => :index}.merge(old_params) )
33
- end
34
-
35
- # GET /translate/reload
36
- def reload
37
- Translate::Keys.files = nil
38
- redirect_to :action => 'index'
39
- end
40
-
41
- private
42
- def initialize_keys
43
- @files = Translate::Keys.files
44
- @keys = (@files.keys.map(&:to_s) + Translate::Keys.new.i18n_keys(@from_locale)).uniq
45
- @keys.reject! do |key|
46
- from_text = lookup(@from_locale, key)
47
- # When translating from one language to another, make sure there is a text to translate from.
48
- # The only supported formats are String and Array. We don't support other formats
49
- (@from_locale != @to_locale && !from_text.present?) || (from_text.present? && !from_text.is_a?(String) && !from_text.is_a?(Array))
50
- end
51
- end
52
-
53
- def page_title
54
- "Translate"
55
- end
56
-
57
- def lookup(locale, key)
58
- I18n.backend.send(:lookup, locale, key)
59
- end
60
- helper_method :lookup
61
-
62
- def from_locales
63
- # Attempt to get the list of locale from configuration
64
- from_loc = Rails.application.config.from_locales if Rails.application.config.respond_to?(:from_locales)
65
- return I18n.available_locales if from_loc.blank?
66
- raise StandardError, "from_locale expected to be an array" if from_loc.class != Array
67
- from_loc
68
- end
69
- helper_method :from_locales
70
-
71
- def to_locales
72
- to_loc = Rails.application.config.to_locales if Rails.application.config.respond_to?(:to_locales)
73
- return I18n.available_locales if to_loc.blank?
74
- raise StandardError, "to_locales expected to be an array" if to_loc.class != Array
75
- to_loc
76
- end
77
- helper_method :to_locales
78
-
79
- def filter_by_translated_or_changed
80
- params[:filter] ||= 'all'
81
- return if params[:filter] == 'all'
82
- @keys.reject! do |key|
83
- case params[:filter]
84
- when 'untranslated'
85
- lookup(@to_locale, key).present?
86
- when 'translated'
87
- lookup(@to_locale, key).blank?
88
- when 'changed'
89
- lookup(@from_locale, key).to_s == lookup(@to_locale, key).to_s
90
- when 'list_changed'
91
- fr = lookup(@from_locale, key).to_s.squish
92
- to = lookup(@to_locale, key).to_s.squish
93
- if fr.downcase != to.downcase
94
- p '--'
95
- p 'c:' + fr
96
- p 'g:' + to
97
- end
98
- fr.downcase == to.downcase
99
- else
100
- raise "Unknown filter '#{params[:filter]}'"
101
- end
102
- end
103
- end
104
-
105
- def filter_by_key_pattern
106
- return if params[:key_pattern].blank?
107
- @keys.reject! do |key|
108
- case params[:key_type]
109
- when "starts_with"
110
- !key.starts_with?(params[:key_pattern])
111
- when "contains"
112
- key.index(params[:key_pattern]).nil?
113
- else
114
- raise "Unknown key_type '#{params[:key_type]}'"
115
- end
116
- end
117
- end
118
-
119
- def filter_by_text_pattern
120
- return if params[:text_pattern].blank?
121
- @keys.reject! do |key|
122
- case params[:text_type]
123
- when 'contains'
124
- !lookup(@from_locale, key).present? || !lookup(@from_locale, key).to_s.downcase.index(params[:text_pattern].downcase)
125
- when 'equals'
126
- !lookup(@from_locale, key).present? || lookup(@from_locale, key).to_s.downcase != params[:text_pattern].downcase
127
- else
128
- raise "Unknown text_type '#{params[:text_type]}'"
129
- end
130
- end
131
- end
132
-
133
- def filter_by_translated_text_pattern
134
- return if params[:translated_text_pattern].blank?
135
- @keys.reject! do |key|
136
- case params[:translated_text_type]
137
- when 'contains' then
138
- !lookup(@to_locale, key).present? || !lookup(@to_locale, key).to_s.downcase.index(params[:translated_text_pattern].downcase)
139
- when 'equals' then
140
- !lookup(@to_locale, key).present? || lookup(@to_locale, key).to_s.downcase != params[:translated_text_pattern].downcase
141
- else
142
- raise "Unknown translated_text_type '#{params[:translated_text_type]}'"
143
- end
144
- end
145
- end
146
-
147
- def sort_keys
148
- params[:sort_by] ||= "key"
149
- case params[:sort_by]
150
- when "key"
151
- @keys.sort!
152
- when "text"
153
- @keys.sort! do |key1, key2|
154
- if lookup(@from_locale, key1).present? && lookup(@from_locale, key2).present?
155
- lookup(@from_locale, key1).to_s.downcase <=> lookup(@from_locale, key2).to_s.downcase
156
- elsif lookup(@from_locale, key1).present?
157
- -1
158
- else
159
- 1
160
- end
161
- end
162
- else
163
- raise "Unknown sort_by '#{params[:sort_by]}'"
164
- end
165
- end
166
-
167
- def paginate_keys
168
- params[:page] ||= 1
169
- @paginated_keys = @keys[offset, per_page]
170
- end
171
-
172
- def offset
173
- (params[:page].to_i - 1) * per_page
174
- end
175
-
176
- def per_page
177
- 50
178
- end
179
- helper_method :per_page
180
-
181
- def init_translations
182
- I18n.backend.send(:init_translations) unless I18n.backend.initialized?
183
- end
184
-
185
- def force_init_translations
186
- I18n.backend.send(:init_translations)
187
- end
188
-
189
- def default_locale
190
- I18n.default_locale
191
- end
192
-
193
- def default_to_locale
194
- :en
195
- end
196
-
197
- def set_locale
198
- session[:from_locale] ||= default_locale
199
- session[:to_locale] ||= default_to_locale
200
- session[:from_locale] = params[:from_locale] if params[:from_locale].present?
201
- session[:to_locale] = params[:to_locale] if params[:to_locale].present?
202
- @from_locale = session[:from_locale].to_sym
203
- @to_locale = session[:to_locale].to_sym
204
- end
205
-
206
- def process_array_parameters(parameter)
207
- reconstructed_hash = Hash.new
208
-
209
- parameter.each do |key, value|
210
- if value.is_a?(String)
211
- reconstructed_hash[key] = value
212
- elsif value.is_a?(Hash)
213
- reconstructed_hash[key] = Translate::Keys.arraylize(value)
214
- end
215
- end
216
- reconstructed_hash
217
- end
218
- 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_action :verify_authenticity_token, raise: false
4
+
5
+ layout 'translate'
6
+
7
+ before_action :init_translations
8
+ before_action :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
+ old_params = params.slice(:filter, :sort_by, :key_type, :key_pattern, :text_type, :text_pattern, :translated_text_type, :translated_text_pattern)
32
+ redirect_to( {:action => :index}.merge(old_params) )
33
+ end
34
+
35
+ # GET /translate/reload
36
+ def reload
37
+ Translate::Keys.files = nil
38
+ redirect_to :action => 'index'
39
+ end
40
+
41
+ private
42
+ def initialize_keys
43
+ @files = Translate::Keys.files
44
+ @keys = (@files.keys.map(&:to_s) + Translate::Keys.new.i18n_keys(@from_locale)).uniq
45
+ @keys.reject! do |key|
46
+ from_text = lookup(@from_locale, key)
47
+ # When translating from one language to another, make sure there is a text to translate from.
48
+ # The only supported formats are String and Array. We don't support other formats
49
+ (@from_locale != @to_locale && !from_text.present?) || (from_text.present? && !from_text.is_a?(String) && !from_text.is_a?(Array))
50
+ end
51
+ end
52
+
53
+ def page_title
54
+ "Translate"
55
+ end
56
+
57
+ def lookup(locale, key)
58
+ I18n.backend.send(:lookup, locale, key)
59
+ end
60
+ helper_method :lookup
61
+
62
+ def from_locales
63
+ # Attempt to get the list of locale from configuration
64
+ from_loc = Rails.application.config.from_locales if Rails.application.config.respond_to?(:from_locales)
65
+ return I18n.available_locales if from_loc.blank?
66
+ raise StandardError, "from_locale expected to be an array" if from_loc.class != Array
67
+ from_loc
68
+ end
69
+ helper_method :from_locales
70
+
71
+ def to_locales
72
+ to_loc = Rails.application.config.to_locales if Rails.application.config.respond_to?(:to_locales)
73
+ return I18n.available_locales if to_loc.blank?
74
+ raise StandardError, "to_locales expected to be an array" if to_loc.class != Array
75
+ to_loc
76
+ end
77
+ helper_method :to_locales
78
+
79
+ def filter_by_translated_or_changed
80
+ params[:filter] ||= 'all'
81
+ return if params[:filter] == 'all'
82
+ @keys.reject! do |key|
83
+ case params[:filter]
84
+ when 'untranslated'
85
+ lookup(@to_locale, key).present?
86
+ when 'translated'
87
+ lookup(@to_locale, key).blank?
88
+ when 'changed'
89
+ lookup(@from_locale, key).to_s == lookup(@to_locale, key).to_s
90
+ when 'list_changed'
91
+ fr = lookup(@from_locale, key).to_s.squish
92
+ to = lookup(@to_locale, key).to_s.squish
93
+ if fr.downcase != to.downcase
94
+ p '--'
95
+ p 'c:' + fr
96
+ p 'g:' + to
97
+ end
98
+ fr.downcase == to.downcase
99
+ else
100
+ raise "Unknown filter '#{params[:filter]}'"
101
+ end
102
+ end
103
+ end
104
+
105
+ def filter_by_key_pattern
106
+ return if params[:key_pattern].blank?
107
+ @keys.reject! do |key|
108
+ case params[:key_type]
109
+ when "starts_with"
110
+ !key.starts_with?(params[:key_pattern])
111
+ when "contains"
112
+ key.index(params[:key_pattern]).nil?
113
+ else
114
+ raise "Unknown key_type '#{params[:key_type]}'"
115
+ end
116
+ end
117
+ end
118
+
119
+ def filter_by_text_pattern
120
+ return if params[:text_pattern].blank?
121
+ @keys.reject! do |key|
122
+ case params[:text_type]
123
+ when 'contains'
124
+ !lookup(@from_locale, key).present? || !lookup(@from_locale, key).to_s.downcase.index(params[:text_pattern].downcase)
125
+ when 'equals'
126
+ !lookup(@from_locale, key).present? || lookup(@from_locale, key).to_s.downcase != params[:text_pattern].downcase
127
+ else
128
+ raise "Unknown text_type '#{params[:text_type]}'"
129
+ end
130
+ end
131
+ end
132
+
133
+ def filter_by_translated_text_pattern
134
+ return if params[:translated_text_pattern].blank?
135
+ @keys.reject! do |key|
136
+ case params[:translated_text_type]
137
+ when 'contains' then
138
+ !lookup(@to_locale, key).present? || !lookup(@to_locale, key).to_s.downcase.index(params[:translated_text_pattern].downcase)
139
+ when 'equals' then
140
+ !lookup(@to_locale, key).present? || lookup(@to_locale, key).to_s.downcase != params[:translated_text_pattern].downcase
141
+ else
142
+ raise "Unknown translated_text_type '#{params[:translated_text_type]}'"
143
+ end
144
+ end
145
+ end
146
+
147
+ def sort_keys
148
+ params[:sort_by] ||= "key"
149
+ case params[:sort_by]
150
+ when "key"
151
+ @keys.sort!
152
+ when "text"
153
+ @keys.sort! do |key1, key2|
154
+ if lookup(@from_locale, key1).present? && lookup(@from_locale, key2).present?
155
+ lookup(@from_locale, key1).to_s.downcase <=> lookup(@from_locale, key2).to_s.downcase
156
+ elsif lookup(@from_locale, key1).present?
157
+ -1
158
+ else
159
+ 1
160
+ end
161
+ end
162
+ else
163
+ raise "Unknown sort_by '#{params[:sort_by]}'"
164
+ end
165
+ end
166
+
167
+ def paginate_keys
168
+ params[:page] ||= 1
169
+ @paginated_keys = @keys[offset, per_page]
170
+ end
171
+
172
+ def offset
173
+ (params[:page].to_i - 1) * per_page
174
+ end
175
+
176
+ def per_page
177
+ 50
178
+ end
179
+ helper_method :per_page
180
+
181
+ def init_translations
182
+ I18n.backend.send(:init_translations) unless I18n.backend.initialized?
183
+ end
184
+
185
+ def force_init_translations
186
+ I18n.backend.send(:init_translations)
187
+ end
188
+
189
+ def default_locale
190
+ I18n.default_locale
191
+ end
192
+
193
+ def default_to_locale
194
+ :en
195
+ end
196
+
197
+ def set_locale
198
+ session[:from_locale] ||= default_locale
199
+ session[:to_locale] ||= default_to_locale
200
+ session[:from_locale] = params[:from_locale] if params[:from_locale].present?
201
+ session[:to_locale] = params[:to_locale] if params[:to_locale].present?
202
+ @from_locale = session[:from_locale].to_sym
203
+ @to_locale = session[:to_locale].to_sym
204
+ end
205
+
206
+ def process_array_parameters(parameter)
207
+ reconstructed_hash = Hash.new
208
+
209
+ parameter.each do |key, value|
210
+ if value.is_a?(String)
211
+ reconstructed_hash[key] = value
212
+ elsif value.is_a?(Hash)
213
+ reconstructed_hash[key] = Translate::Keys.arraylize(value)
214
+ end
215
+ end
216
+ reconstructed_hash
217
+ end
218
+ end