Module: WorldFlags::Helper::Locale
- Defined in:
- lib/world_flags/helper/locale.rb
Instance Method Summary collapse
- #browser_ip ⇒ Object
-
#extract_locale_from_tld ⇒ Object
Get locale from top-level domain or return nil if such locale is not available You have to put something like: 127.0.0.1 application.com 127.0.0.1 application.it 127.0.0.1 application.pl in your /etc/hosts file to try this out locally.
- #locale_priority(name) ⇒ Object
- #locale_source_priority ⇒ Object
- #locale_sources ⇒ Object
-
#locales ⇒ Object
ensure all country/language/domain types are mapped to their equivalent locale code.
- #parsed_domain ⇒ Object
- #parsed_locale ⇒ Object
- #set_locale ⇒ Object
- #valid_locales ⇒ Object
Instance Method Details
#browser_ip ⇒ Object
44 45 46 |
# File 'lib/world_flags/helper/locale.rb', line 44 def browser_ip request.remote_ip end |
#extract_locale_from_tld ⇒ Object
Get locale from top-level domain or return nil if such locale is not available You have to put something like:
127.0.0.1 application.com
127.0.0.1 application.it
127.0.0.1 application.pl
in your /etc/hosts file to try this out locally
58 59 60 |
# File 'lib/world_flags/helper/locale.rb', line 58 def extract_locale_from_tld I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale : nil end |
#locale_priority(name) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/world_flags/helper/locale.rb', line 29 def locale_priority name case name.to_sym when :param params[:locale] when :domain extract_locale_from_tld # http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains when :browser browser_locale # http://www.metamodpro.com/browser-language-codes when :ip ip_country_code(browser_ip) when :default I18n.default_locale end end |
#locale_source_priority ⇒ Object
48 49 50 |
# File 'lib/world_flags/helper/locale.rb', line 48 def locale_source_priority WorldFlags.locale_source_priority end |
#locale_sources ⇒ Object
22 23 24 25 26 27 |
# File 'lib/world_flags/helper/locale.rb', line 22 def locale_sources locale_source_priority.inject([]) do |res, name| res << locale_priority(name) res end end |
#locales ⇒ Object
ensure all country/language/domain types are mapped to their equivalent locale code
18 19 20 |
# File 'lib/world_flags/helper/locale.rb', line 18 def locales locale_sources.compact.downcase.map {|loc| WorldFlags.locale(loc) unless loc.blank? } end |
#parsed_domain ⇒ Object
66 67 68 |
# File 'lib/world_flags/helper/locale.rb', line 66 def parsed_domain request.host.split('.').last end |
#parsed_locale ⇒ Object
62 63 64 |
# File 'lib/world_flags/helper/locale.rb', line 62 def parsed_locale WorldFlags.domain_to_locale(parsed_domain) end |
#set_locale ⇒ Object
4 5 6 |
# File 'lib/world_flags/helper/locale.rb', line 4 def set_locale I18n.locale = locales.select_first_in(valid_locales.downcase) end |
#valid_locales ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/world_flags/helper/locale.rb', line 8 def valid_locales return WorldFlags.available_locales if WorldFlags.available_locales.present? if I18n.respond_to?(:available_locales) && I18n.available_locales.present? I18n.available_locales else raise "You must define a list of available locales for use with WorldFlags either in WorldFlags.available_locales or I18n.available_locales" end end |