-
Notifications
You must be signed in to change notification settings - Fork 84
Rails 2.x Localize filter
Ashley Engelund edited this page Nov 13, 2016
·
1 revision
Note: This page is out of date. It refers to Rails 2.x
Please use the instructions in the README for the localize filter.
Routes
map.filter 'locale'
application_controller.rb
before_filter :set_locale
def set_locale
locale = params[:locale] || cookies[:locale]
I18n.locale = locale.to_s
cookies[:locale] = locale unless (cookies[:locale] && cookies[:locale] == locale)
end
def default_url_options(options={})
{ :locale => I18n.locale }
end
RAILS_ROOT/lib/routing.rb
require 'routing_filter/filter'
module RoutingFilter
class Locale < Filter
def around_recognize(path, env, &block)
locale = nil
path.sub! %r(^/([a-zA-Z]{2}|[a-zA-Z]{2}-[a-zA-Z]{2})(?=/|$)) do locale = $1; '' end
returning yield do |params|
params[:locale] = locale || I18n.default_locale
end
end
def around_generate(*args, &block)
locale = args.extract_options!.delete(:locale) || I18n.default_locale
returning yield do |result|
if locale != I18n.default_locale
result.sub!(%r(^(http.?://[^/]*)?(.*))){ "#{$1}/#{locale}#{$2}" }
end
end
end
end
end
environment.rb
require "#{Rails.root}/lib/routing.rb"
That’s it. Now you have localized routes!