allow user to change his language in his settings
This commit is contained in:
parent
6e1121179d
commit
b7a586b1a5
9 changed files with 92 additions and 1 deletions
|
|
@ -3,6 +3,7 @@
|
||||||
# the COPYRIGHT file.
|
# the COPYRIGHT file.
|
||||||
|
|
||||||
class ApplicationController < ActionController::Base
|
class ApplicationController < ActionController::Base
|
||||||
|
include LanguageHelper
|
||||||
|
|
||||||
protect_from_forgery :except => :receive
|
protect_from_forgery :except => :receive
|
||||||
|
|
||||||
|
|
@ -36,6 +37,10 @@ class ApplicationController < ActionController::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_locale
|
def set_locale
|
||||||
I18n.locale = request.compatible_language_from I18n.available_locales
|
if current_user
|
||||||
|
I18n.locale = current_user.language
|
||||||
|
else
|
||||||
|
I18n.locale = request.compatible_language_from AVAILABLE_LANGUAGE_CODES
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ class UsersController < ApplicationController
|
||||||
else
|
else
|
||||||
params[:user].delete(:password) if params[:user][:password].blank?
|
params[:user].delete(:password) if params[:user][:password].blank?
|
||||||
params[:user].delete(:password_confirmation) if params[:user][:password].blank? and params[:user][:password_confirmation].blank?
|
params[:user].delete(:password_confirmation) if params[:user][:password].blank? and params[:user][:password_confirmation].blank?
|
||||||
|
params[:user].delete(:language) if params[:user][:language].blank?
|
||||||
|
|
||||||
if params[:user][:password] && params[:user][:password_confirmation]
|
if params[:user][:password] && params[:user][:password_confirmation]
|
||||||
if @user.update_attributes(:password => params[:user][:password], :password_confirmation => params[:user][:password_confirmation])
|
if @user.update_attributes(:password => params[:user][:password], :password_confirmation => params[:user][:password_confirmation])
|
||||||
|
|
@ -36,6 +37,12 @@ class UsersController < ApplicationController
|
||||||
else
|
else
|
||||||
flash[:error] = "Password Change Failed"
|
flash[:error] = "Password Change Failed"
|
||||||
end
|
end
|
||||||
|
elsif params[:user][:language]
|
||||||
|
if @user.update_attributes(:language => params[:user][:language])
|
||||||
|
flash[:notice] = "Language Changed"
|
||||||
|
else
|
||||||
|
flash[:error] = "Language Change Failed"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
redirect_to edit_user_path(@user)
|
redirect_to edit_user_path(@user)
|
||||||
|
|
|
||||||
9
app/helpers/language_helper.rb
Normal file
9
app/helpers/language_helper.rb
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
module LanguageHelper
|
||||||
|
def available_language_options
|
||||||
|
options = []
|
||||||
|
AVAILABLE_LANGUAGES.each do |locale, language|
|
||||||
|
options << [language, locale]
|
||||||
|
end
|
||||||
|
options.sort_by { |o| o[0] }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -19,6 +19,7 @@ class User
|
||||||
include MongoMapper::Document
|
include MongoMapper::Document
|
||||||
include Diaspora::UserModules
|
include Diaspora::UserModules
|
||||||
include Encryptor::Private
|
include Encryptor::Private
|
||||||
|
include LanguageHelper
|
||||||
|
|
||||||
plugin MongoMapper::Devise
|
plugin MongoMapper::Devise
|
||||||
|
|
||||||
|
|
@ -42,11 +43,15 @@ class User
|
||||||
|
|
||||||
key :getting_started, Boolean, :default => true
|
key :getting_started, Boolean, :default => true
|
||||||
|
|
||||||
|
key :language, String
|
||||||
|
|
||||||
before_validation :strip_username, :on => :create
|
before_validation :strip_username, :on => :create
|
||||||
|
before_validation :set_current_language, :on => :create
|
||||||
validates_presence_of :username
|
validates_presence_of :username
|
||||||
validates_uniqueness_of :username, :case_sensitive => false
|
validates_uniqueness_of :username, :case_sensitive => false
|
||||||
validates_format_of :username, :with => /\A[A-Za-z0-9_.]+\z/
|
validates_format_of :username, :with => /\A[A-Za-z0-9_.]+\z/
|
||||||
validates_with InvitedUserValidator
|
validates_with InvitedUserValidator
|
||||||
|
validates_inclusion_of :language, :in => AVAILABLE_LANGUAGE_CODES
|
||||||
|
|
||||||
one :person, :class_name => 'Person', :foreign_key => :owner_id
|
one :person, :class_name => 'Person', :foreign_key => :owner_id
|
||||||
validate :person_is_valid
|
validate :person_is_valid
|
||||||
|
|
@ -75,6 +80,10 @@ class User
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def set_current_language
|
||||||
|
self.language = I18n.locale.to_s if self.language.blank?
|
||||||
|
end
|
||||||
|
|
||||||
def self.find_for_authentication(conditions={})
|
def self.find_for_authentication(conditions={})
|
||||||
if conditions[:username] =~ /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i # email regex
|
if conditions[:username] =~ /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i # email regex
|
||||||
conditions[:email] = conditions.delete(:username)
|
conditions[:email] = conditions.delete(:username)
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,16 @@
|
||||||
or
|
or
|
||||||
= f.submit 'Change password'
|
= f.submit 'Change password'
|
||||||
|
|
||||||
|
%h3 Change language
|
||||||
|
= form_for @user do |f|
|
||||||
|
= f.error_messages
|
||||||
|
|
||||||
|
%p
|
||||||
|
= f.select :language, available_language_options
|
||||||
|
= f.submit 'Change language'
|
||||||
|
|
||||||
|
%br
|
||||||
|
|
||||||
%h3 Export Data
|
%h3 Export Data
|
||||||
= link_to "download my xml", users_export_path, :class => "button"
|
= link_to "download my xml", users_export_path, :class => "button"
|
||||||
= link_to "download my photos", users_export_photos_path, :class => "button"
|
= link_to "download my photos", users_export_photos_path, :class => "button"
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,18 @@ if File.exists?(File.expand_path("./config/fb_config.yml"))
|
||||||
else
|
else
|
||||||
FACEBOOK = false
|
FACEBOOK = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if File.exists?(File.expand_path("./config/languages.yml"))
|
||||||
|
languages = YAML::load(File.open(File.expand_path("./config/languages.yml")))
|
||||||
|
AVAILABLE_LANGUAGES = languages['available']
|
||||||
|
DEFAULT_LANGUAGE = languages['default']
|
||||||
|
AVAILABLE_LANGUAGE_CODES = languages['available'].keys.map { |v| v.to_s}
|
||||||
|
else
|
||||||
|
AVAILABLE_LANGUAGES = { :en => 'English' }
|
||||||
|
DEFAULT_LANGUAGES = 'en'
|
||||||
|
AVAILABLE_LANGUAGE_CODES = ['en']
|
||||||
|
end
|
||||||
|
|
||||||
# Initialize the rails application
|
# Initialize the rails application
|
||||||
Diaspora::Application.initialize!
|
Diaspora::Application.initialize!
|
||||||
|
|
||||||
|
|
|
||||||
17
config/languages.yml
Normal file
17
config/languages.yml
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
default: 'en'
|
||||||
|
available:
|
||||||
|
cs: 'Lietuviškai'
|
||||||
|
cy: 'Cymraeg'
|
||||||
|
de: 'Deutsch'
|
||||||
|
en: 'English'
|
||||||
|
es: 'Español'
|
||||||
|
fr: 'Français'
|
||||||
|
id: 'Bahasa Indonesia'
|
||||||
|
it: 'Italiano'
|
||||||
|
nb: 'Norske'
|
||||||
|
nl: 'Nederlandse'
|
||||||
|
pt-BR: 'Português'
|
||||||
|
ro: 'Română'
|
||||||
|
ru: 'Россию'
|
||||||
|
sv: 'Svenska'
|
||||||
|
tr: 'Türk'
|
||||||
|
|
@ -10,6 +10,7 @@ describe UsersController do
|
||||||
let!(:aspect) { user.aspect(:name => "lame-os") }
|
let!(:aspect) { user.aspect(:name => "lame-os") }
|
||||||
|
|
||||||
let!(:old_password) { user.encrypted_password }
|
let!(:old_password) { user.encrypted_password }
|
||||||
|
let!(:old_language) { user.language }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
sign_in :user, user
|
sign_in :user, user
|
||||||
|
|
@ -47,5 +48,13 @@ describe UsersController do
|
||||||
user.encrypted_password.should == old_password
|
user.encrypted_password.should == old_password
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'language' do
|
||||||
|
it 'should allow user to change his language' do
|
||||||
|
put("update", :id => user.id, "user" => {"language" => "fr"})
|
||||||
|
user.reload
|
||||||
|
user.language.should_not == old_language
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -91,6 +91,19 @@ describe User do
|
||||||
duplicate_user.should_not be_valid
|
duplicate_user.should_not be_valid
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "of language" do
|
||||||
|
it "requires availability" do
|
||||||
|
user = Factory.build(:user, :language => 'some invalid language')
|
||||||
|
user.should_not be_valid
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should save with current language if blank" do
|
||||||
|
I18n.locale = :fr
|
||||||
|
user = Factory(:user, :language => nil)
|
||||||
|
user.language.should == 'fr'
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ".build" do
|
describe ".build" do
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue