Module: Locale::Driver::Env

Defined in:
lib/locale/driver/env.rb

Overview

Locale::Driver::Env module. Detect the user locales and the charset. All drivers(except CGI) refer environment variables first and use it as the locale if it’s defined. This is a low-level module. Application shouldn’t use this directly.

Defined Under Namespace

Modules: Private

Class Method Summary collapse

Class Method Details

.charsetObject

Gets the charset from environment variables (LC_ALL > LC_CTYPE > LANG) or return nil.

  • Returns: the system charset.



76
77
78
79
80
81
82
83
# File 'lib/locale/driver/env.rb', line 76

def charset  # :nodoc:
  [ENV["LC_ALL"], ENV["LC_CTYPE"], ENV["LANG"]].each do |env|
    tag = Private.parse(env)
    next if tag.nil?
    return tag.charset
  end
  nil
end

.localeObject

Gets the locale from environment variable. Priority order except charset is LC_ALL > LC_MESSAGES > LANG. Priority order for charset is LC_ALL > LC_CTYPE > LANG. Returns: the locale as Locale::Tag::Posix.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/locale/driver/env.rb', line 41

def locale
  lc_all = Private.parse(ENV["LC_ALL"])
  return lc_all if lc_all

  lc_messages = Private.parse(ENV["LC_MESSAGES"])
  lang = Private.parse(ENV["LANG"])

  tag = lc_messages || lang
  return nil if tag.nil?

  lc_ctype = Private.parse(ENV["LC_CTYPE"])
  tag.charset = lc_ctype.charset if lc_ctype

  tag
end

.localesObject

Gets the locales from environment variables. (LANGUAGE > LC_ALL > LC_MESSAGES > LANG)

  • Returns: an Array of the locale as Locale::Tag::Posix or nil.



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/locale/driver/env.rb', line 59

def locales
  return nil if (ENV["LC_ALL"] || ENV["LC_MESSAGES"] || ENV["LANG"]) == "C"
  locales = ENV["LANGUAGE"]
  if (locales != nil and locales.size > 0)
    locs = locales.split(/:/).collect{|v| Locale::Tag::Posix.parse(v)}.compact
    if locs.size > 0
      return Locale::TagList.new(locs)
    end
  elsif (loc = locale)
    return Locale::TagList.new([loc])
  end
  nil
end