File tree 2 files changed +13
-2
lines changed
2 files changed +13
-2
lines changed Original file line number Diff line number Diff line change @@ -72,9 +72,12 @@ def user_preferred_locale(header)
72
72
73
73
if I18n . enforce_available_locales
74
74
locale = locales . reverse . find { |locale | I18n . available_locales . any? { |al | match? ( al , locale ) } }
75
- if locale
76
- I18n . available_locales . find { |al | match? ( al , locale ) }
75
+ matched_locale = I18n . available_locales . find { |al | match? ( al , locale ) } if locale
76
+ if !locale && !matched_locale
77
+ matched_locale = locales . reverse . find { |locale | I18n . available_locales . any? { |al | variant_match? ( al , locale ) } }
78
+ matched_locale = matched_locale [ 0 , 2 ] if matched_locale
77
79
end
80
+ matched_locale
78
81
else
79
82
locales . last
80
83
end
@@ -83,5 +86,9 @@ def user_preferred_locale(header)
83
86
def match? ( s1 , s2 )
84
87
s1 . to_s . casecmp ( s2 . to_s ) == 0
85
88
end
89
+
90
+ def variant_match? ( s1 , s2 )
91
+ s1 . to_s . casecmp ( s2 [ 0 , 2 ] . to_s ) == 0
92
+ end
86
93
end
87
94
end
Original file line number Diff line number Diff line change @@ -71,6 +71,10 @@ def enforce_available_locales(enforce)
71
71
_ ( response_with_languages ( 'en-gb,en-us;q=0.95;en' ) . body ) . must_equal ( 'en-gb' )
72
72
end
73
73
74
+ specify 'should match languages with variants' do
75
+ _ ( response_with_languages ( 'pt;Q=0.9,es-CL' ) . body ) . must_equal ( 'es' )
76
+ end
77
+
74
78
specify 'should skip * if it is followed by other languages' do
75
79
_ ( response_with_languages ( '*,dk;q=0.5' ) . body ) . must_equal ( 'dk' )
76
80
end
You can’t perform that action at this time.
0 commit comments