<commit_before>module Globalize

  class NoBaseLanguageError < StandardError; end

=begin rdoc
  Locale defines the currenctly active _locale_. You'll mostly use it like this:
    Locale.set("en-US")

  +en+ is the code for English, and +US+ is the country code. The country code is
  optional, but you'll need to define it to get a lot of the localization features.
=end
  class Locale
    attr_reader :language, :country, :code
    attr_accessor :date_format, :currency_format, :currency_code,
      :thousands_sep, :decimal_sep, :currency_decimal_sep,
      :number_grouping_scheme

    @@cache = {}
    @@translator_class = DbViewTranslator
    @@translator = {}
    @@active = nil
    @@base_language = nil
    @@base_language_code = nil
    @@translator = @@translator_class.instance

    # Is there an active locale?
    def self.active?; !@@active.nil? end

    # This is the focal point of the class. Sets the locale in the familiar
    # RFC 3066 format (see: http://www.faqs.org/rfcs/rfc3066.html). It can
    # also take a Locale object. Set it to the +nil+ object, to deactivate
    # the locale.
    def self.set(locale)
      if locale.kind_of? Locale
        @@active = locale
      elsif locale.nil?
        @@active = nil
      else
        @@active = ( @@cache[locale] ||= Locale.new(locale) )
      end
    end

    # Clears the locale cache -- used mostly for testing.
    def self.clear_cache
      @@cache.clear
    end

    # Returns the active locale.
    def self.active; @@active end

    # Sets the base language. The base language is the language that has
    # complete coverage in the database. For instance, if you have a +Category+
    # model with a +name+ field, the base language is the language in which names
    # are stored in the model itself, and not in the translations table.
    #
    # Takes either a language code (valid RFC 3066 code like +en+ or <tt>en-US</tt>)
    # or a language object.
    #
    # May be set with a language code in environment.rb, without accessing the db.
    def self.set_base_language(lang)
      if lang.kind_of? Language
        @@base_language = lang
      else
        @@base_language_code = RFC_3066.parse lang
      end
    end

    # Returns the base language. Raises an exception if none is set.
    def self.base_language
      @@base_language ? @@base_language :
        (@@base_language_code ?
        (@@base_language = Language.pick(@@base_language_code)) :
        raise(NoBaseLanguageError, "base language must be defined"))
    end

    # Is the currently active language the base language?
    def self.base?
      active ? active.language == base_language : true
    end

    # Returns the currently active language model or +nil+.
    def self.language
      active? ? active.language : nil
    end

    # Returns the currently active language code or +nil+.
    def self.language_code
      active? ? language.code : nil
    end

    # Returns the currently active country model or +nil+.
    def self.country
      active? ? active.country : nil
    end

    def self.switch_locale(code)<commit_msg>--Adding rdoc to Globalize::Locale<commit_after>module Globalize

  class NoBaseLanguageError < StandardError; end

=begin rdoc
  Locale defines the currenctly active _locale_. You'll mostly use it like this:
    Locale.set("en-US")

  +en+ is the code for English, and +US+ is the country code. The country code is
  optional, but you'll need to define it to get a lot of the localization features.
=end
  class Locale
    attr_reader :language, :country, :code
    attr_accessor :date_format, :currency_format, :currency_code,
      :thousands_sep, :decimal_sep, :currency_decimal_sep,
      :number_grouping_scheme

    @@cache = {}
    @@translator_class = DbViewTranslator
    @@translator = {}
    @@active = nil
    @@base_language = nil
    @@base_language_code = nil
    @@translator = @@translator_class.instance

    # Is there an active locale?
    def self.active?; !@@active.nil? end

    # This is the focal point of the class. Sets the locale in the familiar
    # RFC 3066 format (see: http://www.faqs.org/rfcs/rfc3066.html). It can
    # also take a Locale object. Set it to the +nil+ object, to deactivate
    # the locale.
    def self.set(locale)
      if locale.kind_of? Locale
        @@active = locale
      elsif locale.nil?
        @@active = nil
      else
        @@active = ( @@cache[locale] ||= Locale.new(locale) )
      end
    end

    # Clears the locale cache -- used mostly for testing.
    def self.clear_cache
      @@cache.clear
    end

    # Returns the active locale.
    def self.active; @@active end

    # Sets the base language. The base language is the language that has
    # complete coverage in the database. For instance, if you have a +Category+
    # model with a +name+ field, the base language is the language in which names
    # are stored in the model itself, and not in the translations table.
    #
    # Takes either a language code (valid RFC 3066 code like +en+ or <tt>en-US</tt>)
    # or a language object.
    #
    # May be set with a language code in environment.rb, without accessing the db.
    def self.set_base_language(lang)
      if lang.kind_of? Language
        @@base_language = lang
      else
        @@base_language_code = RFC_3066.parse lang
      end
    end

    # Returns the base language. Raises an exception if none is set.
    def self.base_language
      @@base_language ? @@base_language :
        (@@base_language_code ?
        (@@base_language = Language.pick(@@base_language_code)) :
        raise(NoBaseLanguageError, "base language must be defined"))
    end

    # Is the currently active language the base language?
    def self.base?
      active ? active.language == base_language : true
    end

    # Returns the currently active language model or +nil+.
    def self.language
      active? ? active.language : nil
    end

    # Returns the currently active language code or +nil+.
    def self.language_code
      active? ? language.code : nil
    end

    # Returns the currently active country model or +nil+.
    def self.country
      active? ? active.country : nil
    end

    # Allows you to switch the current locale while within the block.
    # The previously current locale is reset after the block is finished.
    #
    # e.g
    #     Locale.set('en-US')
    #     Locale.switch_locale('es-ES') do
    #       product.name = 'esquis'
    #     end
    #
    #     product.name
    #     > skis
    def self.switch_locale(code)
{'fraction_non_alphanumeric': 0.08504129703273172, 'fraction_numerical': 0.004894463138574488, 'mean_word_length': 2.6674144699943914, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7642413', 'n_tokens_mistral': 1989, 'n_tokens_neox': 1870, 'n_words': 709}