rails-i18nterface 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +7 -0
  2. data/app/assets/javascripts/rails_i18nterface/application.js +1 -1
  3. data/app/assets/javascripts/rails_i18nterface/base.js +6 -2
  4. data/app/assets/stylesheets/rails_i18nterface/application.css +1 -1
  5. data/app/controllers/rails_i18nterface/translate_controller.rb +23 -30
  6. data/app/helpers/rails_i18nterface/translate_helper.rb +12 -13
  7. data/app/models/rails_i18nterface/translation.rb +23 -0
  8. data/app/views/rails_i18nterface/translate/index.html.erb +1 -1
  9. data/config/routes.rb +3 -3
  10. data/db/migrate/20130422115639_rename_translation_to_namespace.rb +8 -0
  11. data/lib/rails-i18nterface/keys.rb +15 -13
  12. data/lib/rails-i18nterface/log.rb +2 -2
  13. data/lib/rails-i18nterface/sourcefiles.rb +11 -6
  14. data/lib/rails-i18nterface/storage.rb +3 -3
  15. data/lib/rails-i18nterface/utils.rb +5 -9
  16. data/lib/rails-i18nterface/version.rb +1 -1
  17. data/lib/rails-i18nterface/yamlfile.rb +5 -5
  18. data/lib/rails-i18nterface.rb +7 -7
  19. data/spec/controllers/translate_controller_spec.rb +29 -29
  20. data/spec/internal/app/models/article.rb +1 -1
  21. data/spec/internal/config/routes.rb +2 -2
  22. data/spec/internal/db/combustion_test.sqlite +0 -0
  23. data/spec/internal/db/schema.rb +2 -0
  24. data/spec/internal/log/test.log +1380 -546
  25. data/spec/lib/keys_spec.rb +63 -63
  26. data/spec/lib/log_spec.rb +12 -11
  27. data/spec/lib/sourcefiles_spec.rb +11 -11
  28. data/spec/lib/storage_spec.rb +7 -7
  29. data/spec/lib/utils_spec.rb +1 -1
  30. data/spec/lib/yamlfile_spec.rb +5 -5
  31. data/spec/models/translation_spec.rb +9 -0
  32. data/spec/spec_helper.rb +19 -4
  33. metadata +84 -67
  34. data/app/models/translation.rb +0 -4
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4247f071313c096f5b0101c05d322d81469698be
4
+ data.tar.gz: dac5a3a210a13e2d6a9c084fa943262d12f519a9
5
+ SHA512:
6
+ metadata.gz: 89af3200a75dadecada9ff5d64c3aff54f542d27f7642eec4608775adfb2b534e422931ab051f9c3bd9bac769f05689ee301bde5f47f76c369826a767fd05a00
7
+ data.tar.gz: 0ed1c842b643b7adf9040c204f1db74f1259a787d6c88c09f98e22d393427944aa598868d5e0fe2f741c1bf2ee07bbb01cbf45b502c1398d775610efd99ae357
@@ -4,5 +4,5 @@
4
4
  // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
5
5
  // the compiled file.
6
6
  //
7
- //= require rails_i18nterface/ender
7
+ //= require rails_i18nterface/ender.min
8
8
  //= require rails_i18nterface/base
@@ -67,10 +67,14 @@ $.domReady(function() {
67
67
  url: 'delete/'+key,
68
68
  method: 'delete',
69
69
  success: function(resp) {
70
- alert(resp);
70
+ if (resp == error) {
71
+ alert(resp.msg);
72
+ } else {
73
+ alert(resp);
74
+ }
71
75
  },
72
76
  error: function(resp,msg) {
73
- alert("Error: "+resp.statusText);
77
+ alert("Error: " + msg);
74
78
  }
75
79
  })
76
80
  }
