From 35f008aa2a16240dc20d543ecc1089aa19332a00 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Wed, 23 Oct 2024 09:32:18 -0400 Subject: [PATCH] Move translation service config to `config_for` yml (#30663) --- app/lib/translation_service.rb | 20 +++++++++++++++----- config/application.rb | 2 ++ config/translation.yml | 7 +++++++ 3 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 config/translation.yml diff --git a/app/lib/translation_service.rb b/app/lib/translation_service.rb index bfe5de44f8..ee268d7a7c 100644 --- a/app/lib/translation_service.rb +++ b/app/lib/translation_service.rb @@ -8,17 +8,27 @@ class TranslationService 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 diff --git a/config/application.rb b/config/application.rb index 5e2f44453d..0641113982 100644 --- a/config/application.rb +++ b/config/application.rb @@ -109,6 +109,8 @@ module Mastodon end end + config.x.translation = config_for(:translation) + config.to_prepare do Doorkeeper::AuthorizationsController.layout 'modal' Doorkeeper::AuthorizedApplicationsController.layout 'admin' diff --git a/config/translation.yml b/config/translation.yml new file mode 100644 index 0000000000..e074c5d0f2 --- /dev/null +++ b/config/translation.yml @@ -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) %>