rails-i18nterface 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  [![Dependency Status](https://gemnasium.com/mose/rails-i18nterface.png)](https://gemnasium.com/mose/rails-i18nterface)
4
4
  [![Build Status](https://secure.travis-ci.org/mose/rails-i18nterface.png?branch=master)](http://travis-ci.org/mose/rails-i18nterface)
5
+ [![Coverage Status](https://coveralls.io/repos/mose/rails-i18nterface/badge.png?branch=master)](https://coveralls.io/r/mose/rails-i18nterface)
5
6
  [![Code Climate](https://codeclimate.com/github/mose/rails-i18nterface.png)](https://codeclimate.com/github/mose/rails-i18nterface)
6
7
  [![Gem Version](https://badge.fury.io/rb/rails-i18nterface.png)](http://badge.fury.io/rb/rails-i18nterface)
7
8
 
@@ -123,4 +124,4 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
123
124
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
124
125
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
125
126
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
126
- ```
127
+ ```
@@ -278,7 +278,7 @@ ul.paging {
278
278
  display: block;
279
279
  margin: 0;
280
280
  }
281
- ul.paging li{
281
+ ul.paging li {
282
282
  display: block;
283
283
  margin: 0;
284
284
  float: left;
@@ -292,9 +292,9 @@ ul.paging li.selected a {
292
292
  text-decoration: none;
293
293
  border-radius: 5px;
294
294
  }
295
- ul.paging li a{
296
- line-height:1em;
297
- padding:0.5em 0.5em;
295
+ ul.paging li a {
296
+ line-height: 1em;
297
+ padding: 0.5em 0.5em;
298
298
  min-width: 20px;
299
299
  text-decoration: none;
300
300
  font-weight: bold;
@@ -306,6 +306,10 @@ ul.paging li a:hover {
306
306
  ul.paging li.selected a:hover {
307
307
  background: #000;
308
308
  }
309
+ li.gap {
310
+ padding: 0 1em;
311
+ letter-spacing: .5em;
312
+ }
309
313
  /*forms filter*/
310
314
  #searchbox .sep {
311
315
  margin-left: 40px;
@@ -11,16 +11,9 @@ module RailsI18nterface
11
11
  def index
12
12
  @dbvalues = {}
13
13
  @keys = RailsI18nterface::Keys.new(Rails.root, @from_locale, @to_locale)
14
- @files = @keys.files
15
- @yaml_keys = @keys.yaml_keys
16
- @all_keys = @keys.all_keys
17
- @deep_keys = to_deep_hash(@all_keys)
18
- filter_by_key_pattern
19
- filter_by_text_pattern
20
- filter_by_translated_or_changed
21
- sort_keys
14
+ @keys.apply_filters(params)
22
15
  paginate_keys
23
- @total_entries = @all_keys.size
16
+ @total_entries = @keys.all_keys.size
24
17
  @page_title = 'Translate'
25
18
  @show_filters = ['all', 'untranslated', 'translated']
26
19
  end
@@ -57,94 +50,15 @@ module RailsI18nterface
57
50
 
58
51
  private
59
52
 
60
-
61
- def lookup(locale, key)
62
- I18n.backend.send(:lookup, locale, key)
63
- end
64
- helper_method :lookup
65
-
66
- def filter_by_translated_or_changed
67
- params[:filter] ||= 'all'
68
- return if params[:filter] == 'all'
69
- @all_keys.reject! do |key|
70
- case params[:filter]
71
- when 'untranslated'
72
- lookup(@to_locale, key).present?
73
- when 'translated'
74
- lookup(@to_locale, key).blank?
75
- else
76
- raise "Unknown filter '#{params[:filter]}'"
77
- end
78
- end
79
- end
80
-
81
- def filter_by_key_pattern
82
- return if params[:key_pattern].blank?
83
- @all_keys.reject! do |key|
84
- case params[:key_type]
85
- when 'starts_with'
86
- if params[:key_pattern] == '.'
87
- key.match(/\./)
88
- else
89
- !key.starts_with?(params[:key_pattern])
90
- end
91
- when 'contains'
92
- key.index(params[:key_pattern]).nil?
93
- else
94
- raise "Unknown key_type '#{params[:key_type]}'"
95
- end
96
- end
97
- end
98
-
99
- def filter_by_text_pattern
100
- return if params[:text_pattern].blank?
101
- @all_keys.reject! do |key|
102
- lookup_key = lookup(@from_locale, key)
103
- case params[:text_type]
104
- when 'contains'
105
- !lookup_key.present? || !lookup_key.to_s.downcase.index(params[:text_pattern].downcase)
106
- when 'equals'
107
- !lookup_key.present? || lookup_key.to_s.downcase != params[:text_pattern].downcase
108
- else
109
- raise "Unknown text_type '#{params[:text_type]}'"
110
- end
111
- end
112
- end
113
-
114
- def sort_keys
115
- params[:sort_by] ||= 'key'
116
- case params[:sort_by]
117
- when 'key'
118
- @all_keys.sort!
119
- when 'text'
120
- @all_keys.sort! do |key1, key2|
121
- if lookup(@from_locale, key1).present? && lookup(@from_locale, key2).present?
122
- lookup(@from_locale, key1).to_s.downcase <=> lookup(@from_locale, key2).to_s.downcase
123
- elsif lookup(@from_locale, key1).present?
124
- -1
125
- else
126
- 1
127
- end
128
- end
129
- else
130
- raise "Unknown sort_by '#{params[:sort_by]}'"
131
- end
132
- end
133
-
134
53
  def paginate_keys
135
54
  params[:page] ||= 1
136
- @paginated_keys = @all_keys[offset, per_page]
55
+ @paginated_keys = @keys.all_keys[offset, @per_page]
137
56
  end
138
57
 
139
58
  def offset
140
- (params[:page].to_i - 1) * per_page
59
+ (params[:page].to_i - 1) * @per_page
141
60
  end
142
61
 
143
- def per_page
144
- params[:per_page] ? params[:per_page].to_i : 50
145
- end
146
- helper_method :per_page
147
-
148
62
  def init_translations
149
63
  I18n.backend.send(:init_translations) unless I18n.backend.initialized?
150
64
  end
@@ -153,17 +67,16 @@ module RailsI18nterface
153
67
  I18n.backend.send(:init_translations) rescue false
154
68
  end
155
69
 
156
- def default_locale
157
- I18n.default_locale
158
- end
159
-
160
70
  def set_locale
161
- session[:from_locale] ||= default_locale
162
- session[:to_locale] ||= :en
71
+ session[:from_locale] ||= I18n.default_locale
72
+ session[:to_locale] ||= I18n.default_locale
73
+ session[:per_page] ||= 50
163
74
  session[:from_locale] = params[:from_locale] if params[:from_locale].present?
164
- session[:to_locale] = params[:to_locale] if params[:to_locale].present?
75
+ session[:to_locale] = params[:to_locale] if params[:tolocale].present?
76
+ session[:per_page] = params[:per_page].to_i if params[:per_page].present?
165
77
  @from_locale = session[:from_locale].to_sym
166
78
  @to_locale = session[:to_locale].to_sym
79
+ @per_page = session[:per_page]
167
80
  end
168
81
 
169
82
  end
@@ -3,6 +3,10 @@
3
3
  module RailsI18nterface
4
4
  module TranslateHelper
5
5
 
6
+ def lookup(locale, key)
7
+ I18n.backend.send(:lookup, locale, key)
8
+ end
9
+
6
10
  def simple_filter(labels, param_name = 'filter', selected_value = nil)
7
11
  selected_value ||= params[param_name]
8
12
  filter = []
@@ -6,12 +6,64 @@
6
6
  <% if n_pages > 1 %>
7
7
  <div class="clearfix">
8
8
  <ul class="paging">
9
- <% (1..n_pages).each do |page_number| %>
10
- <% if current_page == page_number %>
11
- <li class="selected"><%= link_to(page_number, params.merge(:page => page_number), :title => "Page #{page_number}" ) %></li>
12
- <% else %>
13
- <li><%= link_to(page_number, params.merge(:page => page_number), :title => "Page #{page_number}") %></li>
14
- <% end %>
9
+ <% if n_pages > 18 %>
10
+ <% if current_page < 8 %>
11
+ <% (1..8).each do |page_number| %>
12
+ <% if current_page == page_number %>
13
+ <li class="selected">
14
+ <%= link_to(page_number, params.merge(:page => page_number), :title => "Page #{page_number}" ) %>
15
+ </li>
16
+ <% else %>
17
+ <li><%= link_to(page_number, params.merge(:page => page_number), :title => "Page #{page_number}") %></li>
18
+ <% end %>
19
+ <% end %>
20
+ <li class="gap">...</li>
21
+ <li>
22
+ <%= link_to(n_pages, params.merge(:page => n_pages), :title => "Page #{n_pages}" ) %>
23
+ </li>
24
+ <% elsif current_page > n_pages - 8 %>
25
+ <li>
26
+ <%= link_to(1, params.merge(:page => 1), :title => "Page 1" ) %>
27
+ </li>
28
+ <li class="gap">...</li>
29
+ <% ((n_pages - 8)..n_pages).each do |page_number| %>
30
+ <% if current_page == page_number %>
31
+ <li class="selected">
32
+ <%= link_to(page_number, params.merge(:page => page_number), :title => "Page #{page_number}" ) %>
33
+ </li>
34
+ <% else %>
35
+ <li><%= link_to(page_number, params.merge(:page => page_number), :title => "Page #{page_number}") %></li>
36
+ <% end %>
37
+ <% end %>
38
+ <% else %>
39
+ <li>
40
+ <%= link_to(1, params.merge(:page => 1), :title => "Page 1" ) %>
41
+ </li>
42
+ <li class="gap">...</li>
43
+ <% ((current_page - 4)..(current_page + 4)).each do |page_number| %>
44
+ <% if current_page == page_number %>
45
+ <li class="selected">
46
+ <%= link_to(page_number, params.merge(:page => page_number), :title => "Page #{page_number}" ) %>
47
+ </li>
48
+ <% else %>
49
+ <li><%= link_to(page_number, params.merge(:page => page_number), :title => "Page #{page_number}") %></li>
50
+ <% end %>
51
+ <% end %>
52
+ <li class="gap">...</li>
53
+ <li>
54
+ <%= link_to(n_pages, params.merge(:page => n_pages), :title => "Page #{n_pages}" ) %>
55
+ </li>
56
+ <% end %>
57
+ <% else %>
58
+ <% (1..n_pages).each do |page_number| %>
59
+ <% if current_page == page_number %>
60
+ <li class="selected">
61
+ <%= link_to(page_number, params.merge(:page => page_number), :title => "Page #{page_number}" ) %>
62
+ </li>
63
+ <% else %>
64
+ <li><%= link_to(page_number, params.merge(:page => page_number), :title => "Page #{page_number}") %></li>
65
+ <% end %>
66
+ <% end %>
15
67
  <% end %>
16
68
  </ul>
17
69
  </div>
@@ -12,7 +12,7 @@
12
12
  <%= link_to @page_title, root_path %></a>
13
13
  <div class="center">
14
14
  <label>Show:</label> <%= simple_filter(@show_filters).html_safe %>
15
- Found <strong><%= @total_entries %></strong> messages
15
+ Found <strong><%= @keys.all_keys.size %></strong> messages
16
16
  <%= link_to "Reload messages", translate_reload_path(params) %>
17
17
  </div>
18
18
  </h1>
@@ -21,7 +21,8 @@
21
21
  <% end %>
22
22
  </div>
23
23
  <div id="searchbox">
24
-
24
+ Per page
25
+ <%= text_field_tag(:per_page, session[:per_page], size: 2) %>
25
26
  <%= select_tag(:key_type, options_for_select([["Key contains", 'contains'], ["Key starts with", 'starts_with']], params[:key_type])) %>
26
27
  <%= text_field_tag(:key_pattern, params[:key_pattern], :size => 50, :id => "key_pattern_value", :class => "text-default") %>
27
28
 
@@ -35,15 +36,15 @@
35
36
  <% end %>
36
37
 
37
38
  <div id="namespaces">
38
- <%= build_namespace(@deep_keys).html_safe %>
39
+ <%= build_namespace(@keys.namespaces).html_safe %>
39
40
  </div>
40
41
 
41
42
  <div id="inside">
42
43
  <div class="paging">
43
- <%= render :partial => 'pagination', :locals => {:total_entries => @total_entries, :per_page => per_page} %>
44
+ <%= render :partial => 'pagination', :locals => {:total_entries => @keys.all_keys.size, :per_page => @per_page} %>
44
45
  </div>
45
46
 
46
- <% if @total_entries > 0 %>
47
+ <% if @keys.all_keys.size > 0 %>
47
48
  <%= form_tag(translate_path, method: :put) do %>
48
49
  <div>
49
50
  <%= hidden_field_tag(:filter, params[:filter], :id => "hid_filter") %>
@@ -72,11 +73,11 @@
72
73
  <p class="edit-form">
73
74
  <% if n_lines > 1 %>
74
75
  <div class="right">
75
- <% if @files[key] %>
76
+ <% if @keys.files[key] %>
76
77
  <div class="files">
77
- <div class="count">found in <%= @files[key].count %> files</div>
78
+ <div class="count">found in <%= @keys.files[key].count %> files</div>
78
79
  <div class="filelist">
79
- <div><%= @files[key].join("</div><div>").html_safe %></div>
80
+ <div><%= @keys.files[key].join("</div><div>").html_safe %></div>
80
81
  </div>
81
82
  </div>
82
83
  <% end %>
@@ -96,11 +97,11 @@
96
97
  </div>
97
98
  <% else %>
98
99
  <div class="right">
99
- <% if @files[key] %>
100
+ <% if @keys.files[key] %>
100
101
  <div class="files">
101
- <div class="count">found in <%= @files[key].count %> files</div>
102
+ <div class="count">found in <%= @keys.files[key].count %> files</div>
102
103
  <div class="filelist">
103
- <div><%= @files[key].join("</div><div>").html_safe %></div>
104
+ <div><%= @keys.files[key].join("</div><div>").html_safe %></div>
104
105
  </div>
105
106
  </div>
106
107
  <% end %>
@@ -127,7 +128,7 @@
127
128
  <% end %>
128
129
 
129
130
  <div class="paging clear">
130
- <%= render :partial => 'pagination', :locals => {:total_entries => @total_entries, :per_page => per_page} %>
131
+ <%= render :partial => 'pagination', :locals => {:total_entries => @keys.all_keys.size, :per_page => @per_page} %>
131
132
  </div>
132
133
 
133
134
  </div>
@@ -6,7 +6,6 @@ require 'rails-i18nterface/cache'
6
6
  require 'rails-i18nterface/yamlfile'
7
7
  require 'rails-i18nterface/sourcefiles'
8
8
  require 'rails-i18nterface/keys'
9
- require 'rails-i18nterface/storage'
10
9
 
11
10
  module RailsI18nterface
12
11
  end
@@ -3,38 +3,32 @@
3
3
  module RailsI18nterface
4
4
  module Cache
5
5
 
6
- def cache_save(obj, uri, options={})
6
+ def cache_save(obj, uri)
7
7
  FileUtils.rm uri if File.exists? uri
8
8
  File.open(uri, 'wb') do |f|
9
9
  Marshal.dump(obj, f)
10
10
  end
11
+ obj
11
12
  end
12
13
 
13
- def cache_load(uri, file, &process)
14
- mtime = File.mtime(file)
15
- if cached?(uri) && uptodate?(uri, file)
16
- File.open(uri) do |f|
17
- Marshal.load f
18
- end
14
+ def cache_load(uri, options={}, &process)
15
+ if File.file? uri
16
+ load uri
17
+ elsif block_given?
18
+ cache_save(yield(options), uri)
19
19
  else
20
- if block_given?
21
- obj = yield
22
- cache_save(obj, uri)
23
- File.utime(mtime, mtime, uri)
24
- obj
25
- else
26
- nil
27
- end
20
+ nil
28
21
  end
29
22
  end
30
23
 
31
- def cached?(uri)
32
- File.file? uri
33
- end
24
+ private
34
25
 
35
- def uptodate?(uri, file)
36
- File.mtime(uri) == File.mtime(file)
26
+ def load(uri)
27
+ File.open(uri) do |f|
28
+ Marshal.load f
29
+ end
37
30
  end
38
31
 
32
+
39
33
  end
40
34
  end
@@ -5,20 +5,81 @@ module RailsI18nterface
5
5
 
6
6
  include Utils
7
7
 
8
- attr_accessor :files, :keys
8
+ attr_accessor :files, :keys, :namespaces
9
9
  attr_reader :yaml_keys, :all_keys
10
10
 
11
11
  def initialize(root_dir, from, to)
12
12
  @root_dir = root_dir
13
- @files = RailsI18nterface::Sourcefiles.extract_files(root_dir)
13
+ @files = RailsI18nterface::Sourcefiles.load_files(root_dir)
14
14
  @yaml_keys = i18n_keys(I18n.default_locale)
15
15
  @from_locale = from
16
16
  @to_locale = to
17
17
  @all_keys = (@files.keys.map(&:to_s) + @yaml_keys).uniq
18
+ @namespaces = deep_sort(to_deep_hash(@all_keys))
18
19
  end
19
20
 
20
- def reload
21
- @files = RailsI18nterface::Sourcefiles.extract_files(root_dir)
21
+ def apply_filters(params)
22
+ params[:filter] ||= 'all'
23
+ filter_by_translated(params[:filter]) if params[:filter] != 'all'
24
+ filter_by_key_pattern(params[:key_type], params[:key_pattern]) if !params[:key_pattern].blank?
25
+ filter_by_text_pattern(params[:text_type], params[:text_pattern]) if !params[:text_pattern].blank?
26
+ sort_keys(params[:sort_by])
27
+ end
28
+
29
+ def filter_by_translated(filter)
30
+ @all_keys.reject! do |key|
31
+ if filter == 'untranslated'
32
+ lookup(@to_locale, key).present?
33
+ else #translated
34
+ lookup(@to_locale, key).blank?
35
+ end
36
+ end
37
+ end
38
+
39
+ def filter_by_key_pattern(type, pattern)
40
+ @all_keys.reject! do |key|
41
+ if type == 'starts_with'
42
+ if pattern == '.'
43
+ key.match(/\./)
44
+ else
45
+ !key.starts_with?(pattern)
46
+ end
47
+ else #contains
48
+ key.index(pattern).nil?
49
+ end
50
+ end
51
+ end
52
+
53
+ def filter_by_text_pattern(type, pattern)
54
+ @all_keys.reject! do |key|
55
+ lookup_key = lookup(@from_locale, key)
56
+ if type == 'contains'
57
+ !lookup_key.present? || !lookup_key.to_s.downcase.index(pattern.downcase)
58
+ else #equals
59
+ !lookup_key.present? || lookup_key.to_s.downcase != pattern.downcase
60
+ end
61
+ end
62
+ end
63
+
64
+ def sort_keys(order)
65
+ order ||= 'key'
66
+ if order == 'key'
67
+ @all_keys.sort!
68
+ else #text
69
+ @all_keys.sort! do |key1, key2|
70
+ if lookup(@from_locale, key1).present? && lookup(@from_locale, key2).present?
71
+ lookup(@from_locale, key1).to_s.downcase <=> lookup(@from_locale, key2).to_s.downcase
72
+ elsif lookup(@from_locale, key1).present?
73
+ -1
74
+ else
75
+ 1
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ def reload(root_dir)
82
+ @files = RailsI18nterface::Sourcefiles.refresh(root_dir)
22
83
  @yaml_keys = i18n_keys(I18n.default_locale)
23
84
  @all_keys = (@files.keys.map(&:to_s) + @yaml_keys).uniq
24
85
  end
@@ -47,6 +108,10 @@ module RailsI18nterface
47
108
  @files.keys.reject { |key, file| contains_key?(yaml_keys, key) }
48
109
  end
49
110
 
111
+ def lookup(locale, key)
112
+ I18n.backend.send(:lookup, locale, key)
113
+ end
114
+
50
115
  def self.translated_locales
51
116
  I18n.available_locales.reject do |locale|
52
117
  [:root, I18n.default_locale.to_sym].include?(locale)