File: cgi.rb

package info (click to toggle)
ruby-locale 2.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 444 kB
  • ctags: 293
  • sloc: ruby: 3,203; makefile: 4
file content (150 lines) | stat: -rw-r--r-- 4,821 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012  Kouhei Sutou <kou@clear-code.com>
# Copyright (C) 2002-2008  Masao Mutoh
#
# Original: Ruby-GetText-Package-1.92.0.
# License: Ruby's or LGPL
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

require "locale/driver"

module Locale
  # Locale::Driver module for CGI.
  # Detect the user locales and the charset from CGI parameters.
  # This is a low-level class. Application shouldn't use this directly.
  module Driver
    module CGI
      $stderr.puts self.name + " is loaded." if $DEBUG

      module_function
      # 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")
      # 
      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

      # Gets the charset from CGI parameters. (Based on RFC2616)
      #  * Returns: the charset (HTTP_ACCEPT_CHARSET or nil).
      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

      # 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
      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

      # Clear the current request.
      def clear_current_request
        Thread.current[:current_request] = nil
      end
    end

    MODULES[:cgi] = CGI
  end

  
  module_function
  # Sets a request values for lang/charset.
  #
  # * 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
  def set_request(query_langs, cookie_langs, accept_language, accept_charset)
    driver_module.set_request(query_langs, cookie_langs, accept_language, accept_charset)
    self
  end

  # Sets a CGI object. This is the convenient function of set_request().
  #
  # This method is appeared when Locale.init(:driver => :cgi) is called.
  #
  # * cgi: CGI object
  # * Returns: self
  def set_cgi(cgi)
    set_request(cgi.params["lang"], cgi.cookies["lang"],
                cgi.accept_language, cgi.accept_charset)
    self
  end
  
  # Sets a CGI object.This is the convenient function of set_request().
  #
  # This method is appeared when Locale.init(:driver => :cgi) is called.
  #
  # * cgi: CGI object
  # * Returns: cgi 
  def cgi=(cgi)
    set_cgi(cgi)
    cgi
  end
end