Module: SubdomainLocale::UrlFor

Defined in:
lib/subdomain_locale/url_for.rb

Instance Method Summary collapse

Instance Method Details

#current_localeObject



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

def current_locale
  I18n.locale
end

#default_url_optionsObject



22
23
24
25
26
# File 'lib/subdomain_locale/url_for.rb', line 22

def default_url_options
  super.merge({
    subdomain: subdomain_locales.subdomain_for(current_locale)
  })
end

#subdomain_localesObject



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

def subdomain_locales
  SubdomainLocale.mapping
end

#url_for(options) ⇒ 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
# File 'lib/subdomain_locale/url_for.rb', line 10

def url_for(options)
  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
  end

  super
end