Permalink
Browse files

scopes in routing

  • Loading branch information...
1 parent af7d503 commit a8ef60eedb26b4ee94621ef9db50186e077a8924 Alper committed Feb 24, 2012
Showing with 24 additions and 8 deletions.
  1. +14 −1 README.rdoc
  2. +6 −2 app/controllers/application_controller.rb
  3. +4 −5 config/routes.rb
View
@@ -9,4 +9,17 @@ Used geoip gem, with GeoLite Country. ip => country, country => timezone(s). Thi
== Localization
-First, look if params exists (user may require any different locale, for any time, for any reason), next, check if current_user exists, if so, use its settings, if its public user, that try to guess from browser header, and finally set locale from default locale.
+First, look if params exists (user may require any different locale, for any time, for any reason), next, check if current_user exists, if so, use its settings, if its public user, that try to guess from browser header, and finally set locale from default locale.
+
+ I18n.locale = params[:locale] || current_user.try(:locale) || extract_locale_from_accept_language_header
+ private
+ def extract_locale_from_accept_language_header
+ request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[en|tr]{2}/).first || I18n.default_locale
+ end
+
+== Locale scope in routes
+
+Dynamic scope for locales
+
+ scope "(:locale)", :locale => /en|tr/ do
+ end
@@ -1,16 +1,20 @@
class ApplicationController < ActionController::Base
protect_from_forgery
- before_filter :set_locale
+ before_filter :set_locale, :set_user_timezone
def set_locale
I18n.locale = params[:locale] || current_user.try(:locale) || extract_locale_from_accept_language_header
end
private
+ def set_user_timezone
+ Time.zone = current_user.try(:time_zone)
+ end
+
def extract_locale_from_accept_language_header
- request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[EN|TR]{2}/).first || I18n.default_locale
+ request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[en|tr]{2}/).first || I18n.default_locale
end
end
View
@@ -1,10 +1,9 @@
Int::Application.routes.draw do
- devise_for :users
-
- get "main/index"
-
- root :to => "main#index"
+ scope "(:locale)", :locale => /en|tr/ do
+ devise_for :users
+ root :to => "main#index"
+ end
# The priority is based upon order of creation:
# first created -> highest priority.

0 comments on commit a8ef60e

Please sign in to comment.