forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add user content translations with configurable backends (mastodon#19218
- Loading branch information
Showing
16 changed files
with
306 additions
and
11 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
app/controllers/api/v1/statuses/translations_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
class Api::V1::Statuses::TranslationsController < Api::BaseController | ||
include Authorization | ||
|
||
before_action -> { doorkeeper_authorize! :read, :'read:statuses' } | ||
before_action :set_status | ||
before_action :set_translation | ||
|
||
rescue_from TranslationService::NotConfiguredError, with: :not_found | ||
rescue_from TranslationService::UnexpectedResponseError, TranslationService::QuotaExceededError, TranslationService::TooManyRequestsError, with: :service_unavailable | ||
|
||
def create | ||
render json: @translation, serializer: REST::TranslationSerializer | ||
end | ||
|
||
private | ||
|
||
def set_status | ||
@status = Status.find(params[:status_id]) | ||
authorize @status, :show? | ||
rescue Mastodon::NotPermittedError | ||
not_found | ||
end | ||
|
||
def set_translation | ||
@translation = TranslateStatusService.new.call(@status, content_locale) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
class TranslationService | ||
class Error < StandardError; end | ||
class NotConfiguredError < Error; end | ||
class TooManyRequestsError < Error; end | ||
class QuotaExceededError < Error; end | ||
class UnexpectedResponseError < Error; end | ||
|
||
def self.configured | ||
if ENV['DEEPL_API_KEY'].present? | ||
TranslationService::DeepL.new(ENV.fetch('DEEPL_PLAN', 'free'), ENV['DEEPL_API_KEY']) | ||
elsif ENV['LIBRE_TRANSLATE_ENDPOINT'].present? | ||
TranslationService::LibreTranslate.new(ENV['LIBRE_TRANSLATE_ENDPOINT'], ENV['LIBRE_TRANSLATE_API_KEY']) | ||
else | ||
raise NotConfiguredError | ||
end | ||
end | ||
|
||
def translate(_text, _source_language, _target_language) | ||
raise NotImplementedError | ||
end | ||
end |
Oops, something went wrong.