releaf-i18n_database 1.1.19 → 2.0.1

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.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/config/releaf_i18n_database_manifest.js +2 -0
  3. data/app/controllers/releaf/i18n_database/translations_controller.rb +1 -1
  4. data/app/lib/releaf/i18n_database/parse_spreadsheet_translations.rb +2 -2
  5. data/app/lib/releaf/i18n_database/translations_store.rb +1 -1
  6. data/app/lib/releaf/i18n_database/translations_utilities.rb +1 -1
  7. data/app/models/releaf/i18n_database/i18n_entry.rb +1 -1
  8. data/app/views/releaf/i18n_database/translations/_form_fields.haml +1 -1
  9. data/lib/releaf-i18n_database.rb +1 -1
  10. data/lib/releaf/i18n_database/engine.rb +2 -2
  11. metadata +17 -63
  12. data/spec/builders/translations/builder_common_spec.rb +0 -39
  13. data/spec/builders/translations/edit_builder_spec.rb +0 -93
  14. data/spec/builders/translations/index_builder_spec.rb +0 -96
  15. data/spec/builders/translations/table_builder_spec.rb +0 -68
  16. data/spec/controllers/i18n_backend/translations_controller_spec.rb +0 -148
  17. data/spec/features/translations_spec.rb +0 -426
  18. data/spec/fixtures/all_translations_exported.xlsx +0 -0
  19. data/spec/fixtures/invalid.xls +0 -3
  20. data/spec/fixtures/invalid.xlsx +0 -3
  21. data/spec/fixtures/time.formats.xlsx +0 -0
  22. data/spec/fixtures/translations_import.xlsx +0 -0
  23. data/spec/fixtures/unsupported_import_file.png +0 -0
  24. data/spec/lib/releaf/i18n_database/backend_spec.rb +0 -198
  25. data/spec/lib/releaf/i18n_database/configuration_spec.rb +0 -13
  26. data/spec/lib/releaf/i18n_database/humanize_missing_translations_spec.rb +0 -24
  27. data/spec/lib/releaf/i18n_database/parse_spreadsheet_translations_spec.rb +0 -151
  28. data/spec/lib/releaf/i18n_database/translations_store_spec.rb +0 -530
  29. data/spec/lib/releaf/i18n_database/translations_utilities_spec.rb +0 -175
  30. data/spec/models/i18n_database/i18n_entry_spec.rb +0 -50
  31. data/spec/models/i18n_database/i18n_entry_translation_spec.rb +0 -9
