Module: SubdomainLocale::UrlFor
- Defined in:
- lib/subdomain_locale/url_for.rb
Instance Method Summary collapse
- #current_locale ⇒ Object
- #subdomain_locales ⇒ Object
-
#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.
Instance Method Details
#current_locale ⇒ Object
27 28 29 |
# File 'lib/subdomain_locale/url_for.rb', line 27 def current_locale I18n.locale end |
#subdomain_locales ⇒ Object
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.
-
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(=nil) if .is_a?(Hash) = .dup if .key?(:locale) # Locale specified, force full URL locale = .delete(:locale) [:subdomain] = subdomain_locales.subdomain_for(locale) [:only_path] = false elsif .key?(:only_path) && ![:only_path] && !.key?(:subdomain) # Requested full URL, use current locale [:subdomain] = subdomain_locales.subdomain_for(current_locale) end end super end |