Module: SubdomainLocale::UrlFor

Defined in:
lib/subdomain_locale/url_for.rb

Instance Method Summary collapse

Instance Method Details

#current_localeObject



27
28
29
# File 'lib/subdomain_locale/url_for.rb', line 27

def current_locale
  I18n.locale
end

#subdomain_localesObject



31
32
33
# File 'lib/subdomain_locale/url_for.rb', line 31

def subdomain_locales
  SubdomainLocale.mapping
end

#url_for(options = nil) ⇒ Object

Makes url_for(locale: ‘ru’) the same as url_for(subdomain: ‘ru’, only_path: false) That way you can easily swap locale in subdomain with locale in the path.

    1. assuming you have scope ":locale" in your routes:

url_for params.merge(locale: 'ru') # => /ru/current_path

After including this module:

url_for params.merge(locale: 'ru') # => http://ru.example.com/current_path


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/subdomain_locale/url_for.rb', line 10

def url_for(options=nil)
  if options.is_a?(Hash)
    options = options.dup
    if options.key?(:locale)
      # Locale specified, force full URL
      locale = options.delete(:locale)
      options[:subdomain] = subdomain_locales.subdomain_for(locale)
      options[:only_path] = false
    elsif options.key?(:only_path) && !options[:only_path] && !options.key?(:subdomain)
      # Requested full URL, use current locale
      options[:subdomain] = subdomain_locales.subdomain_for(current_locale)
    end
  end

  super
end