@@ -136,6 +136,75 @@ def teardown
136
136
Lit . loader = old_loader
137
137
end
138
138
139
+ test 'it wont overwrite existing UI-changed values with those from yaml' do
140
+ load_sample_yml ( 'en.yml' )
141
+ old_loader = Lit . loader
142
+ Lit . loader = nil
143
+ Lit . init
144
+
145
+ # Defaults from yml: en.foo: bar, en.nil_thing: [nothing]
146
+ assert_equal 'bar' , I18n . t ( 'foo' )
147
+ assert_equal 'no longer nil' , I18n . t ( 'nil_thing' , default : 'no longer nil' )
148
+
149
+ foo_loc = Lit ::LocalizationKey . find_by_localization_key ( 'foo' ) . localizations . first
150
+ nil_loc = Lit ::LocalizationKey . find_by_localization_key ( 'nil_thing' ) . localizations . first
151
+
152
+ # Check if default values have been loaded into DB
153
+ assert_equal 'bar' , foo_loc . default_value
154
+ assert_equal 'no longer nil' , nil_loc . default_value
155
+
156
+ # Translate as if it was done in UI (is_changed set to true)
157
+ foo_loc . update ( translated_value : 'barbar' , is_changed : true )
158
+ nil_loc . update ( translated_value : 'new one' , is_changed : true )
159
+ [ foo_loc , nil_loc ] . each do |loc |
160
+ Lit . init . cache . update_cache loc . full_key , loc . get_value
161
+ end
162
+
163
+ # Translations should be changed as intended
164
+ assert_equal 'barbar' , I18n . t ( 'foo' )
165
+ assert_equal 'new one' , I18n . t ( 'nil_thing' )
166
+
167
+ # Reload Lit, UI-changed translations should be intact
168
+ Lit . loader = nil
169
+ Lit . init
170
+ assert_equal 'barbar' , I18n . t ( 'foo' )
171
+ assert_equal 'new one' , I18n . t ( 'nil_thing' )
172
+
173
+ Lit . loader = old_loader
174
+ end
175
+
176
+ test 'it will overwrite existing values with those from yaml for unchanged localizations' do
177
+ load_sample_yml ( 'en.yml' )
178
+ old_loader = Lit . loader
179
+ Lit . loader = nil
180
+ Lit . init
181
+
182
+ # Defaults from en.yml: en.foo: bar, en.nil_thing: [nothing]
183
+ assert_equal 'bar' , I18n . t ( 'foo' )
184
+ assert_equal 'no longer nil' , I18n . t ( 'nil_thing' , default : 'no longer nil' )
185
+
186
+ foo_loc = Lit ::LocalizationKey . find_by_localization_key ( 'foo' )
187
+ . localizations . first
188
+ nil_loc = Lit ::LocalizationKey . find_by_localization_key ( 'nil_thing' )
189
+ . localizations . first
190
+
191
+ # Check if default values have been loaded into DB
192
+ assert_equal 'bar' , foo_loc . default_value
193
+ assert_equal 'no longer nil' , nil_loc . default_value
194
+
195
+ # Defaults from en_changed.yml en.foo: barbar, en.nil_thing: not nil anymore
196
+ # Swap yml file and reload Lit, changes in yml file should be visible
197
+ I18n . load_path = [ ]
198
+ load_sample_yml ( 'en_changed.yml' )
199
+
200
+ Lit . loader = nil
201
+ Lit . init
202
+ assert_equal 'barbar' , I18n . t ( 'foo' )
203
+ assert_equal 'not nil anymore' , I18n . t ( 'nil_thing' )
204
+
205
+ Lit . loader = old_loader
206
+ end
207
+
139
208
private
140
209
141
210
def find_localization_for ( key , locale )
0 commit comments