module UrlLocale

Public Class Methods

detect(request) click to toggle source
# File lib/url_locale.rb, line 43
def detect request
  if translations.present?
    host_url = request.url.split(request.host_with_port).first + request.host_with_port
    host_locales[host_url] ||
      path_locales.match(request.path).try(:[], 1) || 
      I18n.default_locale.to_s
  end
end
host_locales() click to toggle source

def host_mode

@@mode = :host

end

def mode

@@mode ||= :path

end

# File lib/url_locale.rb, line 28
def host_locales
  @@host_locales ||= begin
    config_fpath = File.join Rails.root, 'config', 'url_locale.yml'
    result = {}
    if File.exist? config_fpath
      YAML::load_file(config_fpath).each do |locale, hosts|
        for host in hosts
          result[host] = locale.underscore
        end
      end
    end
    result
  end
end
path_locales() click to toggle source

Returns a Regexp to extract the locale from request.path

# File lib/url_locale.rb, line 16
def path_locales
  @@path_locales ||= Regexp.new "^\/(#{translations*'|'})(\/|$)"
end
translations() click to toggle source

Returns an I18n.available_locales - I18n.default_locale

# File lib/url_locale.rb, line 6
def translations
  @@translations ||= (I18n.available_locales - [I18n.default_locale]).map &:to_s
end