File tree 2 files changed +20
-6
lines changed
2 files changed +20
-6
lines changed Original file line number Diff line number Diff line change @@ -74,7 +74,12 @@ def translate(key, **options)
74
74
options = options . dup
75
75
options [ :default ] &&= options [ :default ] . to_s
76
76
message = ::I18n . translate ( key , **options )
77
- message . present? ? message : ::I18n . translate ( key , locale : FALLBACK_LOCALE , **options )
77
+ message . present? ? message : fallback_message ( key , **options )
78
+ end
79
+
80
+ def fallback_message ( key , **options )
81
+ return key unless ::I18n . available_locales . include? ( FALLBACK_LOCALE )
82
+ ::I18n . translate ( key , locale : FALLBACK_LOCALE , **options )
78
83
end
79
84
end
80
85
end
Original file line number Diff line number Diff line change 9
9
10
10
context 'when I18n enforces available locales' do
11
11
before { I18n . enforce_available_locales = true }
12
- after { I18n . enforce_available_locales = false }
12
+ after do
13
+ I18n . available_locales = %i[ de en ]
14
+ I18n . enforce_available_locales = false
15
+ end
13
16
14
17
context 'when the fallback locale is available' do
15
- before { I18n . available_locales = %i[ de en ] }
18
+ before do
19
+ I18n . available_locales = %i[ de en ]
20
+ I18n . default_locale = :de
21
+ end
16
22
17
23
it 'returns the translated message' do
18
24
expect ( subject ) . to eq ( 'cannot convert String to xml' )
19
25
end
20
26
end
21
27
22
28
context 'when the fallback locale is not available' do
23
- before { I18n . available_locales = %i[ de jp ] }
29
+ before do
30
+ I18n . available_locales = %i[ de jp ]
31
+ I18n . default_locale = :de
32
+ end
24
33
25
- it 'currently raises an error ' do
26
- expect { subject } . to raise_error ( I18n :: InvalidLocale )
34
+ it 'returns the translation string ' do
35
+ expect ( subject ) . to eq ( "grape.errors.messages. #{ key } " )
27
36
end
28
37
end
29
38
end
You can’t perform that action at this time.
0 commit comments