From 726eddc40e85a73072895fa2ce336e50f3aa472a Mon Sep 17 00:00:00 2001 From: "konstantin.grabar" Date: Tue, 17 Jan 2012 15:48:40 +0400 Subject: [PATCH] [refs #105] Add internationalization support to project --- app/controllers/application_controller.rb | 45 ++++++++++++------- app/models/user.rb | 3 +- app/views/devise/registrations/edit.html.haml | 6 +++ app/views/devise/registrations/new.html.haml | 6 +++ config/locales/ru.yml | 1 + .../20120117110723_add_language_to_users.rb | 9 ++++ db/schema.rb | 9 ++-- 7 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 db/migrate/20120117110723_add_language_to_users.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4a64259cd..ebd709918 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,8 +1,10 @@ # coding: UTF-8 class ApplicationController < ActionController::Base protect_from_forgery + layout :layout_by_resource + before_filter :set_locale before_filter lambda { EventLog.current_controller = self }, :only => [:create, :destroy, :open_id, :auto_build, :cancel, :publish, :change_visibility] # :update after_filter lambda { EventLog.current_controller = nil } @@ -14,26 +16,39 @@ class ApplicationController < ActionController::Base end protected - def get_owner + + 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 + end + + def get_user_locale + user_signed_in? ? current_user.language : nil + 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 - if self.class.method_defined? :parent - if parent and (parent.is_a? User or parent.is_a? Group) - return parent - else - return current_user - end + if self.class.method_defined? :parent + if parent and (parent.is_a? User or parent.is_a? Group) + return parent else - params['user_id'] && User.find_by_id(params['user_id']) || - params['group_id'] && Group.find_by_id(params['group_id']) || current_user + return current_user 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 - def layout_by_resource - if devise_controller? - "sessions" - else - "application" - end + def layout_by_resource + if devise_controller? + "sessions" + else + "application" end + end end diff --git a/app/models/user.rb b/app/models/user.rb index c246dd6f9..e0de8317a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,5 +1,6 @@ class User < ActiveRecord::Base ROLES = ['admin'] + LANGUAGES = [['Russian', 'ru'], ['English', 'en']] devise :database_authenticatable, :registerable, :omniauthable, # :token_authenticatable, :encryptable, :timeoutable :recoverable, :rememberable, :validatable #, :trackable, :confirmable, :lockable @@ -29,7 +30,7 @@ class User < ActiveRecord::Base validates :ssh_key, :uniqueness => true, :allow_blank => true validates :role, :inclusion => {:in => ROLES}, :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_accessor :login diff --git a/app/views/devise/registrations/edit.html.haml b/app/views/devise/registrations/edit.html.haml index c4b6052ab..fc441a6c5 100644 --- a/app/views/devise/registrations/edit.html.haml +++ b/app/views/devise/registrations/edit.html.haml @@ -39,6 +39,12 @@ - else = resource.role + .group.wat-cf + .left + = f.label :language, :class => "label" + .right + = f.select :language, User::LANGUAGES + / .group.wat-cf / .left / = f.label :current_password, :class => "label" diff --git a/app/views/devise/registrations/new.html.haml b/app/views/devise/registrations/new.html.haml index aeddc6958..5cc31a6c8 100644 --- a/app/views/devise/registrations/new.html.haml +++ b/app/views/devise/registrations/new.html.haml @@ -37,6 +37,12 @@ .right = f.password_field :password_confirmation, :class => "text_field" + .group.wat-cf + .left + = f.label :language, :class => "label" + .right + = f.select :language, User::LANGUAGES + .group.navform.wat-cf %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")} diff --git a/config/locales/ru.yml b/config/locales/ru.yml index f5187e225..8ee23a3e1 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -648,6 +648,7 @@ ru: created_at: Создан updated_at: Обновлен role: Роль в системе + language: Язык product_build_list: id: Id diff --git a/db/migrate/20120117110723_add_language_to_users.rb b/db/migrate/20120117110723_add_language_to_users.rb new file mode 100644 index 000000000..4bf0cec48 --- /dev/null +++ b/db/migrate/20120117110723_add_language_to_users.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 37e09fcc4..55bf19f7e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120113151305) do +ActiveRecord::Schema.define(:version => 20120117110723) do create_table "arches", :force => true do |t| t.string "name", :null => false @@ -294,9 +294,9 @@ ActiveRecord::Schema.define(:version => 20120113151305) do create_table "users", :force => true do |t| t.string "name" - t.string "email", :default => "", :null => false - t.string "encrypted_password", :limit => 128, :default => "", :null => false - t.string "password_salt", :default => "", :null => false + t.string "email", :default => "", :null => false + t.string "encrypted_password", :limit => 128, :default => "", :null => false + t.string "password_salt", :default => "", :null => false t.string "reset_password_token" t.string "remember_token" t.datetime "remember_created_at" @@ -305,6 +305,7 @@ ActiveRecord::Schema.define(:version => 20120113151305) do t.text "ssh_key" t.string "uname" t.string "role" + t.string "language", :default => "en" end add_index "users", ["email"], :name => "index_users_on_email", :unique => true