Project

General

Profile

« Previous | Next » 

Revision 54b7abb5

Added by Marc Dequènes over 15 years ago

  • ID 54b7abb5dfadbe9521cdb501f21e3f2bb79ad10d

[evol] RFC2616 Accept-Language field support #1 (needed for RFC2798 'preferredLanguage' attribute type support)

View differences:

lib/cyborghood/language.rb
require 'locale'
module CyborgHood
# RFC2616 Accept-Language field
# (used in RFC2798 'preferredLanguage' attribute type)
class AcceptLanguage
LANG_RANGE_Q_PATTERN="([a-z]{1,8}(-[a-z]{1,8})*(;q=)|\*)"
ACCEPT_LANG_PATTERN="#{LANG_RANGE_Q_PATTERN}( *, *#{LANG_RANGE_Q_PATTERN})*"
attr_reader :sorted_lang_list
private :new
def initialize(sorted_lang_list)
@sorted_lang_list = sorted_lang_list
end
def self.parse(accept_language_list)
return nil unless accept_language_list =~ Regexp.new(ACCEPT_LANG_PATTERN, Regexp::EXTENDED | Regexp::IGNORECASE)
list = accept_language_list.split(',').collect do |lang_range_q|
lang_range, quality = lang_range_q.split(';')
quality ||= 1
[Locale::Object.new(lang_range), quality]
end
sorted_list = list.sort do |l1, l2|
r = l1[1] <=> l2[1]
# if quality is equal, then prefer the most specific language tag
if r == 0
l1[0].to_iso3066.size <=> l2[0].to_iso3066.size
else
r
end
end
new(sorted_list)
end
def reduce(available_language_list)
# return a new object with ordered matching language tags
end
end
end

Also available in: Unified diff