i18n-active_record 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.textile +6 -0
- data/Rakefile +1 -1
- data/lib/i18n/active_record/version.rb +1 -1
- data/lib/i18n/backend/active_record.rb +13 -4
- data/test/active_record_test.rb +12 -0
- data/test/test_helper.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d4702ca87d86eb911ba7bb1a625d3db8c9837120
|
4
|
+
data.tar.gz: 035751e1098ef51b85d91d85edcc4279691386e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ef662d17a2c8d1ae4e02a31b0e55892357835aaaa6bf8deecae803b21d835015b88b5c13c970f64b2310430e4d11cf6ee074ef364dd8ef76b5e199b041eaf62
|
7
|
+
data.tar.gz: a1ea9dc58acb19f24fd1f22ddcc8cbcb86351ac1b6a5c89baae0ac51599cf91a130fd0194621dd8e40a4019537173b44c60c590f6e46c51d5a7dc462749ab62c
|
data/README.textile
CHANGED
@@ -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
@@ -56,15 +56,24 @@ module I18n
|
|
56
56
|
elsif result.first.key == key
|
57
57
|
result.first.value
|
58
58
|
else
|
59
|
-
|
60
|
-
|
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|
|
data/test/active_record_test.rb
CHANGED
@@ -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
|
data/test/test_helper.rb
CHANGED
@@ -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: '
|
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.
|
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:
|
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.
|
82
|
+
rubygems_version: 2.6.7
|
83
83
|
signing_key:
|
84
84
|
specification_version: 4
|
85
85
|
summary: I18n ActiveRecord backend
|