Module: Locale::Driver::CGI

### Home Classes Methods

### In Files

- lib/locale/driver/cgi.rb

### Methods

- #charset
- #clear_current_request
- #locales
- #set_request

### Class/Module Index

Quicksearch

- Locale
- Locale::Driver
- Locale::Driver::CGI
- Locale::Driver::Env
- Locale::Driver::Env::Private
- Locale::Driver::JRuby
- Locale::Driver::Posix
- Locale::Driver::Win32
- Locale::Driver::Win32::Kernel32
- Locale::Driver::Win32Table
- Locale::Info
- Locale::Info::Language
- Locale::Info::Region
- Locale::Middleware
- Locale::Tag
- Locale::Tag::Cldr
- Locale::Tag::Common
- Locale::Tag::Irregular
- Locale::Tag::Posix
- Locale::Tag::Rfc
- Locale::Tag::Simple
- Locale::TagList

No matching classes.

# Locale::Driver::CGI

### Public Instance Methods

charset() click to toggle source

Gets the charset from CGI parameters. (Based on RFC2616)

    * Returns: the charset (HTTP_ACCEPT_CHARSET or nil).

    # File lib/locale/driver/cgi.rb, line 73
    def charset
      req = Thread.current[:current_request]
      return nil unless req

      charsets = req[:accept_charset]
      if charsets and charsets.size > 0
        num = charsets.index(',')
        charset = num ? charsets[0, num] : charsets
        charset = nil if charset == "*"
      else
        charset = nil
      end
      charset
    end

clear_current_request() click to toggle source

Clear the current request.

    # File lib/locale/driver/cgi.rb, line 106
    def clear_current_request
      Thread.current[:current_request] = nil
    end

locales() click to toggle source

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 37
    def locales
      req = Thread.current[:current_request]
      return nil unless req

      locales = []

      # QUERY_STRING "lang"
      if langs = req[:query_langs]
        langs.each do |lang|
          locales << Locale::Tag.parse(lang)
        end
      end

      unless locales.size > 0
        # COOKIE "lang"
        if langs = req[:cookie_langs]
          langs.each do |lang|
            locales << Locale::Tag.parse(lang) if lang.size > 0
          end
        end
      end

      unless locales.size > 0
        # HTTP_ACCEPT_LANGUAGE
        if lang = req[:accept_language] and lang.size > 0
          # 10.0 is for ruby-1.8.6 which have the bug of str.to_f.
          # Normally, this should be 1.0.
          locales += lang.gsub(/\s/, "").split(/,/).map{|v| v.split(";q=")}.map{|j| [j[0], j[1] ? j[1].to_f : 10.0]}.sort{|a,b| -(a[1] <=> b[1])}.map{|v| Locale::Tag.parse(v[0])}
        end
      end

      locales.size > 0 ? Locale::TagList.new(locales.uniq) : nil
    end

set_request(query_langs, cookie_langs, accept_language, accept_charset) click to toggle source

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 94
    def set_request(query_langs, cookie_langs, accept_language, accept_charset)
      Locale.clear
      Thread.current[:current_request] = {
        :query_langs => query_langs,
        :cookie_langs => cookie_langs,
        :accept_language => accept_language,
        :accept_charset => accept_charset
      }
      self
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.