i18n-active_record 0.3.0 → 0.4.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
  SHA256:
3
- metadata.gz: 437efadf4380a5cd99eccb3c468bf25f0aa0343808e89e0dfad9ac676b31743f
4
- data.tar.gz: 5080254a77103719b02c55127da824ad505e6d76d0ef7b57a8773a7b08db84ca
3
+ metadata.gz: 7aeed8d25f893fefde754e0836bdc6338529b6313a5809d4312fb7fd30087f3a
4
+ data.tar.gz: e924f6936a5f956f6c62c095374b76d1c84e504d65bdf07f4463d2659cb7c50d
5
5
  SHA512:
6
- metadata.gz: 4f5823aa96600903b72bb9a6203a6f6b065482b8cc78bcf97df884758893d69a4d1b9e55fe973830027ebc9e19a72c431410bed38ec0e05c4854a59cca1d17f1
7
- data.tar.gz: ea61b155163a0eecd7e2ea10317d761b9add3b3c0ad4ef7ce713a87cfa7abc250ae25d21753c66a12f4db2921c4749d77b20f2fa1e2003ad5e49906b3cd71e9f
6
+ metadata.gz: 100bd6c09aa1307292e2f751d45b601e47837e58d708bf881b9e0ba6d02e1ec1d8911d5fa4c3d4f0d6d9409e5997d90772e23e1fdbf86e0e4ed6b1af48101988
7
+ data.tar.gz: 29bd66389ac95bf5afa7f97f89e9676b6bb83be6abaa02876e7577dff6c1a83b7f91fbe70f6166841e5924b403e9449c071338076ef8f3c5d1189bd8f3e9f29a
@@ -3,7 +3,7 @@ h1. I18n::Backend::ActiveRecord
3
3
  !https://travis-ci.org/svenfuchs/i18n-active_record.svg?branch=master!:https://travis-ci.org/svenfuchs/i18n-active_record
4
4
 
5
5
  This repository contains the I18n ActiveRecord backend and support code that has been extracted from the "I18n":http://github.com/svenfuchs/i18n.
6
- It is fully compatible with Rails 3, 4 and 5
6
+ It is fully compatible with Rails 3, 4, 5 and 6.
7
7
 
8
8
  h2. Installation
9
9
 
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', '5', 'master'].each do |ar|
37
+ [nil, '3', '4', '5', '6', 'master'].each do |ar|
38
38
  opt = ar && "AR=#{ar}"
39
39
  execute "rake bundle:install #{opt}"
40
40
  end
@@ -19,7 +19,7 @@ module I18n
19
19
  end
20
20
 
21
21
  def create_migrations
22
- migration_template 'migration.rb.erb', 'db/migrate/create_translations.rb'
22
+ migration_template 'migration.rb.erb', "db/migrate/create_#{table_name}.rb"
23
23
  end
24
24
  end
25
25
  end
@@ -1,4 +1,4 @@
1
- class Create<%= name.classify %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version.to_s %>]
1
+ class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
2
2
  def self.up
3
3
  create_table :<%= name.tableize %> do |t|
4
4
  t.string :locale
@@ -1,5 +1,5 @@
1
1
  module I18n
2
2
  module ActiveRecord
3
- VERSION = '0.3.0'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
@@ -45,6 +45,24 @@ module I18n
45
45
  end
46
46
  end
47
47
 
48
+ def reload!
49
+ @translations = nil
50
+ self
51
+ end
52
+
53
+ def initialized?
54
+ !@translations.nil?
55
+ end
56
+
57
+ def init_translations
58
+ @translations = Translation.to_hash
59
+ end
60
+
61
+ def translations(do_init: false)
62
+ init_translations if do_init || !initialized?
63
+ @translations ||= {}
64
+ end
65
+
48
66
  protected
49
67
 
50
68
  def lookup(locale, key, scope = [], options = {})
@@ -75,6 +75,18 @@ module I18n
75
75
  def available_locales
76
76
  Translation.select('DISTINCT locale').to_a.map { |t| t.locale.to_sym }
77
77
  end
78
+
79
+ def to_hash
80
+ Translation.all.each.with_object({}) do |t, memo|
81
+ locale_hash = (memo[t.locale.to_sym] ||= {})
82
+ keys = t.key.split('.')
83
+ keys.each.with_index.inject(locale_hash) do |iterator, (key_part, index)|
84
+ key = key_part.to_sym
85
+ iterator[key] = keys[index + 1] ? (iterator[key] || {}) : t.value
86
+ iterator[key]
87
+ end
88
+ end
89
+ end
78
90
  end
79
91
 
80
92
  def interpolates?(key)
@@ -109,4 +109,28 @@ class I18nBackendActiveRecordTest < I18n::TestCase
109
109
 
110
110
  assert_equal "translation missing: en.no key", I18n.t('.')
111
111
  end
112
+
113
+ test "intially unitinitialized" do
114
+ refute I18n.backend.initialized?
115
+ I18n.backend.init_translations
116
+ assert I18n.backend.initialized?
117
+ I18n.backend.reload!
118
+ refute I18n.backend.initialized?
119
+ I18n.backend.init_translations
120
+ assert I18n.backend.initialized?
121
+ end
122
+
123
+ test "translations returns all translations" do
124
+ expected_hash = { :en => { :foo => { :bar => 'bar', :baz => 'baz' } } }
125
+ I18n.backend.init_translations
126
+ assert_equal expected_hash, I18n.backend.send(:translations)
127
+ assert I18n.backend.initialized?
128
+ end
129
+
130
+ test "translations initialized with do_init argument" do
131
+ expected_hash = { :en => { :foo => { :bar => 'bar', :baz => 'baz' } } }
132
+ refute I18n.backend.initialized?
133
+ assert_equal expected_hash, I18n.backend.send(:translations, { do_init: true })
134
+ assert I18n.backend.initialized?
135
+ end
112
136
  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.3.0
4
+ version: 0.4.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: 2019-09-27 00:00:00.000000000 Z
11
+ date: 2020-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n