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
|
=begin
locale_cgi.rb
Copyright (C) 2002-2006 Masao Mutoh
You may redistribute it and/or modify it under the same
license terms as Ruby.
$Id: locale_cgi.rb,v 1.4 2006/06/11 15:36:20 mutoh Exp $
=end
module Locale
# Locale::System module for Default OS (Unix)
# This is low-level class. Application shouldn't use this directly.
module SystemCGI
@@default_locale = Locale::Object.new("en", nil, "UTF-8")
@@cgi = nil
module_function
# Gets the default locale using setlocale and nl_langinfo.
# * Returns the system locale (Locale::Object).
def system
return @@default_locale unless @@cgi
cgi_ = cgi
if ret = cgi_["lang"] and ret.size > 0
elsif ret = cgi_.cookies["lang"][0]
elsif lang = cgi_.accept_language and lang.size > 0
ret = lang.split(/,|;/).first
else
ret = @@default_locale.to_str
end
codesets = cgi_.accept_charset
if codesets and codesets.size > 0
ary = codesets.split(',')
codeset = ary[0]
codeset = @@default_locale.charset if codeset == "*"
else
codeset = @@default_locale.charset
end
Locale::Object.new(ret, nil, codeset)
end
# Gets the charset of the locale.
# * locale: Locale::Object
# * Returns: the charset of the locale
def get_charset(locale)
locale.charset ? locale.charset : @@default_locale.charset
end
# Sets a CGI object.
# * cgi_: CGI object
# * Returns: self
def set_cgi(cgi_)
@@cgi = cgi_
self
end
# Sets a CGI object.
# * cgi_: CGI object
# * Returns: cgi_
def cgi=(cgi_)
set_cgi(cgi_)
cgi_
end
# Gets the CGI object. If it is nil, returns new CGI object.
# * Returns: the CGI object
def cgi
@@cgi = CGI.new unless @@cgi
@@cgi
end
# Sets a default locale. en.UTF-8 is the default value if not set.
# * locale: Locale::Object object. You can't set nil.
# * Returns: self
def set_default_locale(locale)
raise "Wrong parameter: #{locale}" if locale.nil?
@@default_locale = locale
self
end
# Sets a default locale. en.UTF-8 is the default value if not set.
# * locale: Locale::Object
# * Returns: locale
def default_locale=(locale)
set_default_locale(locale)
locale
end
# Gets the default Locale::Object.
# * Returns: the default locale
def default_locale
@@default_locale
end
end
@@locale_system_module = SystemCGI
end
|