Sha256: 7cf4cced03f568867d93b66751e0f4d7e7f4bc91d2dfead9d3d5ec3b1d1f084d

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

module Knj::Locales
	def self.lang
		begin
			locale = _session[:locale]
		rescue NameError
			#_session method does not exist - continue.
		end
		
		locale = ENV["LANGUAGE"] if !locale and ENV["LANGUAGE"]
		locale = $locale if !locale and $locale
		
		raise "Could not figure out locale." if !locale
		raise "Could not understand language: #{locale}." if !match = locale.to_s.match(/^([a-z]{2})_([A-Z]{2})/)
				
		return {
			"first" => match[1],
			"second" => match[2],
			"full" => match[1] + "_" + match[2]
		}
	end
	
	def self.localeconv
		f = Knj::Locales.lang["first"]
		
		dec = "."
		thousand = ","
		
		case f
			when "da", "es", "de", "sv"
				dec = ","
				thousand = "."
			when "en"
				#do nothing.
			else
				raise "Cant figure out numbers for language: #{f}."
		end
		
		return {
			"decimal_point" => dec,
			"thousands_sep" => thousand
		}
	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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
knjrbfw-0.0.8 lib/knj/locales.rb
knjrbfw-0.0.7 lib/knj/locales.rb
knjrbfw-0.0.4 lib/knj/locales.rb
knjrbfw-0.0.3 lib/knj/locales.rb