Sha256: f102a798b5a3efcf73f07f41b351285602124a17c9d07483b963e22098237d6c

Contents?: true

Size: 1.58 KB

Versions: 6

Compression:

Stored size: 1.58 KB

Contents

module Amalgam
  module UrlHelper
    def locale_url(locale)
      available_locales = ::I18n::available_locales.collect{|m| m.to_s.downcase}

      path_list = request.fullpath.split('/')
      if path_list.present?
        if available_locales.include?(path_list[1].downcase)
          path_list[1] = locale
          return "#{request.protocol}#{request.host_with_port}#{path_list.join('/')}"
        end
      else
        return "#{request.protocol}#{request.host_with_port}/#{locale}"
      end

      if locale_param = request.fullpath.match(/locale=[\w|-]*/)
        url = "#{request.protocol}#{request.host_with_port}#{request.fullpath}"
        url[locale_param[0]] = "locale=#{locale}"
        return url
      end

      domain_list = request.host_with_port.split('.')
      if available_locales.include?(domain_list.first.downcase)
        domain_list[0] = locale
        return "#{request.protocol}#{domain_list.join('.')}#{request.fullpath}"
      end

      if request.fullpath.include?('?')
        return "#{request.protocol}#{request.host_with_port}#{request.fullpath}&locale=#{locale}"
      else
        return "#{request.protocol}#{request.host_with_port}#{request.fullpath}?locale=#{locale}"
      end
    end

    def with_subdomain(subdomain)
      subdomain = (subdomain || "")
      subdomain += "." unless subdomain.empty?
      [subdomain, request.domain, request.port_string].join
    end

    def url_for(options = nil)
      if options.kind_of?(Hash) && options.has_key?(:subdomain)
        options[:host] = with_subdomain(options.delete(:subdomain))
      end
      super
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
amalgam-2.1.4.1 app/helpers/amalgam/url_helper.rb
amalgam-2.1.4 app/helpers/amalgam/url_helper.rb
amalgam-2.1.3.1 app/helpers/amalgam/url_helper.rb
amalgam-2.1.3 app/helpers/amalgam/url_helper.rb
amalgam-2.1.2 app/helpers/amalgam/url_helper.rb
amalgam-2.1.1 app/helpers/amalgam/url_helper.rb