Replace Status#translatable?
with language matrix in separate endpoint (#24037)
This commit is contained in:
committed by
GitHub
parent
630436ab2d
commit
bd047acc35
@ -17,25 +17,31 @@ class TranslationService::DeepL < TranslationService
|
||||
end
|
||||
end
|
||||
|
||||
def supported?(source_language, target_language)
|
||||
source_language.in?(languages('source')) && target_language.in?(languages('target'))
|
||||
def languages
|
||||
source_languages = [nil] + fetch_languages('source')
|
||||
|
||||
# In DeepL, EN and PT are deprecated in favor of EN-GB/EN-US and PT-BR/PT-PT, so
|
||||
# they are supported but not returned by the API.
|
||||
target_languages = %w(en pt) + fetch_languages('target')
|
||||
|
||||
source_languages.index_with { |language| target_languages.without(nil, language) }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def languages(type)
|
||||
Rails.cache.fetch("translation_service/deepl/languages/#{type}", expires_in: 7.days, race_condition_ttl: 1.minute) do
|
||||
request(:get, "/v2/languages?type=#{type}") do |res|
|
||||
# In DeepL, EN and PT are deprecated in favor of EN-GB/EN-US and PT-BR/PT-PT, so
|
||||
# they are supported but not returned by the API.
|
||||
extra = type == 'source' ? [nil] : %w(en pt)
|
||||
languages = Oj.load(res.body_with_limit).map { |language| language['language'].downcase }
|
||||
|
||||
languages + extra
|
||||
end
|
||||
def fetch_languages(type)
|
||||
request(:get, "/v2/languages?type=#{type}") do |res|
|
||||
Oj.load(res.body_with_limit).map { |language| normalize_language(language['language']) }
|
||||
end
|
||||
end
|
||||
|
||||
def normalize_language(language)
|
||||
subtags = language.split(/[_-]/)
|
||||
subtags[0].downcase!
|
||||
subtags[1]&.upcase!
|
||||
subtags.join('-')
|
||||
end
|
||||
|
||||
def request(verb, path, **options)
|
||||
req = Request.new(verb, "#{base_url}#{path}", **options)
|
||||
req.add_headers(Authorization: "DeepL-Auth-Key #{@api_key}")
|
||||
|
Reference in New Issue
Block a user