diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ebd709918..b304e8821 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -18,17 +18,17 @@ class ApplicationController < ActionController::Base protected def set_locale - I18n.locale = extract_locale_from_request - end - - def extract_locale_from_request - get_user_locale || request.env['HTTP_ACCEPT_LANGUAGE'].to_sym || :en + I18n.locale = check_locale( get_user_locale || request.env['HTTP_ACCEPT_LANGUAGE'].to_sym ) end def get_user_locale user_signed_in? ? current_user.language : nil end + def check_locale(locale) + User::LANGUAGES.include?(locale.to_s) ? locale : :en + end + def get_owner # params['user_id'] && User.find_by_id(params['user_id']) || # params['group_id'] && Group.find_by_id(params['group_id']) || current_user diff --git a/app/models/user.rb b/app/models/user.rb index e0de8317a..fbd14bdb4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,7 @@ class User < ActiveRecord::Base ROLES = ['admin'] - LANGUAGES = [['Russian', 'ru'], ['English', 'en']] + LANGUAGES = ['ru', 'en'] + LANGUAGES_FOR_SELECT = [['Russian', 'ru'], ['English', 'en']] devise :database_authenticatable, :registerable, :omniauthable, # :token_authenticatable, :encryptable, :timeoutable :recoverable, :rememberable, :validatable #, :trackable, :confirmable, :lockable @@ -29,6 +30,7 @@ class User < ActiveRecord::Base validate { errors.add(:uname, :taken) if Group.where('uname LIKE ?', uname).present? } validates :ssh_key, :uniqueness => true, :allow_blank => true validates :role, :inclusion => {:in => ROLES}, :allow_blank => true + validates :language, :inclusion => {:in => LANGUAGES}, :allow_blank => true attr_accessible :email, :password, :password_confirmation, :remember_me, :login, :name, :ssh_key, :uname, :language attr_readonly :uname diff --git a/app/views/devise/registrations/edit.html.haml b/app/views/devise/registrations/edit.html.haml index fc441a6c5..b983f1836 100644 --- a/app/views/devise/registrations/edit.html.haml +++ b/app/views/devise/registrations/edit.html.haml @@ -43,7 +43,7 @@ .left = f.label :language, :class => "label" .right - = f.select :language, User::LANGUAGES + = f.select :language, User::LANGUAGES_FOR_SELECT / .group.wat-cf / .left diff --git a/app/views/devise/registrations/new.html.haml b/app/views/devise/registrations/new.html.haml index 5cc31a6c8..5c9d0de5d 100644 --- a/app/views/devise/registrations/new.html.haml +++ b/app/views/devise/registrations/new.html.haml @@ -41,7 +41,7 @@ .left = f.label :language, :class => "label" .right - = f.select :language, User::LANGUAGES + = f.select :language, User::LANGUAGES_FOR_SELECT .group.navform.wat-cf %button.button{ :type => "submit" }