From f91fa1080453e486750e2a26206222eecffd7047 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sun, 17 Jun 2018 18:57:31 +0200 Subject: [PATCH] Allow selecting default posting language instead of auto-detect (#7828) * Allow selecting default posting language instead of auto-detect * Enable default language setting in credentials API * Fix form saving --- app/controllers/api/v1/accounts/credentials_controller.rb | 1 + app/controllers/settings/preferences_controller.rb | 1 + app/javascript/styles/mastodon/forms.scss | 2 ++ app/lib/user_settings_decorator.rb | 5 +++++ app/models/user.rb | 2 +- app/serializers/rest/credential_account_serializer.rb | 1 + app/services/post_status_service.rb | 2 +- app/views/settings/preferences/show.html.haml | 2 ++ config/locales/en.yml | 1 + config/locales/simple_form.en.yml | 5 ++++- 10 files changed, 19 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v1/accounts/credentials_controller.rb b/app/controllers/api/v1/accounts/credentials_controller.rb index 259d07be87..2d0737ee46 100644 --- a/app/controllers/api/v1/accounts/credentials_controller.rb +++ b/app/controllers/api/v1/accounts/credentials_controller.rb @@ -32,6 +32,7 @@ class Api::V1::Accounts::CredentialsController < Api::BaseController { 'setting_default_privacy' => source_params.fetch(:privacy, @account.user.setting_default_privacy), 'setting_default_sensitive' => source_params.fetch(:sensitive, @account.user.setting_default_sensitive), + 'setting_default_language' => source_params.fetch(:language, @account.user.setting_default_language), } end end diff --git a/app/controllers/settings/preferences_controller.rb b/app/controllers/settings/preferences_controller.rb index 851e62d1b7..e2cb131671 100644 --- a/app/controllers/settings/preferences_controller.rb +++ b/app/controllers/settings/preferences_controller.rb @@ -35,6 +35,7 @@ class Settings::PreferencesController < ApplicationController params.require(:user).permit( :setting_default_privacy, :setting_default_sensitive, + :setting_default_language, :setting_unfollow_modal, :setting_boost_modal, :setting_delete_modal, diff --git a/app/javascript/styles/mastodon/forms.scss b/app/javascript/styles/mastodon/forms.scss index de16784a87..f1ed2c90bd 100644 --- a/app/javascript/styles/mastodon/forms.scss +++ b/app/javascript/styles/mastodon/forms.scss @@ -140,6 +140,8 @@ code { } .input.with_block_label { + padding-top: 15px; + & > label { font-family: inherit; font-size: 16px; diff --git a/app/lib/user_settings_decorator.rb b/app/lib/user_settings_decorator.rb index a82f8974b7..8339593978 100644 --- a/app/lib/user_settings_decorator.rb +++ b/app/lib/user_settings_decorator.rb @@ -19,6 +19,7 @@ class UserSettingsDecorator user.settings['interactions'] = merged_interactions if change?('interactions') user.settings['default_privacy'] = default_privacy_preference if change?('setting_default_privacy') user.settings['default_sensitive'] = default_sensitive_preference if change?('setting_default_sensitive') + user.settings['default_language'] = default_language_preference if change?('setting_default_language') user.settings['unfollow_modal'] = unfollow_modal_preference if change?('setting_unfollow_modal') user.settings['boost_modal'] = boost_modal_preference if change?('setting_boost_modal') user.settings['delete_modal'] = delete_modal_preference if change?('setting_delete_modal') @@ -87,6 +88,10 @@ class UserSettingsDecorator settings['setting_theme'] end + def default_language_preference + settings['setting_default_language'] + end + def boolean_cast_setting(key) ActiveModel::Type::Boolean.new.cast(settings[key]) end diff --git a/app/models/user.rb b/app/models/user.rb index c95960dc6d..c820c553a7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -89,7 +89,7 @@ class User < ApplicationRecord delegate :auto_play_gif, :default_sensitive, :unfollow_modal, :boost_modal, :delete_modal, :reduce_motion, :system_font_ui, :noindex, :theme, :display_sensitive_media, :hide_network, - to: :settings, prefix: :setting, allow_nil: false + :default_language, to: :settings, prefix: :setting, allow_nil: false attr_accessor :invite_code diff --git a/app/serializers/rest/credential_account_serializer.rb b/app/serializers/rest/credential_account_serializer.rb index 56857cba88..fb195eb07d 100644 --- a/app/serializers/rest/credential_account_serializer.rb +++ b/app/serializers/rest/credential_account_serializer.rb @@ -9,6 +9,7 @@ class REST::CredentialAccountSerializer < REST::AccountSerializer { privacy: user.setting_default_privacy, sensitive: user.setting_default_sensitive, + language: user.setting_default_language, note: object.note, fields: object.fields.map(&:to_h), } diff --git a/app/services/post_status_service.rb b/app/services/post_status_service.rb index a5a21c3592..7359857257 100644 --- a/app/services/post_status_service.rb +++ b/app/services/post_status_service.rb @@ -30,7 +30,7 @@ class PostStatusService < BaseService sensitive: (options[:sensitive].nil? ? account.user&.setting_default_sensitive : options[:sensitive]) || options[:spoiler_text].present?, spoiler_text: options[:spoiler_text] || '', visibility: options[:visibility] || account.user&.setting_default_privacy, - language: language_from_option(options[:language]) || LanguageDetector.instance.detect(text, account), + language: language_from_option(options[:language]) || account.user&.setting_default_language&.presence || LanguageDetector.instance.detect(text, account), application: options[:application]) end diff --git a/app/views/settings/preferences/show.html.haml b/app/views/settings/preferences/show.html.haml index 6ec03ab4bd..43430069f6 100644 --- a/app/views/settings/preferences/show.html.haml +++ b/app/views/settings/preferences/show.html.haml @@ -12,6 +12,8 @@ .fields-group = f.input :locale, collection: I18n.available_locales, wrapper: :with_label, include_blank: false, label_method: lambda { |locale| human_locale(locale) }, selected: I18n.locale + = f.input :setting_default_language, collection: [nil] + filterable_languages.sort, wrapper: :with_label, label_method: lambda { |locale| locale.nil? ? I18n.t('statuses.language_detection') : human_locale(locale) }, required: false, include_blank: false + = f.input :chosen_languages, collection: filterable_languages.sort, wrapper: :with_block_label, include_blank: false, label_method: lambda { |locale| human_locale(locale) }, required: false, as: :check_boxes, collection_wrapper_tag: 'ul', item_wrapper_tag: 'li' %h4= t 'preferences.publishing' diff --git a/config/locales/en.yml b/config/locales/en.yml index 46875d2ecb..01e5dd2f8d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -675,6 +675,7 @@ en: disallowed_hashtags: one: 'contained a disallowed hashtag: %{tags}' other: 'contained the disallowed hashtags: %{tags}' + language_detection: Automatically detect language open_in_web: Open in web over_character_limit: character limit of %{max} exceeded pin_errors: diff --git a/config/locales/simple_form.en.yml b/config/locales/simple_form.en.yml index a75a0f567f..6783f00452 100644 --- a/config/locales/simple_form.en.yml +++ b/config/locales/simple_form.en.yml @@ -12,10 +12,12 @@ en: other: %{count} characters left fields: You can have up to 4 items displayed as a table on your profile header: PNG, GIF or JPG. At most 2MB. Will be downscaled to 700x335px + locale: The language of the user interface, e-mails and push notifications locked: Requires you to manually approve followers note: one: 1 character left other: %{count} characters left + setting_default_language: The language of your toots can be detected automatically, but it's not always accurate setting_hide_network: Who you follow and who follows you will not be shown on your profile setting_noindex: Affects your public profile and status pages setting_theme: Affects how Mastodon looks when you're logged in from any device. @@ -24,7 +26,7 @@ en: sessions: otp: 'Enter the two-factor code generated by your phone app or use one of your recovery codes:' user: - chosen_languages: Only toots in selected languages will be displayed in public timelines. Select nothing to see toots in all languages + chosen_languages: When checked, only toots in selected languages will be displayed in public timelines labels: account: fields: @@ -53,6 +55,7 @@ en: password: Password setting_auto_play_gif: Auto-play animated GIFs setting_boost_modal: Show confirmation dialog before boosting + setting_default_language: Posting language setting_default_privacy: Post privacy setting_default_sensitive: Always mark media as sensitive setting_delete_modal: Show confirmation dialog before deleting a toot