Skip to content

Commit

Permalink
Move translation service config to config_for yml (mastodon#30663)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjankowski authored Oct 23, 2024
1 parent 5a4f4f3 commit 35f008a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
20 changes: 15 additions & 5 deletions app/lib/translation_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,27 @@ 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'])
if configuration.deepl[:api_key].present?
TranslationService::DeepL.new(
configuration.deepl[:plan],
configuration.deepl[:api_key]
)
elsif configuration.libre_translate[:endpoint].present?
TranslationService::LibreTranslate.new(
configuration.libre_translate[:endpoint],
configuration.libre_translate[:api_key]
)
else
raise NotConfiguredError
end
end

def self.configured?
ENV['DEEPL_API_KEY'].present? || ENV['LIBRE_TRANSLATE_ENDPOINT'].present?
configuration.deepl[:api_key].present? || configuration.libre_translate[:endpoint].present?
end

def self.configuration
Rails.configuration.x.translation
end

def languages
Expand Down
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class Application < Rails::Application
end
end

config.x.translation = config_for(:translation)

config.to_prepare do
Doorkeeper::AuthorizationsController.layout 'modal'
Doorkeeper::AuthorizedApplicationsController.layout 'admin'
Expand Down
7 changes: 7 additions & 0 deletions config/translation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
shared:
deepl:
api_key: <%= ENV.fetch('DEEPL_API_KEY', nil) %>
plan: <%= ENV.fetch('DEEPL_PLAN', 'free') %>
libre_translate:
api_key: <%= ENV.fetch('LIBRE_TRANSLATE_API_KEY', nil) %>
endpoint: <%= ENV.fetch('LIBRE_TRANSLATE_ENDPOINT', nil) %>

0 comments on commit 35f008a

Please sign in to comment.