Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

scopes in routing

  • Loading branch information...
commit a8ef60eedb26b4ee94621ef9db50186e077a8924 1 parent af7d503
Alper authored
15 README.rdoc
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
8 app/controllers/application_controller.rb
View
@@ -1,7 +1,7 @@
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
@@ -9,8 +9,12 @@ def set_locale
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
9 config/routes.rb
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.
Please sign in to comment.
Something went wrong with that request. Please try again.