@@ -24,6 +24,14 @@ def response_with_languages(accept_languages)
2424 Rack ::MockRequest . new ( app ) . get ( '/' , { 'HTTP_ACCEPT_LANGUAGE' => accept_languages } )
2525 end
2626
27+ def enforce_available_locales ( enforce )
28+ default_enforce = I18n . enforce_available_locales
29+ I18n . enforce_available_locales = enforce
30+ yield
31+ ensure
32+ I18n . enforce_available_locales = default_enforce
33+ end
34+
2735 specify 'should use I18n.default_locale if no languages are requested' do
2836 I18n . default_locale = :zh
2937 response_with_languages ( nil ) . body . must_equal ( 'zh' )
@@ -47,14 +55,26 @@ def response_with_languages(accept_languages)
4755 end
4856
4957 specify 'should treat a * as "all other languages"' do
50- response_with_languages ( '*,en;q=0.5' ) . body . must_equal ( I18n . default_locale . to_s )
58+ response_with_languages ( '*,en;q=0.5' ) . body . must_equal ( I18n . default_locale . to_s )
5159 end
5260
5361 specify 'should reset the I18n locale after the response' do
5462 I18n . locale = :es
5563 response_with_languages ( 'en,de;q=0.8' )
5664 I18n . locale . must_equal ( :es )
5765 end
66+
67+ specify 'should pick the available language' do
68+ enforce_available_locales ( true ) do
69+ response_with_languages ( 'ch,en;q=0.9,es;q=0.95' ) . body . must_equal ( 'es' )
70+ end
71+ end
72+
73+ specify 'when not enforce should pick the language with the highest qvalue' do
74+ enforce_available_locales ( false ) do
75+ response_with_languages ( 'ch,en;q=0.9' ) . body . must_equal ( 'ch' )
76+ end
77+ end
5878 end
5979rescue LoadError
6080 STDERR . puts "WARN: Skipping Rack::Locale tests (i18n not installed)"
0 commit comments