Module: HttpAcceptLanguage
- Defined in:
- lib/http_accept_language.rb
Instance Method Summary collapse
-
#compatible_language_from(array) ⇒ Object
Returns the first of the user_preferred_languages that is included into the given array, ignoring region.
-
#preferred_language_from(array) ⇒ Object
Returns a preferred language from an array containing language codes with regions.
-
#user_preferred_language_codes ⇒ Object
Returns a sorted array of language codes symbols based on user’s browser preference sent via the Accept-Language HTTP header.
-
#user_preferred_languages ⇒ Object
Returns a sorted array based on user preference in sent via the Accept-Language HTTP header.
-
#user_preferred_languages=(languages) ⇒ Object
Sets the user languages preference, overiding the browser.
Instance Method Details
#compatible_language_from(array) ⇒ Object
Returns the first of the user_preferred_languages that is included into the given array, ignoring region.
Useful with Rails’ I18n.available_locales.
Example:
Accept-Language: en;q=0.3, nl-NL, nl-be;q=0.9, en-US;q=0.5
request.compatible_language_from [:nl, :it]
# => 'nl'
74 75 76 |
# File 'lib/http_accept_language.rb', line 74 def compatible_language_from(array) (user_preferred_language_codes & strip_region_from(array)).first end |
#preferred_language_from(array) ⇒ Object
Returns a preferred language from an array containing language codes with regions.
Example:
Accept-Language: en;q=0.3, nl-NL, nl-be;q=0.9, en-US;q=0.5
request.preferred_language_from %w( it fr nl nl-NL )
# => 'nl'
58 59 60 |
# File 'lib/http_accept_language.rb', line 58 def preferred_language_from(array) (user_preferred_languages & array.map(&:to_s)).first end |
#user_preferred_language_codes ⇒ Object
Returns a sorted array of language codes symbols based on user’s browser preference sent via the Accept-Language HTTP header.
Example:
Accept-Language: en;q=0.3, nl-NL, nl-be;q=0.9, en-US;q=0.5
request.user_preferred_languages
# => [ :nl, :en ]
36 37 38 39 |
# File 'lib/http_accept_language.rb', line 36 def user_preferred_language_codes @user_preferred_language_codes ||= strip_region_from user_preferred_languages end |
#user_preferred_languages ⇒ Object
Returns a sorted array based on user preference in sent via the Accept-Language HTTP header. Don’t think this is holy!
Returns an empty array if the header does not contain any parsable language code.
Example:
Accept-Language: en;q=0.3, nl-NL, nl-be;q=0.9, en-US;q=0.5
request.user_preferred_languages
# => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/http_accept_language.rb', line 16 def user_preferred_languages begin @user_preferred_languages ||= env['HTTP_ACCEPT_LANGUAGE']. scan(/\b([a-z]{2}(?:-[a-z]{2})?)(?:;q=([01](?:\.\d)?))?\s*($|,)/). sort_by {|l, pref| 1 - (pref || 1).to_f}. map! {|l,| l.downcase.sub(/-\w{2}/) { $&.upcase } } rescue # Just rescue anything if the browser messed up badly. [] end end |
#user_preferred_languages=(languages) ⇒ Object
Sets the user languages preference, overiding the browser
43 44 45 46 |
# File 'lib/http_accept_language.rb', line 43 def user_preferred_languages=(languages) @user_preferred_languages = languages @user_preferred_language_codes = nil end |