Home > Ruby > Radiant & Globalize: Redirecting the user to their language site
Jan
09


After installing Radiant 0.9 and Globalize, I realized I had to redirect the user to their language site.


So, if the user has been assigned to the locale en, when he vist http://mysite.com he should be redirected to http://mysite.com/en


Why? Because I had some files under images with locales, and I had some flash issues that were not going to work from the homepage address.


So I ended updating the file vendor/extensions/globalize2/lib/globalize2/site_controller_extensions.rb

module Globalize2
  module SiteControllerExtensions
    def self.included(base)
      base.class_eval do
        alias_method_chain :find_page, :globalize

        alias_method :o riginal_show_page, :show_page
        alias_method :show_page, :check_homepage_redir

      end
    end

    def check_homepage_redir
      url = params[:url]
      if Array === url
        url = url.join('/')
      else
        url = url.to_s
      end

      if url == '/'
        locale = params[:locale] || cookies[:locale] || session[:locale] || Globalize2Extension.ip_lookup(request.remote_ip)
        redirect_to CGI.unescape('/' + locale + '/') and return
      end

      original_show_page
    end

    def find_page_with_globalize(url)
      globalized_url = '/' + I18n.locale + '/' + url
      find_page_without_globalize(globalized_url)
    end
  end
end
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn

Add reply