Sha256: ccfb6692c8c9bbc70874bed50aa9558967a81bc84d49d1704cf2c4445ac87ea6

Contents?: true

Size: 1.7 KB

Versions: 7

Compression:

Stored size: 1.7 KB

Contents

=begin
  locale/env.rb 

  Copyright (C) 2008  Masao Mutoh

  You may redistribute it and/or modify it under the same
  license terms as Ruby.

  Original: Ruby-GetText-Package-1.92.0.

  $Id: env.rb 27 2008-12-03 15:06:50Z mutoh $
=end

require 'locale/tag'
require 'locale/taglist'

module Locale 
  module Driver
    # 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.
    module Env
      module_function

      # Gets the locale from environment variable. (LC_ALL > LC_MESSAGES > LANG)
      # Returns: the locale as Locale::Tag::Posix.
      def locale
        # At least one environment valiables should be set on *nix system.
        [ENV["LC_ALL"], ENV["LC_MESSAGES"], ENV["LANG"]].each do |loc|
          if loc != nil and loc.size > 0
            return Locale::Tag::Posix.parse(loc)
          end
        end
        nil
      end

      # Gets the locales from environment variables. (LANGUAGE > LC_ALL > LC_MESSAGES > LANG)
      # * Returns: an Array of the locale as Locale::Tag::Posix or nil.
      def locales
        if (locales = ENV["LANGUAGE"])
          Locale::TagList.new(locales.split(/:/).collect{|v| Locale::Tag::Posix.parse(v)})
        elsif (loc = locale)
          Locale::TagList.new([loc])
        else
          nil
        end
      end

      # Gets the charset from environment variable or return nil.
      # * Returns: the system charset.
      def charset  # :nodoc:
        if loc = locale
          loc.charset
        else
          nil
        end
      end
      
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
locale-2.0.5 lib/locale/driver/env.rb
locale-2.0.0 lib/locale/driver/env.rb
locale-2.0.1 lib/locale/driver/env.rb
locale-0.9.0 lib/locale/driver/env.rb
locale-2.0.3 lib/locale/driver/env.rb
locale-2.0.4 lib/locale/driver/env.rb
locale-2.0.2 lib/locale/driver/env.rb