Sha256: b784b5dee3f48a40fea79f97308c1047a83856f5788c14a399a3049ec4c4a083

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 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

		# 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

1 entries across 1 versions & 1 rubygems

Version Path
world-flags-0.4.3 lib/world-flags.rb