Sha256: 7e428760f703afb94e57feb6273ce6874ed68a84f9b47099770f27c9c5f8b960

Contents?: true

Size: 1.44 KB

Versions: 13

Compression:

Stored size: 1.44 KB

Contents

module Knj::Locales
  def self.lang
    match = self.locale.to_s.match(/^([a-z]{2})_([A-Z]{2})/)
    raise "Could not understand language: #{self.locale}." if !match
        
    return {
      "first" => match[1],
      "second" => match[2],
      "full" => match[1] + "_" + match[2]
    }
  end
  
  def self.localeconv
    f = Knj::Locales.lang["first"]
    
    dec = "."
    thousand = ","
    csv_delimiter = ","
    
    case f
      when "da", "es", "de", "sv"
        dec = ","
        thousand = "."
        csv_delimiter = ";"
      when "en"
        #do nothing.
      else
        raise "Cant figure out numbers for language: #{f}."
    end
    
    return {
      "decimal_point" => dec,
      "thousands_sep" => thousand,
      "csv_delimiter" => csv_delimiter
    }
  end
  
  def self.number_in(num_str)
    lc = Knj::Locales.localeconv
    num_str = num_str.to_s.gsub(lc["thousands_sep"], "").gsub(lc["decimal_point"], ".").to_f
    return num_str
  end
  
  def self.number_out(num_str, dec = 2)
    lc = Knj::Locales.localeconv
    return Knj::Php.number_format(num_str, dec, lc["decimal_point"], lc["thousands_sep"])
  end
  
  def self.locale
    begin
      return _session[:locale]
    rescue NameError
      if Thread.current[:locale]
        return Thread.current[:locale]
      elsif $locale
        return $locale
      elsif ENV["LANGUAGE"]
        return ENV["LANGUAGE"]
      end
    end
    
    raise "Could not figure out locale."
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
knjrbfw-0.0.21 lib/knj/locales.rb
knjrbfw-0.0.20 lib/knj/locales.rb
knjrbfw-0.0.19 lib/knj/locales.rb
knjrbfw-0.0.18 lib/knj/locales.rb
knjrbfw-0.0.17 lib/knj/locales.rb
knjrbfw-0.0.16 lib/knj/locales.rb
knjrbfw-0.0.15 lib/knj/locales.rb
knjrbfw-0.0.14 lib/knj/locales.rb
knjrbfw-0.0.13 lib/knj/locales.rb
knjrbfw-0.0.12 lib/knj/locales.rb
knjrbfw-0.0.11 lib/knj/locales.rb
knjrbfw-0.0.10 lib/knj/locales.rb
knjrbfw-0.0.9 lib/knj/locales.rb