Merge pull request #108 from warpc/105-internationalization

105 internationalization
This commit is contained in:
Pasha 2012-01-18 05:38:55 -08:00
commit eb6ec413da
7 changed files with 61 additions and 20 deletions

View File

@ -1,8 +1,10 @@
# coding: UTF-8 # coding: UTF-8
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
protect_from_forgery protect_from_forgery
layout :layout_by_resource layout :layout_by_resource
before_filter :set_locale
before_filter lambda { EventLog.current_controller = self }, before_filter lambda { EventLog.current_controller = self },
:only => [:create, :destroy, :open_id, :auto_build, :cancel, :publish, :change_visibility] # :update :only => [:create, :destroy, :open_id, :auto_build, :cancel, :publish, :change_visibility] # :update
after_filter lambda { EventLog.current_controller = nil } after_filter lambda { EventLog.current_controller = nil }
@ -14,26 +16,39 @@ class ApplicationController < ActionController::Base
end end
protected protected
def get_owner
def set_locale
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['user_id'] && User.find_by_id(params['user_id']) ||
# params['group_id'] && Group.find_by_id(params['group_id']) || current_user # params['group_id'] && Group.find_by_id(params['group_id']) || current_user
if self.class.method_defined? :parent if self.class.method_defined? :parent
if parent and (parent.is_a? User or parent.is_a? Group) if parent and (parent.is_a? User or parent.is_a? Group)
return parent return parent
else
return current_user
end
else else
params['user_id'] && User.find_by_id(params['user_id']) || return current_user
params['group_id'] && Group.find_by_id(params['group_id']) || current_user
end end
else
params['user_id'] && User.find_by_id(params['user_id']) ||
params['group_id'] && Group.find_by_id(params['group_id']) || current_user
end end
end
def layout_by_resource def layout_by_resource
if devise_controller? if devise_controller?
"sessions" "sessions"
else else
"application" "application"
end
end end
end
end end

View File

@ -1,5 +1,7 @@
class User < ActiveRecord::Base class User < ActiveRecord::Base
ROLES = ['admin'] ROLES = ['admin']
LANGUAGES_FOR_SELECT = [['Russian', 'ru'], ['English', 'en']]
LANGUAGES = LANGUAGES_FOR_SELECT.map(&:last)
devise :database_authenticatable, :registerable, :omniauthable, # :token_authenticatable, :encryptable, :timeoutable devise :database_authenticatable, :registerable, :omniauthable, # :token_authenticatable, :encryptable, :timeoutable
:recoverable, :rememberable, :validatable #, :trackable, :confirmable, :lockable :recoverable, :rememberable, :validatable #, :trackable, :confirmable, :lockable
@ -28,8 +30,9 @@ class User < ActiveRecord::Base
validate { errors.add(:uname, :taken) if Group.where('uname LIKE ?', uname).present? } validate { errors.add(:uname, :taken) if Group.where('uname LIKE ?', uname).present? }
validates :ssh_key, :uniqueness => true, :allow_blank => true validates :ssh_key, :uniqueness => true, :allow_blank => true
validates :role, :inclusion => {:in => ROLES}, :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 attr_accessible :email, :password, :password_confirmation, :remember_me, :login, :name, :ssh_key, :uname, :language
attr_readonly :uname attr_readonly :uname
attr_accessor :login attr_accessor :login

View File

@ -39,6 +39,12 @@
- else - else
= resource.role = resource.role
.group.wat-cf
.left
= f.label :language, :class => "label"
.right
= f.select :language, User::LANGUAGES_FOR_SELECT
/ .group.wat-cf / .group.wat-cf
/ .left / .left
/ = f.label :current_password, :class => "label" / = f.label :current_password, :class => "label"

View File

@ -37,6 +37,12 @@
.right .right
= f.password_field :password_confirmation, :class => "text_field" = f.password_field :password_confirmation, :class => "text_field"
.group.wat-cf
.left
= f.label :language, :class => "label"
.right
= f.select :language, User::LANGUAGES_FOR_SELECT
.group.navform.wat-cf .group.navform.wat-cf
%button.button{ :type => "submit" } %button.button{ :type => "submit" }
#{image_tag("web-app-theme/icons/tick.png", :alt => t("devise.registrations.sign_up_header"))} #{t("devise.registrations.sign_up_header")} #{image_tag("web-app-theme/icons/tick.png", :alt => t("devise.registrations.sign_up_header"))} #{t("devise.registrations.sign_up_header")}

View File

@ -637,6 +637,7 @@ ru:
created_at: Создан created_at: Создан
updated_at: Обновлен updated_at: Обновлен
role: Роль в системе role: Роль в системе
language: Язык
product_build_list: product_build_list:
id: Id id: Id

View File

@ -0,0 +1,9 @@
class AddLanguageToUsers < ActiveRecord::Migration
def self.up
add_column :users, :language, :string, :default => 'en'
end
def self.down
remove_column :users, :language
end
end

View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120113212924) do ActiveRecord::Schema.define(:version => 20120117110723) do
create_table "arches", :force => true do |t| create_table "arches", :force => true do |t|
t.string "name", :null => false t.string "name", :null => false
@ -295,9 +295,9 @@ ActiveRecord::Schema.define(:version => 20120113212924) do
create_table "users", :force => true do |t| create_table "users", :force => true do |t|
t.string "name" t.string "name"
t.string "email", :default => "", :null => false t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "password_salt", :default => "", :null => false t.string "password_salt", :default => "", :null => false
t.string "reset_password_token" t.string "reset_password_token"
t.string "remember_token" t.string "remember_token"
t.datetime "remember_created_at" t.datetime "remember_created_at"
@ -306,6 +306,7 @@ ActiveRecord::Schema.define(:version => 20120113212924) do
t.text "ssh_key" t.text "ssh_key"
t.string "uname" t.string "uname"
t.string "role" t.string "role"
t.string "language", :default => "en"
end end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true add_index "users", ["email"], :name => "index_users_on_email", :unique => true