Module: Padrino::Localization::Urls::ClassMethods

Defined in:
lib/padrino-localization.rb

Instance Method Summary collapse

Instance Method Details

Returns a link, unless application locale is equivalent to those from URL.

Examples:

link_to_unless_current_locale :ru, "Main page", url(:main, :index, :locale => :ru)
# => "Main page"

link_to_unless_current_locale :ru, "Main page", url(:main, :index, :locale => :ru)
# => "Main page"


52
53
54
55
56
57
58
59
# File 'lib/padrino-localization.rb', line 52

def link_to_unless_current_locale *opts, &block
  locale = opts.shift.to_s
  if locale == ::I18n.locale.to_s
    opts.first
  else
    link_to *opts, &block
  end
end

#url_with_locale(*args) ⇒ Object

Generates an URL with a current locale, for a resource, inside of application.

Example:

url_for_with_locale(:users) # /ru/users

url_for_with_locale('/') # /ru/

url_for_with_locale(:posts, :new, :locale => 'se') # /se/posts/new


31
32
33
34
35
36
37
38
39
# File 'lib/padrino-localization.rb', line 31

def url_with_locale *args
  opts = args.extract_options!
  locale_scope = opts.delete(:locale) || ::I18n.locale
  locale_scope = ::I18n.locale unless Padrino::Localization::Middleware.languages.include?(locale_scope.to_s)
  args << opts
  link = url_without_locale *args
  link.insert(0,"/#{locale_scope}") if (locale_scope.to_s != ::I18n.default_locale.to_s)
  link
end