From 119b5adbe05b63d9362be7b2efa44be910755e74 Mon Sep 17 00:00:00 2001 From: Claire Date: Mon, 24 Oct 2022 18:30:58 +0200 Subject: [PATCH] =?UTF-8?q?Change=20=E2=80=9CTranslate=E2=80=9D=20button?= =?UTF-8?q?=20to=20only=20show=20up=20when=20a=20translation=20backend=20i?= =?UTF-8?q?s=20configured=20(#19434)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Change “Translate” button to only show up when a translation backend is configured Fixes #19346 * Add `translation` attribute to /api/v2/instance to expose whether the translation feature is enabled Fixes #19328 --- app/javascript/mastodon/components/status_content.js | 4 ++-- app/javascript/mastodon/initial_state.js | 2 ++ app/lib/translation_service.rb | 4 ++++ app/serializers/initial_state_serializer.rb | 1 + app/serializers/rest/instance_serializer.rb | 4 ++++ 5 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/javascript/mastodon/components/status_content.js b/app/javascript/mastodon/components/status_content.js index 6ad3e018a7..0ed79e6588 100644 --- a/app/javascript/mastodon/components/status_content.js +++ b/app/javascript/mastodon/components/status_content.js @@ -6,7 +6,7 @@ import Permalink from './permalink'; import classnames from 'classnames'; import PollContainer from 'mastodon/containers/poll_container'; import Icon from 'mastodon/components/icon'; -import { autoPlayGif, languages as preloadedLanguages } from 'mastodon/initial_state'; +import { autoPlayGif, languages as preloadedLanguages, translationEnabled } from 'mastodon/initial_state'; const MAX_HEIGHT = 642; // 20px * 32 (+ 2px padding at the top) @@ -181,7 +181,7 @@ class StatusContent extends React.PureComponent { const hidden = this.props.onExpandedToggle ? !this.props.expanded : this.state.hidden; const renderReadMore = this.props.onClick && status.get('collapsed'); const renderViewThread = this.props.showThread && status.get('in_reply_to_id') && status.get('in_reply_to_account_id') === status.getIn(['account', 'id']); - const renderTranslate = this.context.identity.signedIn && this.props.onTranslate && ['public', 'unlisted'].includes(status.get('visibility')) && status.get('contentHtml').length > 0 && status.get('language') !== null && intl.locale !== status.get('language'); + const renderTranslate = translationEnabled && this.context.identity.signedIn && this.props.onTranslate && ['public', 'unlisted'].includes(status.get('visibility')) && status.get('contentHtml').length > 0 && status.get('language') !== null && intl.locale !== status.get('language'); const language = preloadedLanguages.find(lang => lang[0] === status.get('language')); const languageName = language ? language[2] : status.get('language'); diff --git a/app/javascript/mastodon/initial_state.js b/app/javascript/mastodon/initial_state.js index f9843f7f8b..bb05dafdf7 100644 --- a/app/javascript/mastodon/initial_state.js +++ b/app/javascript/mastodon/initial_state.js @@ -77,6 +77,7 @@ * @property {boolean} use_blurhash * @property {boolean=} use_pending_items * @property {string} version + * @property {boolean} translation_enabled */ /** @@ -125,6 +126,7 @@ export const unfollowModal = getMeta('unfollow_modal'); export const useBlurhash = getMeta('use_blurhash'); export const usePendingItems = getMeta('use_pending_items'); export const version = getMeta('version'); +export const translationEnabled = getMeta('translation_enabled'); export const languages = initialState?.languages; export default initialState; diff --git a/app/lib/translation_service.rb b/app/lib/translation_service.rb index 526e26ae59..285f309393 100644 --- a/app/lib/translation_service.rb +++ b/app/lib/translation_service.rb @@ -17,6 +17,10 @@ class TranslationService end end + def self.configured? + ENV['DEEPL_API_KEY'].present? || ENV['LIBRE_TRANSLATE_ENDPOINT'].present? + end + def translate(_text, _source_language, _target_language) raise NotImplementedError end diff --git a/app/serializers/initial_state_serializer.rb b/app/serializers/initial_state_serializer.rb index ba446854c9..02e45a92e3 100644 --- a/app/serializers/initial_state_serializer.rb +++ b/app/serializers/initial_state_serializer.rb @@ -31,6 +31,7 @@ class InitialStateSerializer < ActiveModel::Serializer timeline_preview: Setting.timeline_preview, activity_api_enabled: Setting.activity_api_enabled, single_user_mode: Rails.configuration.x.single_user_mode, + translation_enabled: TranslationService.configured?, } if object.current_account diff --git a/app/serializers/rest/instance_serializer.rb b/app/serializers/rest/instance_serializer.rb index 7d00b20ba0..606e7d8311 100644 --- a/app/serializers/rest/instance_serializer.rb +++ b/app/serializers/rest/instance_serializer.rb @@ -72,6 +72,10 @@ class REST::InstanceSerializer < ActiveModel::Serializer min_expiration: PollValidator::MIN_EXPIRATION, max_expiration: PollValidator::MAX_EXPIRATION, }, + + translation: { + enabled: TranslationService.configured?, + }, } end