@@ -117,7 +117,7 @@ a{
117
117
  position: fixed;
118
118
  top: 65px;
119
119
  width: 200px;
120
- height: 100%;
120
+ height: calc(100% - 65px);
121
121
  overflow-y: scroll;
122
122
  background-color: #ddd;
123
123
  padding-top: 10px;
@@ -20,8 +20,8 @@ module RailsI18nterface
20
20
  end
21
21
 
22
22
  def destroy
23
- term = Translation.find_by_key(params[:key])
24
- if term.destroy!
23
+ term = RailsI18nterface::Translation.find_by_key(params[:key])
24
+ if term and term.destroy
25
25
  render json: 'ok'
26
26
  else
27
27
  render json: 'error'
@@ -31,46 +31,35 @@ module RailsI18nterface
31
31
  def load_db_translations
32
32
  @versions = {}
33
33
  @dbvalues = {@to_locale => {}}
34
- (Translation.where(:locale => @to_locale) || []).each { |translation|
34
+ (RailsI18nterface::Translation.where(:locale => @to_locale) || []).each do |translation|
35
35
  @versions[translation.key] = translation.updated_at.to_i
36
36
  yaml_value = I18n.backend.send(:lookup, @to_locale, translation.key)
37
- if (yaml_value and translation.value != yaml_value)
37
+ if yaml_value && translation.value != yaml_value
38
38
  translation.value = yaml_value
39
- Translation.where(key: translation.key).first.update_attribute(:value, yaml_value)
39
+ RailsI18nterface::Translation.where(key: translation.key).first.update_attribute(:value, yaml_value)
40
40
  end
41
41
  @dbvalues[@to_locale][translation.key] = translation.value
42
42
  @keys << translation.key
43
- }
43
+ end
44
44
  @keys.uniq!
45
45
  end
46
46
 
47
47
  def export
48
48
  locale = params[:locale].to_sym
49
49
  keys = {locale => I18n.backend.send(:translations)[locale] || {}}
50
- Translation.where(:locale => @to_locale).each { |translation|
50
+ RailsI18nterface::Translation.where(:locale => @to_locale).each do |translation|
51
51
  next if !translation.value or translation.value == ''
52
- set_nested(keys[locale], translation.key.split("."), translation.value)
53
- }
52
+ set_nested(keys[locale], translation.key.split('.'), translation.value)
53
+ end
54
54
  remove_blanks keys
55
- yaml = RailsI18nterface::Yamlfile.new.keys_to_yaml(keys)
55
+ puts keys
56
+ yaml = RailsI18nterface::Yamlfile.new(nil).keys_to_yaml(keys)
56
57
  response.headers['Content-Disposition'] = "attachment; filename=#{locale}.yml"
57
58
  render :text => yaml
58
59
  end
59
60
 
60
61
  def update
61
- params[:key].each { |k, v|
62
- t = Translation.where(:key => k, :locale => @to_locale).first
63
- unless t
64
- t = Translation.new
65
- t.key = k
66
- t.locale = @to_locale
67
- end
68
- next if t.value == v
69
- if params[:version][k].to_i == t.updated_at.to_i
70
- t.value = v
71
- end
72
- t.save
73
- }
62
+ RailsI18nterface::Translation.update(@to_locale, params[:key])
74
63
  if I18n.backend.respond_to? :store_translations
75
64
  I18n.backend.store_translations(@to_locale, RailsI18nterface::Keys.to_deep_hash(params[:key]))
76
65
  end
@@ -83,7 +72,7 @@ module RailsI18nterface
83
72
 
84
73
  def reload
85
74
  RailsI18nterface::Keys.files = nil
86
- redirect_to :action => 'index'
75
+ redirect_to root_path(params.slice(:filter, :sort_by, :key_type, :key_pattern, :text_type, :text_pattern))
87
76
  end
88
77
 
89
78
  private
@@ -93,9 +82,12 @@ module RailsI18nterface
93
82
  keys = (@files.keys.map(&:to_s) + RailsI18nterface::Keys.new.i18n_keys(@from_locale)).uniq
94
83
  keys.reject! do |key|
95
84
  from_text = lookup(@from_locale, key)
96
- # When translating from one language to another, make sure there is a text to translate from.
97
- # Always exclude non string translation objects as we don't support editing them in the UI.
98
- (@from_locale != @to_locale && !from_text.present?) || (from_text.present? && !from_text.is_a?(String))
85
+ # When translating from one language to another,
86
+ # make sure there is a text to translate from.
87
+ # Always exclude non string translation objects
88
+ # as we don't support editing them in the UI.
89
+ (@from_locale != @to_locale && !from_text.present?) ||
90
+ (from_text.present? && !from_text.is_a?(String))
99
91
  end
100
92
  end
101
93
 
@@ -142,11 +134,12 @@ module RailsI18nterface
142
134
  def filter_by_text_pattern
143
135
  return if params[:text_pattern].blank?
144
136
  @keys.reject! do |key|
137
+ lookup_key = lookup(@from_locale, key)
145
138
  case params[:text_type]
146
139
  when 'contains'
147
- !lookup(@from_locale, key).present? || !lookup(@from_locale, key).to_s.downcase.index(params[:text_pattern].downcase)
140
+ !lookup_key.present? || !lookup_key.to_s.downcase.index(params[:text_pattern].downcase)
148
141
  when 'equals'
149
- !lookup(@from_locale, key).present? || lookup(@from_locale, key).to_s.downcase != params[:text_pattern].downcase
142
+ !lookup_key.present? || lookup_key.to_s.downcase != params[:text_pattern].downcase
150
143
  else
151
144
  raise "Unknown text_type '#{params[:text_type]}'"
152
145
  end
@@ -211,7 +204,7 @@ module RailsI18nterface
211
204
  def old_from_text(key)
212
205
  return @old_from_text[key] if @old_from_text && @old_from_text[key]
213
206
  @old_from_text = {}
214
- text = key.split(".").inject(log_hash) do |hash, k|
207
+ text = key.split(".").reduce(log_hash) do |hash, k|
215
208
  hash ? hash[k] : nil
216
209
  end
217
210
  @old_from_text[key] = text
@@ -14,20 +14,18 @@ module RailsI18nterface
14
14
  filter << "<i>#{label}</i>"
15
15
  else
16
16
  link_params = params.merge({param_name.to_s => type})
17
- link_params.merge!({"page" => nil}) if param_name.to_s != "page"
17
+ link_params.merge!({'page' => nil}) if param_name.to_s != 'page'
18
18
  filter << link_to(label, link_params)
19
19
  end
20
20
  end
21
- filter.join(" | ")
21
+ filter.join(' | ')
22
22
  end
23
23
 
24
24
  def n_lines(text, line_size)
25
25
  n_lines = 1
26
26
  if text.present?
27
27
  n_lines = text.split("\n").size
28
- if n_lines == 1 && text.length > line_size
29
- n_lines = text.length / line_size + 1
30
- end
28
+ n_lines = text.length / line_size + 1 if n_lines == 1 && text.length > line_size
31
29
  end
32
30
  n_lines
33
31
  end
@@ -36,14 +34,14 @@ module RailsI18nterface
36
34
  out = '<ul>'
37
35
  dirs = {}
38
36
  root = []
39
- h.each do |k,v|
37
+ h.each do |k, v|
40
38
  if v.is_a? Hash
41
39
  dirs[k] = v
42
40
  else
43
41
  root << k
44
42
  end
45
43
  end
46
- out << "<li class=\"dir\"><span class=\"display\" data-id=\".\"></span>ROOT"
44
+ out << '<li class="dir"><span class="display" data-id="."></span>ROOT'
47
45
  out << " <span class=\"num\">(#{root.length})</span>"
48
46
  out << '<ul>'
49
47
  root.each do |key|
@@ -51,19 +49,20 @@ module RailsI18nterface
51
49
  end
52
50
  out << '</ul>'
53
51
  out << '</ul>'
54
- out << list_namespace('',dirs)
52
+ out << list_namespace('', dirs)
55
53
  end
56
54
 
57
- def list_namespace(k,h)
55
+ def list_namespace(k, h)
58
56
  out = '<ul>'
59
57
  k != '' && k += '.'
60
- h.each do |key,val|
58
+ h.each do |key, val|
61
59
  if val.is_a? Hash
62
- out << "<li class=\"dir\"><span class=\"display\" data-id=\"#{k+key.to_s}\"></span>#{key}"
60
+ out << "<li class=\"dir\"><span class=\"display\" data-id=\"#{k + key.to_s}\"></span>"
61
+ out << key.to_s
63
62
  out << " <span class=\"num\">(#{val.length})</span>"
64
- out << list_namespace(k+key.to_s,val)
63
+ out << list_namespace(k + key.to_s, val)
65
64
  else
66
- out << "<li class=\"item\" data-id=\"#{k+key.to_s}\">#{key}"
65
+ out << "<li class=\"item\" data-id=\"#{k + key.to_s}\">#{key}"
67
66
  end
68
67
  out << '</li>'
69
68
  end
@@ -0,0 +1,23 @@
1
+ class RailsI18nterface::Translation < ActiveRecord::Base
2
+
3
+ def self.update(locale, keys)
4
+ keys.each do |k, v|
5
+ t = RailsI18nterface::Translation.where(key: k, locale: locale).first
6
+ if t
7
+ if !v || v == ''
8
+ t.destroy!
9
+ next
10
+ elsif t.value != v
11
+ t.value = v
12
+ t.save!
13
+ end
14
+ elsif v != ''
15
+ t = RailsI18nterface::Translation.new
16
+ t.key = k
17
+ t.locale = locale
18
+ t.save!
19
+ end
20
+ end
21
+ end
22
+
23
+ end
@@ -19,7 +19,7 @@
19
19
  <div class="center">
20
20
  <label>Show:</label> <%= simple_filter(show_filters).html_safe %>
21
21
  Found <strong><%= @total_entries %></strong> messages
22
- <%= link_to "Reload messages", translate_reload_path %>
22
+ <%= link_to "Reload messages", translate_reload_path(params) %>
23
23
  </div>
24
24
 
25
25
  </h1>
data/config/routes.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  RailsI18nterface::Engine.routes.draw do
2
- root :to => 'translate#index'
2
+ root to: 'translate#index'
3
3
 
4
4
  put '/translate' => 'translate#update'
5
- get '/reload' => 'translate#reload', :as => 'translate_reload'
6
- get '/export' => 'translate#export', :as => 'translate_export'
5
+ get '/reload' => 'translate#reload', as: 'translate_reload'
6
+ get '/export' => 'translate#export', as: 'translate_export'
7
7
  delete '/delete/*key' => 'translate#destroy', format: false
8
8
  end
@@ -0,0 +1,8 @@
1
+ class RenameTranslationToNamespace < ActiveRecord::Migration
2
+ def self.up
3
+ rename_table :translations, :rails_i18nterface_translations
4
+ end
5
+ def self.down
6
+ rename_table :rails_i18nterface_translations, :translations
7
+ end
8
+ end
@@ -5,15 +5,15 @@ module RailsI18nterface
5
5
 
6
6
  # Allows keys extracted from lookups in files to be cached
7
7
  def self.files
8
- @@files ||= new.files
8
+ @files ||= new.files
9
9
  end
10
10
 
11
11
  def self.names
12
- @@names || new.names
12
+ @names || new.names
13
13
  end
14
14
  # Allows flushing of the files cache
15
15
  def self.files=(files)
16
- @@files = files
16
+ @files = files
17
17
  end
18
18
 
19
19
  def files
@@ -37,7 +37,7 @@ module RailsI18nterface
37
37
  end
38
38
 
39
39
  def untranslated_keys
40
- self.class.translated_locales.inject({}) do |missing, locale|
40
+ self.class.translated_locales.reduce({}) do |missing, locale|
41
41
  missing[locale] = i18n_keys(I18n.default_locale).map do |key|
42
42
  I18n.backend.send(:lookup, locale, key).nil? ? key : nil
43
43
  end.compact
@@ -48,14 +48,16 @@ module RailsI18nterface
48
48
  def missing_keys
49
49
  locale = I18n.default_locale
50
50
  yaml_keys = {}
51
- yaml_keys = Storage.file_paths(locale).inject({}) do |keys, path|
51
+ yaml_keys = Storage.file_paths(locale).reduce({}) do |keys, path|
52
52
  keys = keys.deep_merge(Yamlfile.new(path).read[locale.to_s])
53
53
  end
54
54
  files.reject { |key, file| self.class.contains_key?(yaml_keys, key) }
55
55
  end
56
56
 
57
57
  def self.translated_locales
58
- I18n.available_locales.reject { |locale| [:root, I18n.default_locale.to_sym].include?(locale) }
58
+ I18n.available_locales.reject do |locale|
59
+ [:root, I18n.default_locale.to_sym].include?(locale)
60
+ end
59
61
  end
60
62
 
61
63
  # Checks if a nested hash contains the keys in dot separated I18n key.
@@ -76,10 +78,10 @@ module RailsI18nterface
76
78
  # contains_key?("foo.bar.baz.bla", key) # => false
77
79
  #
78
80
  def self.contains_key?(hash, key)
79
- keys = key.to_s.split(".")
81
+ keys = key.to_s.split('.')
80
82
  return false if keys.empty?
81
- !keys.inject(HashWithIndifferentAccess.new(hash)) do |memo, key|
82
- memo.is_a?(Hash) ? memo.try(:[], key) : nil
83
+ !keys.reduce(HashWithIndifferentAccess.new(hash)) do |memo, k|
84
+ memo.is_a?(Hash) ? memo.try(:[], k) : nil
83
85
  end.nil?
84
86
  end
85
87
 
@@ -98,10 +100,10 @@ module RailsI18nterface
98
100
  # {'pressrelease.label.one' => "Pressmeddelande"}
99
101
  #
100
102
  def self.to_shallow_hash(hash)
101
- hash.inject({}) do |shallow_hash, (key, value)|
103
+ hash.reduce({}) do |shallow_hash, (key, value)|
102
104
  if value.is_a?(Hash)
103
105
  to_shallow_hash(value).each do |sub_key, sub_value|
104
- shallow_hash[[key, sub_key].join(".")] = sub_value
106
+ shallow_hash[[key, sub_key].join('.')] = sub_value
105
107
  end
106
108
  else
107
109
  shallow_hash[key.to_s] = value
@@ -124,10 +126,10 @@ module RailsI18nterface
124
126
  # }
125
127
  # }
126
128
  def self.to_deep_hash(hash)
127
- hash.inject({}) do |deep_hash, (key, value)|
129
+ hash.reduce({}) do |deep_hash, (key, value)|
128
130
  keys = key.to_s.split('.').reverse
129
131
  leaf_key = keys.shift
130
- key_hash = keys.inject({leaf_key.to_sym => value}) { |hash, key| {key.to_sym => hash} }
132
+ key_hash = keys.reduce({leaf_key.to_sym => value}) { |h, k| { k.to_sym => h } }
131
133
  deep_merge!(deep_hash, key_hash)
132
134
  deep_hash
133
135
  end
@@ -24,14 +24,14 @@ module RailsI18nterface
24
24
  end
25
25
 
26
26
  def from_texts
27
- file.deep_stringify_keys(Keys.to_deep_hash(keys.inject({}) do |hash, key|
27
+ file.deep_stringify_keys(Keys.to_deep_hash(keys.reduce({ }) do |hash, key|
28
28
  hash[key] = I18n.backend.send(:lookup, from_locale, key)
29
29
  hash
30
30
  end))
31
31
  end
32
32
 
33
33
  def file_path
34
- File.join(Rails.root, "config", "locales", "log", "from_#{from_locale}_to_#{to_locale}.yml")
34
+ File.join(Rails.root, 'config', 'locales', 'log', "from_#{from_locale}_to_#{to_locale}.yml")
35
35
  end
36
36
  end
37
37
  end
@@ -2,9 +2,14 @@ module RailsI18nterface
2
2
  module Sourcefiles
3
3
 
4
4
  def self.extract_files
5
- i18n_lookup_pattern = /\b(?:I18n\.t|I18n\.translate|t)(?:\s|\():?(?:'|")(\.?[a-z0-9_\.]+)(?:'|")/
5
+ i18n_lookup_pattern = /\b
6
+ (?:I18n\.t|I18n\.translate|t)
7
+ (?:\s|\():?(?:'|")
8
+ (\.?[a-z0-9_\.]+)
9
+ (?:'|")
10
+ /x
6
11
  f = {}
7
- self.files_to_scan.inject(HashWithIndifferentAccess.new) do |files, file|
12
+ self.files_to_scan.reduce(HashWithIndifferentAccess.new) do |files, file|
8
13
  f = files.merge! self.populate_keys(file, i18n_lookup_pattern)
9
14
  end
10
15
  f.merge self.extract_activerecords
@@ -30,8 +35,8 @@ module RailsI18nterface
30
35
  end
31
36
 
32
37
  def self.files_to_scan
33
- Dir.glob(File.join(Storage.root_dir, "{app,config,lib}", "**","*.{rb,erb,haml,slim,rhtml}")) +
34
- Dir.glob(File.join(Storage.root_dir, "{public,app/assets}", "javascripts", "**","*.{js,coffee}"))
38
+ Dir.glob(File.join(Storage.root_dir, '{app,config,lib}', '**', '*.{rb,erb,haml,slim,rhtml}')) +
39
+ Dir.glob(File.join(Storage.root_dir, '{public,app/assets}', 'javascripts', '**', '*.{js,coffee}'))
35
40
  end
36
41
 
37
42
  def self.extract_activerecords
@@ -43,14 +48,14 @@ module RailsI18nterface
43
48
  while matchdata != nil
44
49
  model = matchdata[1]
45
50
  files["activerecord.models.#{model}"] = ['db/schema.rb']
46
- files.merge!(self.extract_attributes(model,matchdata[2]))
51
+ files.merge!(self.extract_attributes(model, matchdata[2]))
47
52
  matchdata = regex.match(matchdata.post_match)
48
53
  end
49
54
  end
50
55
  files
51
56
  end
52
57
 
53
- def self.extract_attributes(model,txt)
58
+ def self.extract_attributes(model, txt)
54
59
  files = {}
55
60
  regex = Regexp.new('\s*t\.[-_0-9a-z]*\s*"([^"]*?)"')
56
61
  matchdata = regex.match(txt)
@@ -11,7 +11,7 @@ module RailsI18nterface
11
11
  end
12
12
 
13
13
  def self.file_paths(locale)
14
- Dir.glob(File.join(root_dir, "config", "locales", "**","#{locale}.yml"))
14
+ Dir.glob(File.join(root_dir, 'config', 'locales', '**', "#{locale}.yml"))
15
15
  end
16
16
 
17
17
  def self.root_dir
@@ -21,11 +21,11 @@ module RailsI18nterface
21
21
  private
22
22
 
23
23
  def keys
24
- {locale => I18n.backend.send(:translations)[locale]}
24
+ { locale => I18n.backend.send(:translations)[locale] }
25
25
  end
26
26
 
27
27
  def file_path
28
- File.join(self.class.root_dir, "config", "locales", "#{locale}.yml")
28
+ File.join(self.class.root_dir, 'config', 'locales', "#{locale}.yml")
29
29
  end
30
30
  end
31
31
  end