@@ -1,3 +0,0 @@
1
- NOT REALLY
2
- A SPREADSHEET
3
- FILE
@@ -1,3 +0,0 @@
1
- NOT REALLY
2
- A SPREADSHEET
3
- FILE
@@ -1,198 +0,0 @@
1
- require "rails_helper"
2
-
3
- describe Releaf::I18nDatabase::Backend do
4
- let(:translations_store){ Releaf::I18nDatabase::TranslationsStore.new }
5
-
6
- describe ".configure_component" do
7
- it "adds new `Releaf::I18nDatabase::Configuration` configuration with default config" do
8
- stub_const("Releaf::I18nDatabase::Backend::DEFAULT_CONFIG", a: :b)
9
- allow(Releaf::I18nDatabase::Configuration).to receive(:new)
10
- .with(a: :b).and_return("_new")
11
- expect(Releaf.application.config).to receive(:add_configuration).with("_new")
12
- described_class.configure_component
13
- end
14
- end
15
-
16
- describe ".reset_cache" do
17
- it "reset translations cache to backend instance" do
18
- subject.translations_cache = :x
19
- allow(described_class).to receive(:backend_instance).and_return(subject)
20
- expect{ described_class.reset_cache }.to change{ subject.translations_cache }.to(nil)
21
- end
22
- end
23
-
24
- describe ".backend_instance" do
25
- context "when I18n backend has chained backends" do
26
- it "returns Releaf::I18nDatabase::Backend instance" do
27
- backends = I18n::Backend::Chain.new(subject, I18n::Backend::Simple.new)
28
- allow(I18n).to receive(:backend).and_return(backends)
29
- expect(described_class.backend_instance).to eq(subject)
30
- end
31
-
32
- it "returns nil when chain hasn't Releaf::I18nDatabase::Backend instance" do
33
- backends = I18n::Backend::Chain.new(I18n::Backend::Simple.new)
34
- allow(I18n).to receive(:backend).and_return(backends)
35
- expect(described_class.backend_instance).to be nil
36
- end
37
- end
38
-
39
- context "when I18n backend has single backend and it is instance of Releaf::I18nDatabase::Backend" do
40
- it "returns Releaf::I18nDatabase::Backend instance" do
41
- allow(I18n).to receive(:backend).and_return(subject)
42
- expect(described_class.backend_instance).to eq(subject)
43
- end
44
- end
45
-
46
- context "when I18n backend has single backend and it is not instance of Releaf::I18nDatabase::Backend" do
47
- it "returns nil" do
48
- allow(I18n).to receive(:backend).and_return(I18n::Backend::Simple.new)
49
- expect(described_class.backend_instance).to be nil
50
- end
51
- end
52
- end
53
-
54
- describe ".initialize_component" do
55
- it "adds itself as i18n backend as primary backend while keeping Rails default simple backend as secondary" do
56
- allow(I18n).to receive(:backend).and_return(:current_backend)
57
- allow(I18n::Backend::Chain).to receive(:new).with(:new_backend, :current_backend).and_return(:x)
58
- allow(described_class).to receive(:new).and_return(:new_backend)
59
- expect(I18n).to receive(:backend=).with(:x)
60
- described_class.initialize_component
61
- end
62
- end
63
-
64
- describe ".locales_pluralizations" do
65
- it "returns array all pluralization forms for releaf locales" do
66
- allow(Releaf.application.config).to receive(:all_locales).and_return([:de, :ru, :aasdsd])
67
- allow(I18n).to receive(:locale_available?).and_return(true)
68
- expect(described_class.locales_pluralizations).to eq([:one, :other, :few, :many, :zero])
69
- end
70
- end
71
-
72
- describe "#translations" do
73
- let(:another_translations_store){ Releaf::I18nDatabase::TranslationsStore.new }
74
-
75
- context "when translations has been loaded and is not expired" do
76
- it "returns assigned translations hash instance" do
77
- subject.translations_cache = translations_store
78
- allow(translations_store).to receive(:expired?).and_return(false)
79
- expect(Releaf::I18nDatabase::TranslationsStore).to_not receive(:new)
80
- expect(subject.translations).to eq(translations_store)
81
- end
82
- end
83
-
84
- context "when translations has been loaded and is expired" do
85
- it "initializes new `TranslationsStore`, cache and return it" do
86
- subject.translations_cache = translations_store
87
- allow(translations_store).to receive(:expired?).and_return(true)
88
- expect(Releaf::I18nDatabase::TranslationsStore).to receive(:new).and_return(another_translations_store)
89
- expect(subject.translations).to eq(another_translations_store)
90
- end
91
- end
92
-
93
- context "when translations has not been loaded" do
94
- it "initializes new `TranslationsStore`, cache and return it" do
95
- subject.translations_cache = nil
96
- expect(Releaf::I18nDatabase::TranslationsStore).to receive(:new).and_return(another_translations_store)
97
- expect(subject.translations).to eq(another_translations_store)
98
- end
99
- end
100
- end
101
-
102
- describe "#store_translations" do
103
- it "pass given translations to simple translation backend" do
104
- simple_backend = I18n.backend.backends.last
105
- expect(simple_backend).to receive(:store_translations).with(:lv, {a: "x"}, {c: "d"})
106
- subject.store_translations(:lv, {a: "x"}, {c: "d"})
107
- end
108
- end
109
-
110
- describe ".translations_updated_at" do
111
- it "returns translations updated_at from cached settings" do
112
- allow(Releaf::Settings).to receive(:[]).with(described_class::UPDATED_AT_KEY).and_return("x")
113
- expect(described_class.translations_updated_at).to eq("x")
114
- end
115
- end
116
-
117
- describe ".translations_updated_at=" do
118
- it "stores translations updated_at to cached settings" do
119
- expect(Releaf::Settings).to receive(:[]=).with(described_class::UPDATED_AT_KEY, "xx")
120
- described_class.translations_updated_at = "xx"
121
- end
122
- end
123
-
124
- describe "#lookup" do
125
- before do
126
- allow(subject).to receive(:translations).and_return(translations_store)
127
- allow(subject).to receive(:normalize_flat_keys).with(:lv, "some.localization", "_scope_", ":")
128
- .and_return("xx.s.loc")
129
- end
130
-
131
- it "flattens key before passing further" do
132
- expect(translations_store).to receive(:missing?).with(:lv, "xx.s.loc")
133
- expect(translations_store).to receive(:lookup).with(:lv, "xx.s.loc", separator: ":", a: "b")
134
- expect(translations_store).to receive(:missing).with(:lv, "xx.s.loc", separator: ":", a: "b")
135
-
136
- subject.lookup(:lv, "some.localization", "_scope_", separator: ":", a: "b")
137
- end
138
-
139
- context "when translation is known as missing" do
140
- it "does not make lookup in translation hash, does not mark it as missing and return nil" do
141
- allow(translations_store).to receive(:missing?).with(:lv, "xx.s.loc").and_return(true)
142
- expect(translations_store).to_not receive(:lookup)
143
- expect(translations_store).to_not receive(:missing)
144
- expect(subject.lookup(:lv, "some.localization", "_scope_", separator: ":", a: "b")).to be nil
145
- end
146
- end
147
-
148
- context "when translation is not known as missing" do
149
- before do
150
- allow(translations_store).to receive(:missing?).with(:lv, "xx.s.loc").and_return(false)
151
- end
152
-
153
- context "when lookup result is not nil" do
154
- before do
155
- allow(translations_store).to receive(:lookup).with(:lv, "xx.s.loc", separator: ":", a: "b").and_return("x")
156
- end
157
-
158
- it "returns lookup result" do
159
- expect(subject.lookup(:lv, "some.localization", "_scope_", separator: ":", a: "b")).to eq("x")
160
- end
161
-
162
- it "does not mark translation as missing" do
163
- expect(translations_store).to_not receive(:missing).with(:lv, "xx.s.loc", separator: ":", a: "b")
164
- subject.lookup(:lv, "some.localization", "_scope_", separator: ":", a: "b")
165
- end
166
- end
167
-
168
- context "when lookup result is nil" do
169
- before do
170
- allow(translations_store).to receive(:lookup).with(:lv, "xx.s.loc", separator: ":", a: "b").and_return(nil)
171
- end
172
-
173
- it "returns nil" do
174
- expect(subject.lookup(:lv, "some.localization", "_scope_", separator: ":", a: "b")).to be nil
175
- end
176
-
177
- it "marks translation as missing" do
178
- expect(translations_store).to receive(:missing).with(:lv, "xx.s.loc", separator: ":", a: "b")
179
- subject.lookup(:lv, "some.localization", "_scope_", separator: ":", a: "b")
180
- end
181
- end
182
- end
183
-
184
- context "when database doesn't exists" do
185
- it "returns an empty array" do
186
- allow(Releaf::I18nDatabase::I18nEntry).to receive(:pluck).and_raise(ActiveRecord::NoDatabaseError.new("xxx"))
187
- expect(subject.lookup(:lv, "some.localization", "_scope_", separator: ":", a: "b")).to be nil
188
- end
189
- end
190
-
191
- context "when node table doesn't exist" do
192
- it "returns an empty array" do
193
- allow(Releaf::I18nDatabase::I18nEntry).to receive(:pluck).and_raise(ActiveRecord::StatementInvalid.new("xxx"))
194
- expect(subject.lookup(:lv, "some.localization", "_scope_", separator: ":", a: "b")).to be nil
195
- end
196
- end
197
- end
198
- end
@@ -1,13 +0,0 @@
1
- require "rails_helper"
2
-
3
- describe Releaf::I18nDatabase::Configuration do
4
- subject{ described_class.new(translation_auto_creation: true,
5
- translation_auto_creation_patterns: [1, 2],
6
- translation_auto_creation_exclusion_patterns: [3, 4]) }
7
-
8
- it do
9
- is_expected.to have_attributes(translation_auto_creation: true)
10
- is_expected.to have_attributes(translation_auto_creation_patterns: [1, 2])
11
- is_expected.to have_attributes(translation_auto_creation_exclusion_patterns: [3, 4])
12
- end
13
- end
@@ -1,24 +0,0 @@
1
- require "rails_helper"
2
-
3
- describe Releaf::I18nDatabase::HumanizeMissingTranslations do
4
- describe ".call" do
5
- context "when key is present and exception is I18n::MissingTranslation" do
6
- it "humanizes missing translations" do
7
- expect(I18n.t("some.missing translation")).to eq("Missing translation")
8
- end
9
- end
10
-
11
- context "when key is not present and exception is I18n::MissingTranslation" do
12
- it "does not intercept it" do
13
- expect(I18n.t(nil)).to eq("translation missing: en.no key")
14
- end
15
- end
16
-
17
- context "when exception is not I18n::MissingTranslation" do
18
- it "does not intercept it" do
19
- allow(I18n::Backend::Transliterator).to receive(:get).and_raise(I18n::ArgumentError)
20
- expect{I18n.transliterate("error?")}.to raise_error(I18n::ArgumentError)
21
- end
22
- end
23
- end
24
- end
@@ -1,151 +0,0 @@
1
- require "rails_helper"
2
-
3
- describe Releaf::I18nDatabase::ParseSpreadsheetTranslations do
4
- let(:translation){ Releaf::I18nDatabase::I18nEntry.new }
5
- let(:fixture_path){ File.expand_path('../../../fixtures/translations_import.xlsx', __dir__) }
6
- let(:error_message){ "Don't know how to open file #{fixture_path}" }
7
- subject{ described_class.new(file_path: fixture_path, extension: "xlsx") }
8
-
9
- describe "#call" do
10
- it "returns translations" do
11
- allow(subject).to receive(:translations).and_return("x")
12
- expect(subject.call).to eq("x")
13
- end
14
- end
15
-
16
- describe "#rows" do
17
- let(:spreadsheet){ Roo::Spreadsheet.open(fixture_path) }
18
-
19
- before do
20
- allow(subject).to receive(:spreadsheet).and_return(spreadsheet)
21
- end
22
-
23
- it "returns speadsheet casted to array" do
24
- allow(spreadsheet).to receive(:to_a).and_return([1, 2])
25
- expect(subject.rows).to eq([1, 2])
26
- end
27
-
28
- it "caches casted array" do
29
- expect(spreadsheet).to receive(:to_a).and_return([1, 2]).once
30
- subject.rows
31
- subject.rows
32
- end
33
- end
34
-
35
- describe "#data_rows" do
36
- it "returns all rows except first and one with empty first value (key)" do
37
- allow(subject).to receive(:rows).and_return([[1, 2, 3], [:a, 2, 3], [nil, 2, 4], ["", 4, 1], [6, 2, ""]])
38
- expect(subject.data_rows).to eq([[:a, 2, 3], [6, 2, ""]])
39
- end
40
- end
41
-
42
- describe "#locales" do
43
- it "returns all non blank values from first row" do
44
- allow(subject).to receive(:rows).and_return([["lv", "", "de", nil, "en"], [:a, 2, 3], [6, 2, ""]])
45
- expect(subject.locales).to eq(["lv", "de", "en"])
46
- end
47
-
48
- it "caches resolved values" do
49
- expect(subject).to receive(:rows).and_return([["lv", "", "de", nil, "en"], [:a, 2, 3], [6, 2, ""]]).once
50
- subject.locales
51
- subject.locales
52
- end
53
- end
54
-
55
- describe "#spreadsheet" do
56
- it "returns Roo spreadsheet instance" do
57
- allow(Roo::Spreadsheet).to receive(:open).with(fixture_path, extension: "xlsx", file_warning: :ignore).and_return("instance")
58
- expect(subject.spreadsheet).to eq("instance")
59
- end
60
-
61
-
62
- context "when opening the file raises an unsupported content error" do
63
- it "raises Releaf::TranslationsImporter::UnsupportedFileFormatError" do
64
- error = ArgumentError.new("error message")
65
- allow(Roo::Spreadsheet).to receive(:open).and_raise( error )
66
- expect(subject).to receive(:file_format_error?).with( "ArgumentError", "error message").and_return true
67
- expect{ subject.spreadsheet }.to raise_error(described_class::UnsupportedFileFormatError)
68
- end
69
- end
70
-
71
- context "when opening the file raises any other error" do
72
- it "raises Releaf::TranslationsImporter::UnsupportedFileFormatError" do
73
- error = ArgumentError.new("error message")
74
- allow(Roo::Spreadsheet).to receive(:open).and_raise( error )
75
- expect(subject).to receive(:file_format_error?).with( "ArgumentError", "error message").and_return false
76
- expect{ subject.spreadsheet }.to raise_error( error )
77
- end
78
- end
79
-
80
- end
81
-
82
- describe "#file_format_error?" do
83
-
84
- context "when given error is an ArgumentError" do
85
- context "when the message contains 'Don't know how to open file'" do
86
- it "returns true" do
87
- expect(subject.file_format_error?("ArgumentError", error_message)).to be true
88
- end
89
- end
90
- context "when the message is different" do
91
- it "returns false" do
92
- expect(subject.file_format_error?("ArgumentError", "error message")).to be false
93
- end
94
- end
95
- end
96
-
97
- context "when the error is a Zip::ZipError" do
98
- it "returns true" do
99
- expect(subject.file_format_error?("Zip::ZipError", "error message")).to be true
100
- end
101
- end
102
-
103
- context "when the error is a Ole::Storage::FormatError" do
104
- it "returns true" do
105
- expect(subject.file_format_error?("Ole::Storage::FormatError", "error message")).to be true
106
- end
107
- end
108
-
109
-
110
- end
111
-
112
-
113
- describe "#translations" do
114
- it "returns array of processed `Releaf::I18nDatabase::Translation` instances" do
115
- allow(subject).to receive(:data_rows).and_return([[1, 2, 3], [:a, nil, 3], [6, 2, ""]])
116
- allow(subject).to receive(:translation_instance).with(1, ["2", "3"]).and_return("t1")
117
- allow(subject).to receive(:translation_instance).with(:a, ["", "3"]).and_return("t2")
118
- allow(subject).to receive(:translation_instance).with(6, ["2", ""]).and_return("t3")
119
- expect(subject.translations).to eq(["t1", "t2", "t3"])
120
- end
121
- end
122
-
123
- describe "#translation_instance" do
124
- it "returns first or initialized translation for given key and given localizations built" do
125
- where_scope = Releaf::I18nDatabase::I18nEntry.where(key: ["sommmmquery"])
126
- allow(Releaf::I18nDatabase::I18nEntry).to receive(:where).with(key: "as.ld").and_return(where_scope)
127
- allow(where_scope).to receive(:first_or_initialize).and_return(translation)
128
-
129
- expect(subject).to receive(:maintain_translation_locales).with(translation, ["x", "y"])
130
- expect(subject.translation_instance("as.ld", ["x", "y"])).to eq(translation)
131
- end
132
- end
133
-
134
- describe "#maintain_translation_locales" do
135
- before do
136
- allow(subject).to receive(:locales).and_return(["en", "de", "ge", "lv"])
137
- translation.i18n_entry_translation.build(locale: "de", text: "po")
138
- translation.i18n_entry_translation.build(locale: "ge", text: "x")
139
- end
140
-
141
- it "builds translation data for all missing locales" do
142
- expect{ subject.maintain_translation_locales(translation, ["ent", "det", "get", "lvt"]) }
143
- .to change{ translation.i18n_entry_translation.map{|td| td.locale } }.from(["de", "ge"]).to(["de", "ge", "en", "lv"])
144
- end
145
-
146
- it "overwrites existing translation data localizations only if new localizations is not empty" do
147
- expect{ subject.maintain_translation_locales(translation, ["ent", "", "get", "lvt"]) }
148
- .to change{ translation.i18n_entry_translation.map{|td| [td.locale, td.text] } }.to([["de", "po"], ["ge", "get"], ["en", "ent"], ["lv", "lvt"]])
149
- end
150
- end
151
- end
@@ -1,530 +0,0 @@
1
- require "rails_helper"
2
-
3
- describe Releaf::I18nDatabase::TranslationsStore do
4
- describe "#initialize" do
5
- it "assigns updated at from `Releaf::I18nDatabase::Backend` last updated at timestamp" do
6
- allow(Releaf::I18nDatabase::Backend).to receive(:translations_updated_at).and_return("x")
7
- expect(subject.updated_at).to eq("x")
8
- end
9
-
10
- it "assigns empty hash to missing keys" do
11
- expect(subject.missing_keys).to eq({})
12
- end
13
- end
14
-
15
- describe "#config" do
16
- it "returns Releaf.application.config" do
17
- allow(Releaf.application).to receive(:config).and_return(:x)
18
- expect(subject.config).to eq(:x)
19
- end
20
- end
21
-
22
- describe "#expired?" do
23
- context "when last translation update differs from last cache load" do
24
- it "returns true" do
25
- allow(Releaf::I18nDatabase::Backend).to receive(:translations_updated_at).and_return(1)
26
- subject.updated_at = 2
27
- expect(subject.expired?).to be true
28
- end
29
- end
30
-
31
- context "when last translation update does not differ from last cache load" do
32
- it "returns false" do
33
- allow(Releaf::I18nDatabase::Backend).to receive(:translations_updated_at).and_return(1)
34
- subject.updated_at = 1
35
- expect(subject.expired?).to be false
36
- end
37
- end
38
- end
39
-
40
- describe "#exist?" do
41
- context "when given key exists within stored keys hash" do
42
- it "returns true" do
43
- allow(subject).to receive(:stored_keys).and_return("asd.xxx" => true)
44
- expect(subject.exist?("asd.xxx")).to be true
45
- end
46
- end
47
-
48
- context "when given key does not exist within keys hash" do
49
- it "returns false" do
50
- allow(subject).to receive(:stored_keys).and_return("asd.xxyy" => true)
51
- expect(subject.exist?("asd.xxx")).to be false
52
- end
53
- end
54
- end
55
-
56
- describe "#lookup" do
57
- it "returns first valid and returnable result" do
58
- allow(subject).to receive(:dig_valid_translation)
59
- .with(:ru, ["admin", "menu", "translations"], true, count: 23).and_return("x1")
60
- allow(subject).to receive(:returnable_result?)
61
- .with("x1", count: 23).and_return(false)
62
-
63
- allow(subject).to receive(:dig_valid_translation)
64
- .with(:ru, ["admin", "translations"], false, count: 23).and_return("x2")
65
- allow(subject).to receive(:returnable_result?)
66
- .with("x2", count: 23).and_return(true)
67
-
68
- expect(subject.lookup(:ru, "admin.menu.translations", count: 23)).to eq("x2")
69
- end
70
-
71
- it "iterates throught all translation scopes until last key" do
72
- expect(subject).to receive(:dig_valid_translation)
73
- .with(:ru, ["admin", "menu", "another", "translations"], true, count: 23).and_return(nil)
74
- expect(subject).to receive(:dig_valid_translation)
75
- .with(:ru, ["admin", "menu", "translations"], false, count: 23).and_return(nil)
76
- expect(subject).to receive(:dig_valid_translation)
77
- .with(:ru, ["admin", "translations"], false, count: 23).and_return(nil)
78
- expect(subject).to receive(:dig_valid_translation)
79
- .with(:ru, ["translations"], false, count: 23).and_return("x1")
80
-
81
- subject.lookup(:ru, "admin.menu.another.translations", count: 23)
82
- end
83
-
84
- context "when no matches found" do
85
- it "returns nil" do
86
- allow(subject).to receive(:dig_valid_translation).and_return(nil)
87
- expect(subject.lookup(:ru, "admin.menu.translations", count: 23)).to be nil
88
- end
89
- end
90
- end
91
-
92
- describe "#dig_translation" do
93
- before do
94
- allow(subject).to receive(:stored_translations).and_return(
95
- lv: {
96
- admin: {
97
- some_scope: {
98
- save: "Saglabāt"
99
- }
100
- }
101
- },
102
- lt: {
103
- public: {
104
- another_scope: {
105
- email: "E-pastas"
106
- }
107
- }
108
- }
109
- )
110
- end
111
-
112
- it "dig given locale prefixed keys hash against stored translations and returns matched value (String or Hash)" do
113
- expect(subject.dig_translation(:lv, [:admin, :some_scope, :save])).to eq("Saglabāt")
114
- expect(subject.dig_translation(:lt, [:public, :another_scope, :email])).to eq("E-pastas")
115
- expect(subject.dig_translation(:lt, [:public, :another_scope])).to eq(email: "E-pastas")
116
- end
117
-
118
- context "when no match found" do
119
- it "returns nil" do
120
- expect(subject.dig_translation(:lt, [:admin, :some_scope, :save])).to be nil
121
- expect(subject.dig_translation(:lt, [:public, :email])).to be nil
122
- expect(subject.dig_translation(:ge, [:public, :email])).to be nil
123
- end
124
- end
125
- end
126
-
127
- describe "#dig_valid_translation" do
128
- before do
129
- allow(subject).to receive(:dig_translation).with(:de, [:admin, :save]).and_return("translated_value")
130
- end
131
-
132
- context "when digged translation has valid result" do
133
- it "returns digged translation result" do
134
- allow(subject).to receive(:invalid_result?).with(:de, "translated_value", true, a: :b).and_return(false)
135
- expect(subject.dig_valid_translation(:de, [:admin, :save], true, a: :b)).to eq("translated_value")
136
- end
137
- end
138
-
139
- context "when digged translation has invalid result" do
140
- it "returns nil" do
141
- allow(subject).to receive(:invalid_result?).with(:de, "translated_value", true, a: :b).and_return(true)
142
- expect(subject.dig_valid_translation(:de, [:admin, :save], true, a: :b)).to be nil
143
- end
144
- end
145
- end
146
-
147
- describe "#invalid_result?" do
148
- before do
149
- allow(subject).to receive(:invalid_nonpluralized_result?).with("xx", true, a: :b).and_return(false)
150
- allow(subject).to receive(:invalid_pluralized_result?).with(:ge, "xx", a: :b).and_return(false)
151
- end
152
-
153
- context "when invalid nonplurarized result" do
154
- it "returns true" do
155
- allow(subject).to receive(:invalid_nonpluralized_result?).with("xx", true, a: :b).and_return(true)
156
- expect(subject.invalid_result?(:ge, "xx", true, a: :b)).to be true
157
- end
158
- end
159
-
160
- context "when invalid plurarized result" do
161
- it "returns true" do
162
- allow(subject).to receive(:invalid_pluralized_result?).with(:ge, "xx", a: :b).and_return(true)
163
- expect(subject.invalid_result?(:ge, "xx", true, a: :b)).to be true
164
- end
165
- end
166
-
167
- context "when no invalid nonplurarized or pluralized result" do
168
- it "returns false" do
169
- expect(subject.invalid_result?(:ge, "xx", true, a: :b)).to be false
170
- end
171
- end
172
- end
173
-
174
- describe "#invalid_nonpluralized_result?" do
175
- context "when hash result, not first lookup and options without count" do
176
- it "returns true" do
177
- expect(subject.invalid_nonpluralized_result?({a: :b}, false, scope: "xxx")).to be true
178
- end
179
- end
180
-
181
- context "when hash result, first lookup and options without count" do
182
- it "returns false" do
183
- expect(subject.invalid_nonpluralized_result?({a: :b}, true, scope: "xxx")).to be false
184
- end
185
- end
186
-
187
- context "when hash result, not first lookup and options with count" do
188
- it "returns false" do
189
- expect(subject.invalid_nonpluralized_result?({a: :b}, false, count: 43, scope: "xxx")).to be false
190
- end
191
- end
192
-
193
- context "when non hash result, not first lookup and options without count" do
194
- it "returns false" do
195
- expect(subject.invalid_nonpluralized_result?("x", false, scope: "xxx")).to be false
196
- end
197
- end
198
- end
199
-
200
- describe "#invalid_pluralized_result?" do
201
- before do
202
- allow(subject).to receive(:valid_pluralized_result?).with(:ge, 12, a: :b).and_return(false)
203
- end
204
-
205
- context "when hash result, options has count and invalid pluralized result" do
206
- it "returns true" do
207
- expect(subject.invalid_pluralized_result?(:ge, {a: :b}, {count: 12})).to be true
208
- end
209
- end
210
-
211
- context "when non hash result, options has count and invalid pluralized result" do
212
- it "returns false" do
213
- expect(subject.invalid_pluralized_result?(:ge, "X", {count: 12})).to be false
214
- end
215
- end
216
-
217
- context "when hash result, options has no count value and invalid pluralized result" do
218
- it "returns false" do
219
- expect(subject.invalid_pluralized_result?(:ge, {a: :b}, {scope: "xx.xx"})).to be false
220
- end
221
- end
222
-
223
- context "when hash result, options has count and valid pluralized result" do
224
- it "returns false" do
225
- allow(subject).to receive(:valid_pluralized_result?).with(:ge, 12, a: :b).and_return(true)
226
- expect(subject.invalid_pluralized_result?(:ge, {a: :b}, {count: 12})).to be false
227
- end
228
- end
229
- end
230
-
231
- describe "#valid_pluralized_result?" do
232
- context "when given hash contains valid result for given locale and count" do
233
- it "return true" do
234
- expect(subject.valid_pluralized_result?(:lv, 2, one: "x", other: "y")).to be true
235
- end
236
- end
237
-
238
- context "when given hash contains invalid result for given locale and count" do
239
- it "return false" do
240
- expect(subject.valid_pluralized_result?(:lv, 2, one: "x", few: "y")).to be false
241
- end
242
- end
243
- end
244
-
245
- describe "#returnable_result?" do
246
- context "when given result is not blank" do
247
- it "returns true" do
248
- expect(subject.returnable_result?("x", a: "b")).to be true
249
- end
250
- end
251
-
252
- context "when `inherit_scopes` option value is boolean `false`" do
253
- it "returns true" do
254
- expect(subject.returnable_result?(nil, inherit_scopes: false)).to be true
255
- end
256
- end
257
-
258
- context "when given result is blank and `inherit_scopes` option value is not boolean `false`" do
259
- it "returns false" do
260
- expect(subject.returnable_result?(nil, inherit_scopes: true)).to be false
261
- expect(subject.returnable_result?(nil, a: "b")).to be false
262
- end
263
- end
264
- end
265
-
266
- describe "#localization_data" do
267
- it "returns hash with all non empty translations with locale prefixed key and localization as value" do
268
- i18n_entry_1 = Releaf::I18nDatabase::I18nEntry.create(key: "some.food")
269
- i18n_entry_1.i18n_entry_translation.create(locale: "lv", text: "suņi")
270
- i18n_entry_1.i18n_entry_translation.create(locale: "en", text: "dogs")
271
-
272
- i18n_entry_2 = Releaf::I18nDatabase::I18nEntry.create(key: "some.good")
273
- i18n_entry_2.i18n_entry_translation.create(locale: "lv", text: "xx")
274
- i18n_entry_2.i18n_entry_translation.create(locale: "en", text: "")
275
-
276
- expect(subject.localization_data).to eq("lv.some.food" => "suņi", "en.some.food" => "dogs",
277
- "lv.some.good" => "xx")
278
- end
279
-
280
- it "has cached method result" do
281
- expect(described_class).to cache_instance_method(:localization_data)
282
- end
283
- end
284
-
285
- describe "#stored_keys" do
286
- it "returns hash with existing translation keys" do
287
- Releaf::I18nDatabase::I18nEntry.create(key: "some.food")
288
- Releaf::I18nDatabase::I18nEntry.create(key: "some.good")
289
- expect(subject.stored_keys).to eq("some.food" => true, "some.good" => true)
290
- end
291
-
292
- it "has cached method result" do
293
- expect(described_class).to cache_instance_method(:stored_keys)
294
- end
295
- end
296
-
297
- describe "#stored_translations" do
298
- it "returns deep merged hash from all key translation hashes" do
299
- allow(subject).to receive(:stored_keys).and_return("some.food" => true, "some.good" => true)
300
- allow(subject).to receive(:key_hash).with("some.food").and_return(lv: {a: "a1"}, en: {d: {e: "ee"}})
301
- allow(subject).to receive(:key_hash).with("some.good").and_return(lv: {b: "b2"}, en: {d: {u: "ll"}})
302
- expect(subject.stored_translations).to eq(lv: {a: "a1", b: "b2"}, en: {d: {e: "ee", u: "ll"}})
303
- end
304
-
305
- context "when no keys exists" do
306
- it "returns empty hash" do
307
- allow(subject).to receive(:stored_keys).and_return({})
308
- expect(subject.stored_translations).to eq({})
309
- end
310
- end
311
-
312
- it "has cached method result" do
313
- expect(described_class).to cache_instance_method(:stored_translations)
314
- end
315
- end
316
-
317
- describe "#key_hash" do
318
- it "build stored translation hash with keys and translated values for given key" do
319
- allow(subject.config).to receive(:all_locales).and_return([:ge, :de])
320
- allow(subject).to receive(:localization_data).and_return(
321
- "lv.admin.releaf_i18n_database_translations.Save" => "zc",
322
- "ge.admin.Save" => "asdasd",
323
- "de.admin.releaf_i18n_database_translations.Save" => "Seiv",
324
- "de.admin.releaf_i18n_database_translations.Cancel" => "dCancel",
325
- "ge.admin.releaf_i18n_database_translations.Cancel" => "gCancel",
326
- )
327
-
328
- expect(subject.key_hash("admin.releaf_i18n_database_translations.Save")).to eq(
329
- ge: {
330
- admin: {
331
- releaf_i18n_database_translations: {
332
- Save: nil
333
- }
334
- }
335
- },
336
- de: {
337
- admin: {
338
- releaf_i18n_database_translations: {
339
- Save: "Seiv"
340
- }
341
- }
342
- }
343
- )
344
-
345
- expect(subject.key_hash("admin.releaf_i18n_database_translations.Cancel")).to eq(
346
- ge: {
347
- admin: {
348
- releaf_i18n_database_translations: {
349
- Cancel: "gCancel"
350
- }
351
- }
352
- },
353
- de: {
354
- admin: {
355
- releaf_i18n_database_translations: {
356
- Cancel: "dCancel"
357
- }
358
- }
359
- }
360
- )
361
- end
362
- end
363
-
364
- describe "#missing?" do
365
- context "when given locale and key combination exists within missing keys hash" do
366
- it "returns true" do
367
- allow(subject).to receive(:missing_keys).and_return("lv.asd.xxx" => true)
368
- expect(subject.missing?(:lv, "asd.xxx")).to be true
369
- end
370
- end
371
-
372
- context "when given locale and key combination does not exist missing keys hash" do
373
- it "returns false" do
374
- allow(subject).to receive(:missing_keys).and_return("en.asd.xxx" => true)
375
- expect(subject.missing?(:lv, "asd.xxx")).to be false
376
- end
377
- end
378
- end
379
-
380
- describe "#missing" do
381
- it "adds given locale and key combination as missing" do
382
- expect{ subject.missing(:de, "as.pasd", a: "x") }.to change { subject.missing_keys["de.as.pasd"] }
383
- .from(nil).to(true)
384
- end
385
-
386
- context "when missing translation creation is available for given key and options" do
387
- it "creates missing translation" do
388
- allow(subject).to receive(:auto_create?).with("ps.asda", a: "x").and_return(true)
389
- expect(subject).to receive(:auto_create).with("ps.asda", a: "x")
390
- subject.missing(:de, "ps.asda", a: "x")
391
- end
392
- end
393
-
394
- context "when missing translation creation is not available for given key and options" do
395
- it "does not create missing translation" do
396
- allow(subject).to receive(:auto_create?).with("ps.asda", a: "x").and_return(false)
397
- expect(subject).to_not receive(:auto_create)
398
- subject.missing(:de, "ps.asda", a: "x")
399
- end
400
- end
401
- end
402
-
403
- describe "#auto_create?" do
404
- before do
405
- allow(subject.config.i18n_database ).to receive(:translation_auto_creation).and_return(true)
406
- allow(subject).to receive(:stored_keys).and_return("xxxome.save" => "xxxome.save")
407
- allow(subject).to receive(:auto_creation_inclusion?).with("some.save").and_return(true)
408
- allow(subject).to receive(:auto_creation_exception?).with("some.save").and_return(false)
409
- end
410
-
411
- context "when missing translation creation is enabled globally by i18n config and not disabled by `auto_create` option" do
412
- it "returns true" do
413
- expect(subject.auto_create?("some.save", {})).to be true
414
- expect(subject.auto_create?("some.save", auto_create: true)).to be true
415
- expect(subject.auto_create?("some.save", auto_create: nil)).to be true
416
- end
417
- end
418
-
419
- context "when no auto creation inclusion" do
420
- it "returns false" do
421
- allow(subject).to receive(:auto_creation_inclusion?).with("some.save").and_return(false)
422
- expect(subject.auto_create?("some.save", {})).to be false
423
- end
424
- end
425
-
426
- context "when auto creation exception" do
427
- it "returns false" do
428
- allow(subject).to receive(:auto_creation_exception?).with("some.save").and_return(true)
429
- expect(subject.auto_create?("some.save", {})).to be false
430
- end
431
- end
432
-
433
- context "when missing translation creation is disabled globally by i18n config" do
434
- it "returns false" do
435
- allow(subject.config.i18n_database ).to receive(:translation_auto_creation).and_return(false)
436
- expect(subject.auto_create?("some.save", {})).to be false
437
- end
438
- end
439
-
440
- context "when missing translation creation is disabled by `auto_create` option" do
441
- it "returns false" do
442
- expect(subject.auto_create?("some.save", auto_create: false)).to be false
443
- end
444
- end
445
-
446
- context "when key already exists within stored keys hash" do
447
- it "returns false" do
448
- allow(subject).to receive(:stored_keys).and_return("some.save" => "some.save")
449
- expect(subject.auto_create?("some.save", {})).to be false
450
- end
451
- end
452
- end
453
-
454
- describe "#auto_creation_inclusion?" do
455
- context "when given key matches any auto creation pattern" do
456
- it "returns true" do
457
- allow(subject.config.i18n_database ).to receive(:translation_auto_creation_patterns).and_return([/^another\./, /^some\./])
458
- expect(subject.auto_creation_inclusion?("some.save")).to be true
459
- end
460
- end
461
-
462
- context "when given key matches no auto creation pattern" do
463
- it "returns false" do
464
- allow(subject.config.i18n_database ).to receive(:translation_auto_creation_patterns).and_return([/^another\./, /^foo\./])
465
- expect(subject.auto_creation_inclusion?("some.save")).to be false
466
- end
467
- end
468
- end
469
-
470
- describe "#auto_creation_exception?" do
471
- context "when given key matches any auto creation exception pattern" do
472
- it "returns true" do
473
- allow(subject.config.i18n_database ).to receive(:translation_auto_creation_exclusion_patterns).and_return([/^another\./, /^some\./])
474
- expect(subject.auto_creation_exception?("some.save")).to be true
475
- end
476
- end
477
-
478
- context "when given key matches no auto creation exception pattern" do
479
- it "returns false" do
480
- allow(subject.config.i18n_database ).to receive(:translation_auto_creation_exclusion_patterns).and_return([/^another\./, /^foo\./])
481
- expect(subject.auto_creation_exception?("some.save")).to be false
482
- end
483
- end
484
- end
485
-
486
- describe "#auto_create" do
487
- before do
488
- allow(Releaf::I18nDatabase::Backend).to receive(:locales_pluralizations).and_return([:one, :many, :other])
489
- end
490
-
491
- context "when pluralizable translation given" do
492
- it "creates translation for each pluralization form" do
493
- allow(subject).to receive(:pluralizable_translation?).with(a: "b").and_return(true)
494
- expect(Releaf::I18nDatabase::I18nEntry).to receive(:create).with(key: "aasd.oihgja.sd.one")
495
- expect(Releaf::I18nDatabase::I18nEntry).to receive(:create).with(key: "aasd.oihgja.sd.many")
496
- expect(Releaf::I18nDatabase::I18nEntry).to receive(:create).with(key: "aasd.oihgja.sd.other")
497
- subject.auto_create("aasd.oihgja.sd", a: "b")
498
- end
499
- end
500
-
501
- context "when non pluralizable translation given" do
502
- it "creates translation" do
503
- allow(subject).to receive(:pluralizable_translation?).with(a: "b").and_return(false)
504
- expect(Releaf::I18nDatabase::I18nEntry).to receive(:create).with(key: "aasd.oihgja.sd")
505
- subject.auto_create("aasd.oihgja.sd", a: "b")
506
- end
507
- end
508
- end
509
-
510
- describe "#pluralizable_translation?" do
511
- context "when given options has count key and `create_plurals` key is true" do
512
- it "returns true" do
513
- expect(subject.pluralizable_translation?(count: 3, create_plurals: true)).to be true
514
- end
515
- end
516
-
517
- context "when given options has count key and `create_plurals` key is not true" do
518
- it "returns false" do
519
- expect(subject.pluralizable_translation?(count: 3, create_plurals: false)).to be false
520
- expect(subject.pluralizable_translation?(count: 3)).to be false
521
- end
522
- end
523
-
524
- context "when given options has no count key and `create_plurals` key is true" do
525
- it "returns false" do
526
- expect(subject.pluralizable_translation?(create_plurals: true)).to be false
527
- end
528
- end
529
- end
530
- end