about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2016-11-16 17:51:02 +0100
committerEugen Rochko <eugen@zeonfederated.com>2016-11-16 17:56:31 +0100
commit2c766bd4b4c6dcf8e7c9a6dd9421edca0de57aba (patch)
treed84f472de5ff84502928fec8582be492c45a6faf
parent01e43c3e5799b575a70798056945365ddf51f3ad (diff)
Add user locale setting
-rw-r--r--app/assets/stylesheets/forms.scss16
-rw-r--r--app/controllers/application_controller.rb7
-rw-r--r--app/controllers/settings/preferences_controller.rb4
-rw-r--r--app/mailers/notification_mailer.rb20
-rw-r--r--app/models/user.rb1
-rw-r--r--app/views/settings/preferences/show.html.haml2
-rw-r--r--config/application.rb3
-rw-r--r--config/locales/simple_form.en.yml1
-rw-r--r--db/migrate/20161116162355_add_locale_to_users.rb5
-rw-r--r--db/schema.rb3
10 files changed, 49 insertions, 13 deletions
diff --git a/app/assets/stylesheets/forms.scss b/app/assets/stylesheets/forms.scss
index ad2e30b61..306f474d6 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 c7a99b22f..f9aeb127a 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 4dc2cd61f..5be8719ae 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 33bea4c79..cf5ad3f92 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 4a330d8ea..4eb1d20a5 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 21a6dbd5d..60608136f 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 6525571cc..e992c2481 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 ef69f7569..bd22a1f3d 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 000000000..ec1a42ef1
--- /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 a2d05b1bd..84f75c278 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