Sha256: 866f282eefeb243c1b4b920dcc61b8c651988d29292f9a012e1c2209c2daac63

Contents?: true

Size: 1.79 KB

Versions: 8

Compression:

Stored size: 1.79 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 config &block
  		(block_given? && block.arity == 1) ? yield(self) : instance_eval(&block)
		end

		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

		# avoid uk being translated to ukraine for domain names!		
		def domain_to_locale code
			domain_locale_map[code.to_sym] || flag_locale_map[code.to_sym] || code
		end

		def domain_locale_map
			{ 
				:uk => "en_GB",
				:tp => "tl",
				:su => 'ru',
				:an => 'nl'
  		}
  	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

8 entries across 8 versions & 1 rubygems

Version Path
world-flags-0.5.1 lib/world-flags.rb
world-flags-0.5.0 lib/world-flags.rb
world-flags-0.4.9 lib/world-flags.rb
world-flags-0.4.8.3 lib/world-flags.rb
world-flags-0.4.8.2 lib/world-flags.rb
world-flags-0.4.7 lib/world-flags.rb
world-flags-0.4.5 lib/world-flags.rb
world-flags-0.4.4 lib/world-flags.rb