Module: Locale::Driver::CGI
Public Instance Methods
charset ()
Gets the charset from CGI parameters. (Based on RFC2616)
* Returns: the charset (HTTP_ACCEPT_CHARSET > "UTF-8").
# File lib/locale/driver/cgi.rb, line 72 72: def charset 73: cgi_ = cgi 74: charsets = cgi_.accept_charset 75: if charsets and charsets.size > 0 76: num = charsets.index(',') 77: charset = num ? charsets[0, num] : charsets 78: charset = @@default_charset if charset == "*" 79: else 80: charset = @@default_charset 81: end 82: charset 83: end
locales ()
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 31 31: def locales 32: return Locale::TagList.new([@@default_locale]) unless cgi 33: cgi_ = cgi 34: 35: locales = Locale::TagList.new 36: 37: # QUERY_STRING "lang" 38: if cgi_.has_key?("lang") 39: langs = cgi_.params["lang"] 40: if langs 41: langs.each do |lang| 42: locales << Locale::Tag.parse(lang) 43: end 44: end 45: end 46: 47: unless locales.size > 0 48: # COOKIE "lang" 49: langs = cgi_.cookies["lang"] 50: if langs 51: langs.each do |lang| 52: locales << Locale::Tag.parse(lang) if lang.size > 0 53: end 54: end 55: end 56: 57: unless locales.size > 0 58: # HTTP_ACCEPT_LANGUAGE 59: if lang = cgi_.accept_language and lang.size > 0 60: 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])} 61: end 62: end 63: 64: unless locales.size > 0 65: locales << @@default_locale 66: end 67: Locale::TagList.new(locales.uniq) 68: end
set_cgi (cgi_)
Sets a CGI object.
- cgi_: CGI object
- Returns: self
# File lib/locale/driver/cgi.rb, line 88 88: def set_cgi(cgi_) 89: Thread.current[:current_cgi] = cgi_ 90: self 91: end