Module: UrlLocale

Defined in:
lib/url_locale.rb,
lib/url_locale/middleware.rb

Defined Under Namespace

Classes: Middleware

Class Method Summary collapse

Class Method Details

.detect(request) ⇒ Object



43
44
45
46
47
48
49
50
# 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_localesObject

def host_mode

@@mode = :host

end

def mode

@@mode ||= :path

end



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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_localesObject

Returns a Regexp to extract the locale from request.path



16
17
18
# File 'lib/url_locale.rb', line 16

def path_locales
  @@path_locales ||= Regexp.new "^\/(#{translations*'|'})(\/|$)"
end

.translationsObject

Returns an I18n.available_locales - I18n.default_locale



6
7
8
# File 'lib/url_locale.rb', line 6

def translations
  @@translations ||= (I18n.available_locales - [I18n.default_locale]).map &:to_s
end