Returns the first of the user_preferred_languages that is compatible with the available locales. Ignores region.
Example:
request.compatible_language_from I18n.available_locales
# File lib/http_accept_language.rb, line 49 def compatible_language_from(available_languages) user_preferred_languages.map do |preferred| available_languages.find do |available| available.to_s =~ /^#{Regexp.escape(preferred.to_s)}(-|$)/ end end.compact.first end
Finds the locale specifically requested by the browser.
Example:
request.preferred_language_from I18n.available_locales # => 'nl'
# File lib/http_accept_language.rb, line 38 def preferred_language_from(array) (user_preferred_languages & array.collect { |i| i.to_s }).first end
Returns a sorted array based on user preference in HTTP_ACCEPT_LANGUAGE. Browsers send this HTTP header, so don't think this is holy.
Example:
request.user_preferred_languages # => [ 'nl-NL', 'nl-BE', 'nl', 'en-US', 'en' ]
# File lib/http_accept_language.rb, line 11 def user_preferred_languages @user_preferred_languages ||= env['HTTP_ACCEPT_LANGUAGE'].split(/\s*,\s*/).collect do |l| l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/ l.split(';q=') end.sort do |x,y| raise "Not correctly formatted" unless x.first =~ /^[a-z\-]+$/ y.last.to_f <=> x.last.to_f end.collect do |l| l.first.downcase.gsub(/-[a-z]+$/) { |x| x.upcase } end rescue # Just rescue anything if the browser messed up badly. [] end
Generated with the Darkfish Rdoc Generator 2.