i18n-active_record 0.1.2 → 0.2.0

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: 071f03d4c1cb846851214370883ecf1e2d8205f9
4
- data.tar.gz: 3aabc622ff195919dfc1062767d74e77927a1af4
3
+ metadata.gz: d4702ca87d86eb911ba7bb1a625d3db8c9837120
4
+ data.tar.gz: 035751e1098ef51b85d91d85edcc4279691386e1
5
5
  SHA512:
6
- metadata.gz: f1907bf7d94ac2c8a2901056973ac4cddbf036bc4d2b76b4e31d60842409d7a6b4b3f6fb5534bb9b42d803506b9e4f4abc5e4c8914998a218c495198cb421fc1
7
- data.tar.gz: 1ec12a7fd71286c3dd408fce0aea98d7bc3f4d01220f69156fc3f6cdaf3660bb9cb9262f8ab7fc0fea4d81011da1a2a3be934d4f9af9e4f99b7cf4b7c672060e
6
+ metadata.gz: 0ef662d17a2c8d1ae4e02a31b0e55892357835aaaa6bf8deecae803b21d835015b88b5c13c970f64b2310430e4d11cf6ee074ef364dd8ef76b5e199b041eaf62
7
+ data.tar.gz: a1ea9dc58acb19f24fd1f22ddcc8cbcb86351ac1b6a5c89baae0ac51599cf91a130fd0194621dd8e40a4019537173b44c60c590f6e46c51d5a7dc462749ab62c
@@ -78,6 +78,12 @@ h2. Usage
78
78
 
79
79
  You can now use @I18n.t('Your String')@ to lookup translations in the database.
80
80
 
81
+ h2. Missing Translations -> Interpolations
82
+
83
+ The interpolations field in the Translations table is used by I18n::Backend::ActiveRecord::Missing to store the interpolations seen the first time this Translation was requested. This will help translators understand what interpolations to expect, and thus to include when providing the translations.
84
+
85
+ The interpolations field is otherwise unused since the "value" in Translation.value is actually used for interpolation during actual translations.
86
+
81
87
  h2. Examples
82
88
 
83
89
  * http://collectiveidea.com/blog/archives/2016/05/31/beyond-yml-files-dynamic-translations/
data/Rakefile CHANGED
@@ -34,7 +34,7 @@ namespace :bundle do
34
34
  end
35
35
 
36
36
  task :install_all do
37
- [nil, '3', '4', 'master'].each do |ar|
37
+ [nil, '3', '4', '5', 'master'].each do |ar|
38
38
  opt = ar && "AR=#{ar}"
39
39
  execute "rake bundle:install #{opt}"
40
40
  end
@@ -1,5 +1,5 @@
1
1
  module I18n
2
2
  module ActiveRecord
3
- VERSION = '0.1.2'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -56,15 +56,24 @@ module I18n
56
56
  elsif result.first.key == key
57
57
  result.first.value
58
58
  else
59
- chop_range = (key.size + FLATTEN_SEPARATOR.size)..-1
60
- result = result.inject({}) do |hash, r|
61
- hash[r.key.slice(chop_range)] = r.value
62
- hash
59
+ result = result.inject({}) do |hash, translation|
60
+ hash.deep_merge build_translation_hash_by_key(key, translation)
63
61
  end
64
62
  result.deep_symbolize_keys
65
63
  end
66
64
  end
67
65
 
66
+ def build_translation_hash_by_key(lookup_key, translation)
67
+ hash = {}
68
+ chop_range = (lookup_key.size + FLATTEN_SEPARATOR.size)..-1
69
+ translation_nested_keys = translation.key.slice(chop_range).split(FLATTEN_SEPARATOR)
70
+ translation_nested_keys.each.with_index.inject(hash) do |iterator, (key, index)|
71
+ iterator[key] = translation_nested_keys[index + 1] ? {} : translation.value
72
+ iterator[key]
73
+ end
74
+ hash
75
+ end
76
+
68
77
  # For a key :'foo.bar.baz' return ['foo', 'foo.bar', 'foo.bar.baz']
69
78
  def expand_keys(key)
70
79
  key.to_s.split(FLATTEN_SEPARATOR).inject([]) do |keys, key|
@@ -76,4 +76,16 @@ class I18nBackendActiveRecordTest < I18n::TestCase
76
76
 
77
77
  assert I18n::Backend::ActiveRecord.config.cleanup_with_destroy
78
78
  end
79
+
80
+ test "fetching subtree of translations" do
81
+ I18n::Backend::ActiveRecord::Translation.delete_all
82
+ I18n.backend.store_translations(:en, foo: { bar: { fizz: 'buzz', spuz: 'zazz' }, baz: { fizz: 'buzz' } })
83
+ assert_equal I18n.t(:foo), { bar: { fizz: 'buzz', spuz: 'zazz' }, baz: { fizz: 'buzz' } }
84
+ end
85
+
86
+ test "build_translation_hash_by_key" do
87
+ translation = I18n::Backend::ActiveRecord::Translation.new(value: 'translation', key: 'foo.bar.fizz.buzz')
88
+ expected_hash = { 'bar' => { 'fizz' => { 'buzz' => 'translation' } } }
89
+ assert_equal I18n.backend.send(:build_translation_hash_by_key, 'foo', translation), expected_hash
90
+ end
79
91
  end
@@ -19,7 +19,7 @@ rescue ::ActiveRecord::ConnectionNotEstablished
19
19
  when 'postgres'
20
20
  ::ActiveRecord::Base.establish_connection adapter: 'postgresql', database: 'i18n_unittest', username: ENV['PG_USER'] || 'i18n', password: '', host: 'localhost'
21
21
  when 'mysql'
22
- ::ActiveRecord::Base.establish_connection adapter: 'mysql', database: 'i18n_unittest', username: 'root', password: '', host: 'localhost'
22
+ ::ActiveRecord::Base.establish_connection adapter: 'mysql2', database: 'i18n_unittest', username: 'root', password: '', host: 'localhost'
23
23
  else
24
24
  ::ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
25
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-active_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sven Fuchs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-24 00:00:00.000000000 Z
11
+ date: 2017-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -79,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
79
  version: '0'
80
80
  requirements: []
81
81
  rubyforge_project: "[none]"
82
- rubygems_version: 2.5.2
82
+ rubygems_version: 2.6.7
83
83
  signing_key:
84
84
  specification_version: 4
85
85
  summary: I18n ActiveRecord backend