From 2c766bd4b4c6dcf8e7c9a6dd9421edca0de57aba Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Wed, 16 Nov 2016 17:51:02 +0100 Subject: [PATCH] Add user locale setting --- app/assets/stylesheets/forms.scss | 16 ++++++++++----- app/controllers/application_controller.rb | 7 +++++++ .../settings/preferences_controller.rb | 4 ++-- app/mailers/notification_mailer.rb | 20 +++++++++++++++---- app/models/user.rb | 1 + app/views/settings/preferences/show.html.haml | 2 ++ config/application.rb | 3 ++- config/locales/simple_form.en.yml | 1 + .../20161116162355_add_locale_to_users.rb | 5 +++++ db/schema.rb | 3 ++- 10 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 db/migrate/20161116162355_add_locale_to_users.rb diff --git a/app/assets/stylesheets/forms.scss b/app/assets/stylesheets/forms.scss index ad2e30b613..306f474d6f 100644 --- a/app/assets/stylesheets/forms.scss +++ b/app/assets/stylesheets/forms.scss @@ -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; } } diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c7a99b22f0..f9aeb127aa 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/settings/preferences_controller.rb b/app/controllers/settings/preferences_controller.rb index 4dc2cd61f6..5be8719aef 100644 --- a/app/controllers/settings/preferences_controller.rb +++ b/app/controllers/settings/preferences_controller.rb @@ -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 diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index 33bea4c799..cf5ad3f929 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 4a330d8eac..4eb1d20a56 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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') } diff --git a/app/views/settings/preferences/show.html.haml b/app/views/settings/preferences/show.html.haml index 21a6dbd5da..60608136fb 100644 --- a/app/views/settings/preferences/show.html.haml +++ b/app/views/settings/preferences/show.html.haml @@ -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 diff --git a/config/application.rb b/config/application.rb index 6525571cc7..e992c2481b 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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', '*')] diff --git a/config/locales/simple_form.en.yml b/config/locales/simple_form.en.yml index ef69f75696..bd22a1f3d3 100644 --- a/config/locales/simple_form.en.yml +++ b/config/locales/simple_form.en.yml @@ -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 diff --git a/db/migrate/20161116162355_add_locale_to_users.rb b/db/migrate/20161116162355_add_locale_to_users.rb new file mode 100644 index 0000000000..ec1a42ef1e --- /dev/null +++ b/db/migrate/20161116162355_add_locale_to_users.rb @@ -0,0 +1,5 @@ +class AddLocaleToUsers < ActiveRecord::Migration[5.0] + def change + add_column :users, :locale, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index a2d05b1bd2..84f75c2789 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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