Sha256: 964b273f9d4f1efb27b06e9f7666dcf322f6f2a811fc3a36a570d9b498ee74c7

Contents?: true

Size: 1009 Bytes

Versions: 6

Compression:

Stored size: 1009 Bytes

Contents

module WorldFlags
  module Helper
    module Locale
      def set_locale    
        I18n.locale = locales.select_first_in(valid_locales.downcase)
      end  

      def valid_locales
        if I18n.respond_to?(:available_locales) && I18n.available_locales.present?
          I18n.available_locales
        else
          WorldFlags.valid_locales
        end
      end

      def locales
        [params[:locale], extract_locale_from_tld, browser_locale, ip_country_code, I18n.default_locale].downcase
      end
 
      # 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
      def extract_locale_from_tld
        parsed_locale = request.host.split('.').last
        I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale  : nil
      end      
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
world-flags-0.4.0 lib/world_flags/helper/locale.rb
world-flags-0.3.6 lib/world_flags/helper/locale.rb
world-flags-0.3.5 lib/world_flags/helper/locale.rb
world-flags-0.3.4 lib/world_flags/helper/locale.rb
world-flags-0.3.2.2 lib/world_flags/helper/locale.rb
world-flags-0.3.2.1 lib/world_flags/helper/locale.rb