Sha256: 705ec64b265515cec1881c7396844f500a00e891d0f2c75f2f1e9ea65421bf41

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

require 'hashie'
require 'world_flags/core_ext'
require 'world_flags/helpers'

require 'world_flags/rails/engine' if defined?(::Rails::Engine)

require 'world_flags/languages'
require 'world_flags/countries'
require 'world_flags/util'

module WorldFlags
	class << self
		include Util

		attr_writer :active_locales
		attr_writer :locale_flag_map		

		def label code = :us, options = {:language => :en}
			locale = extract_locale!(options) || default_locale_used || :en
			options[:country] ? country_label(code, locale) : language_label(code, locale)
		end

		def flag_code code = :us
			# ensure that 'en_US' becomes simply 'us'
			code = code.to_s.sub(/^\w+_/, '').downcase
			(locale_flag_map[code.to_sym] || code).to_sym
		end

		def locale code = :us
			flag_locale_map[code.to_sym] || code
		end
		
		# override using fx 'locale_to_country_code.json' file
		def locale_flag_map
			@locale_flag_map ||= keys_to_sym(locale_flag_hash) 
		end

		# see core_ext
		def flag_locale_map
			@flag_locale_map ||= keys_to_sym(locale_flag_map.hash_revert)
		end

		protected

		def keys_to_sym hash
			hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
		end

		def locale_flag_hash
			{ 
				:en => "us",
  			:da => "dk",
  			:sv => "se",
  			:sq => "al",
  			:nb => "no",
  			:ja => "jp",
  			:uk => "ua"
  		}
  	end

		def extract_locale! options
			options[:country] ? options.delete(:country) : options.delete(:language)
		end		
	end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
world-flags-0.4.2 lib/world-flags.rb
world-flags-0.4.1 lib/world-flags.rb
world-flags-0.4.0 lib/world-flags.rb