Add user locale setting

This commit is contained in:
Eugen Rochko 2016-11-16 17:51:02 +01:00
parent 01e43c3e57
commit 2c766bd4b4
10 changed files with 49 additions and 13 deletions

View File

@ -14,20 +14,23 @@ code {
margin-bottom: 15px;
}
.input.file {
.input.file, .input.select {
padding: 15px 0;
margin-bottom: 0;
display: flex;
label {
font-family: 'Roboto';
font-size: 16px;
color: #fff;
width: 100px;
display: inline-block;
display: block;
flex: 0 0 auto;
padding-top: 5px;
}
input[type=file] {
width: 280px;
input[type=file], select {
flex: 1 1 auto;
}
}
@ -42,11 +45,14 @@ code {
font-family: 'Roboto';
font-size: 14px;
color: #9baec8;
display: block;
}
input[type=checkbox] {
display: inline-block;
margin-bottom: -13px;
position: relative;
top: 3px;
margin-right: 8px;
}
}

View File

@ -13,6 +13,7 @@ class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, with: :not_found
before_action :store_current_location, except: :raise_not_found, unless: :devise_controller?
before_action :set_locale, if: 'user_signed_in?'
def raise_not_found
raise ActionController::RoutingError, "No route matches #{params[:unmatched_route]}"
@ -24,6 +25,12 @@ class ApplicationController < ActionController::Base
store_location_for(:user, request.url)
end
def set_locale
I18n.locale = current_user.locale || I18n.default_locale
rescue I18n::InvalidLocale
I18n.locale = I18n.default_locale
end
protected
def not_found

View File

@ -14,7 +14,7 @@ class Settings::PreferencesController < ApplicationController
current_user.settings(:notification_emails).favourite = user_params[:notification_emails][:favourite] == '1'
current_user.settings(:notification_emails).mention = user_params[:notification_emails][:mention] == '1'
if current_user.save
if current_user.update(user_params.except(:notification_emails))
redirect_to settings_preferences_path, notice: I18n.t('generic.changes_saved_msg')
else
render action: :show
@ -24,6 +24,6 @@ class Settings::PreferencesController < ApplicationController
private
def user_params
params.require(:user).permit(notification_emails: [:follow, :reblog, :favourite, :mention])
params.require(:user).permit(:locale, notification_emails: [:follow, :reblog, :favourite, :mention])
end
end

View File

@ -8,7 +8,10 @@ class NotificationMailer < ApplicationMailer
@status = status
return unless @me.user.settings(:notification_emails).mention
mail to: @me.user.email, subject: I18n.t('notification_mailer.mention.subject', name: @status.account.acct)
I18n.with_locale(@me.user.locale || I18n.default_locale) do
mail to: @me.user.email, subject: I18n.t('notification_mailer.mention.subject', name: @status.account.acct)
end
end
def follow(followed_account, follower)
@ -16,7 +19,10 @@ class NotificationMailer < ApplicationMailer
@account = follower
return unless @me.user.settings(:notification_emails).follow
mail to: @me.user.email, subject: I18n.t('notification_mailer.follow.subject', name: @account.acct)
I18n.with_locale(@me.user.locale || I18n.default_locale) do
mail to: @me.user.email, subject: I18n.t('notification_mailer.follow.subject', name: @account.acct)
end
end
def favourite(target_status, from_account)
@ -25,7 +31,10 @@ class NotificationMailer < ApplicationMailer
@status = target_status
return unless @me.user.settings(:notification_emails).favourite
mail to: @me.user.email, subject: I18n.t('notification_mailer.favourite.subject', name: @account.acct)
I18n.with_locale(@me.user.locale || I18n.default_locale) do
mail to: @me.user.email, subject: I18n.t('notification_mailer.favourite.subject', name: @account.acct)
end
end
def reblog(target_status, from_account)
@ -34,6 +43,9 @@ class NotificationMailer < ApplicationMailer
@status = target_status
return unless @me.user.settings(:notification_emails).reblog
mail to: @me.user.email, subject: I18n.t('notification_mailer.reblog.subject', name: @account.acct)
I18n.with_locale(@me.user.locale || I18n.default_locale) do
mail to: @me.user.email, subject: I18n.t('notification_mailer.reblog.subject', name: @account.acct)
end
end
end

View File

@ -7,6 +7,7 @@ class User < ApplicationRecord
accepts_nested_attributes_for :account
validates :account, presence: true
validates :locale, inclusion: I18n.available_locales.map(&:to_s), unless: 'locale.nil?'
scope :prolific, -> { joins('inner join statuses on statuses.account_id = users.account_id').select('users.*, count(statuses.id) as statuses_count').group('users.id').order('statuses_count desc') }
scope :recent, -> { order('id desc') }

View File

@ -4,6 +4,8 @@
= simple_form_for current_user, url: settings_preferences_path, html: { method: :put } do |f|
= render 'shared/error_messages', object: current_user
= f.input :locale, collection: I18n.available_locales, wrapper: :with_label, include_blank: false
= f.simple_fields_for :notification_emails, current_user.settings(:notification_emails) do |ff|
= ff.input :follow, as: :boolean, wrapper: :with_label
= ff.input :reblog, as: :boolean, wrapper: :with_label

View File

@ -20,7 +20,8 @@ module Mastodon
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.i18n.available_locales = [:en]
config.i18n.default_locale = :en
# config.paths.add File.join('app', 'api'), glob: File.join('**', '*.rb')
# config.autoload_paths += Dir[Rails.root.join('app', 'api', '*')]

View File

@ -12,6 +12,7 @@ en:
display_name: Display name
email: E-mail address
header: Header
locale: Language
new_password: New password
note: Bio
password: Password

View File

@ -0,0 +1,5 @@
class AddLocaleToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :locale, :string
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20161105130633) do
ActiveRecord::Schema.define(version: 20161116162355) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -203,6 +203,7 @@ ActiveRecord::Schema.define(version: 20161105130633) do
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.string "locale"
t.index ["account_id"], name: "index_users_on_account_id", using: :btree
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree