Gets the charset from CGI parameters. (Based on RFC2616)
* Returns: the charset (HTTP_ACCEPT_CHARSET or nil).
# File lib/locale/driver/cgi.rb, line 62 62: def charset 63: req = Thread.current[:current_request] 64: return nil unless req 65: 66: charsets = req[:accept_charset] 67: if charsets and charsets.size > 0 68: num = charsets.index(',') 69: charset = num ? charsets[0, num] : charsets 70: charset = nil if charset == "*" 71: else 72: charset = nil 73: end 74: charset 75: end
Clear the current request.
# File lib/locale/driver/cgi.rb, line 94 94: def clear_current_request 95: Thread.current[:current_request] = nil 96: end
Gets required locales from CGI parameters. (Based on RFC2616)
Returns: An Array of Locale::Tag’s subclasses
(QUERY_STRING "lang" > COOKIE "lang" > HTTP_ACCEPT_LANGUAGE > "en")
# File lib/locale/driver/cgi.rb, line 28 28: def locales 29: req = Thread.current[:current_request] 30: return nil unless req 31: 32: locales = [] 33: 34: # QUERY_STRING "lang" 35: if langs = req[:query_langs] 36: langs.each do |lang| 37: locales << Locale::Tag.parse(lang) 38: end 39: end 40: 41: unless locales.size > 0 42: # COOKIE "lang" 43: if langs = req[:cookie_langs] 44: langs.each do |lang| 45: locales << Locale::Tag.parse(lang) if lang.size > 0 46: end 47: end 48: end 49: 50: unless locales.size > 0 51: # HTTP_ACCEPT_LANGUAGE 52: if lang = req[:accept_language] and lang.size > 0 53: locales += lang.gsub(/\s/, "").split(/,/).map{|v| v.split(";q=")}.map{|j| [j[0], j[1] ? j[1].to_f : 1.0]}.sort{|a,b| -(a[1] <=> b[1])}.map{|v| Locale::Tag.parse(v[0])} 54: end 55: end 56: 57: locales.size > 0 ? Locale::TagList.new(locales.uniq) : nil 58: end
Set a request.
query_langs: An Array of QUERY_STRING value “lang”.
cookie_langs: An Array of cookie value “lang”.
accept_language: The value of HTTP_ACCEPT_LANGUAGE
accept_charset: The value of HTTP_ACCEPT_CHARSET
# File lib/locale/driver/cgi.rb, line 83 83: def set_request(query_langs, cookie_langs, accept_language, accept_charset) 84: Thread.current[:current_request] = { 85: :query_langs => query_langs, 86: :cookie_langs => cookie_langs, 87: :accept_language => accept_language, 88: :accept_charset => accept_charset 89: } 90: self 91: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.