/
active_record.rb
59 lines (50 loc) · 1.68 KB
/
active_record.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require 'i18n/backend/base'
require 'i18n/backend/active_record/translation'
module I18n
module Backend
class ActiveRecord
include Base
def reload!
end
def store_translations(locale, data)
separator = I18n.default_separator # TODO allow to pass as an option?
wind_keys(data).each do |key, v|
Translation.locale(locale).lookup(expand_keys(key, separator), separator).delete_all
Translation.create(:locale => locale.to_s, :key => key, :value => v)
end
end
def available_locales
begin
Translation.available_locales
rescue ::ActiveRecord::StatementInvalid
[]
end
end
protected
def lookup(locale, key, scope = [], separator = nil)
return unless key
separator ||= I18n.default_separator
key = (Array(scope) + Array(key)).join(separator)
result = Translation.locale(locale).lookup(key, separator).all
if result.empty?
return nil
elsif result.first.key == key
return result.first.value
else
chop_range = (key.size + separator.size)..-1
result = result.inject({}) do |hash, r|
hash[r.key.slice(chop_range)] = r.value
hash
end
deep_symbolize_keys(unwind_keys(result))
end
end
# For a key :'foo.bar.baz' return ['foo', 'foo.bar', 'foo.bar.baz']
def expand_keys(key, separator = I18n.default_separator)
key.to_s.split(separator).inject([]) do |keys, key|
keys << [keys.last, key].compact.join(separator)
end
end
end
end
end