module Locale::Driver::Env

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.

Public Instance Methods

locale() click to toggle source

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.

# File lib/locale/driver/env.rb, line 40
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
locales() click to toggle source

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

# File lib/locale/driver/env.rb, line 58
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