Translate to regional language variant (e.g. pt-BR) (#32428)
This commit is contained in:
parent
dc2f9eef77
commit
0ff427fab3
3 changed files with 21 additions and 4 deletions
|
@ -23,6 +23,6 @@ class Api::V1::Statuses::TranslationsController < Api::V1::Statuses::BaseControl
|
||||||
private
|
private
|
||||||
|
|
||||||
def set_translation
|
def set_translation
|
||||||
@translation = TranslateStatusService.new.call(@status, content_locale)
|
@translation = TranslateStatusService.new.call(@status, I18n.locale.to_s)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,6 +9,8 @@ class TranslateStatusService < BaseService
|
||||||
def call(status, target_language)
|
def call(status, target_language)
|
||||||
@status = status
|
@status = status
|
||||||
@source_texts = source_texts
|
@source_texts = source_texts
|
||||||
|
|
||||||
|
target_language = target_language.split(/[_-]/).first unless target_languages.include?(target_language)
|
||||||
@target_language = target_language
|
@target_language = target_language
|
||||||
|
|
||||||
raise Mastodon::NotPermittedError unless permitted?
|
raise Mastodon::NotPermittedError unless permitted?
|
||||||
|
@ -32,11 +34,15 @@ class TranslateStatusService < BaseService
|
||||||
def permitted?
|
def permitted?
|
||||||
return false unless @status.distributable? && TranslationService.configured?
|
return false unless @status.distributable? && TranslationService.configured?
|
||||||
|
|
||||||
languages[@status.language]&.include?(@target_language)
|
target_languages.include?(@target_language)
|
||||||
end
|
end
|
||||||
|
|
||||||
def languages
|
def languages
|
||||||
Rails.cache.fetch('translation_service/languages', expires_in: 7.days, race_condition_ttl: 1.hour) { TranslationService.configured.languages }
|
Rails.cache.fetch('translation_service/languages', expires_in: 7.days, race_condition_ttl: 1.hour) { translation_backend.languages }
|
||||||
|
end
|
||||||
|
|
||||||
|
def target_languages
|
||||||
|
languages[@status.language] || []
|
||||||
end
|
end
|
||||||
|
|
||||||
def content_hash
|
def content_hash
|
||||||
|
|
|
@ -18,7 +18,7 @@ RSpec.describe TranslateStatusService do
|
||||||
describe '#call' do
|
describe '#call' do
|
||||||
before do
|
before do
|
||||||
translation_service = TranslationService.new
|
translation_service = TranslationService.new
|
||||||
allow(translation_service).to receive(:languages).and_return({ 'en' => ['es'] })
|
allow(translation_service).to receive(:languages).and_return({ 'en' => ['es', 'es-MX'] })
|
||||||
allow(translation_service).to receive(:translate) do |texts|
|
allow(translation_service).to receive(:translate) do |texts|
|
||||||
texts.map do |text|
|
texts.map do |text|
|
||||||
TranslationService::Translation.new(
|
TranslationService::Translation.new(
|
||||||
|
@ -37,6 +37,7 @@ RSpec.describe TranslateStatusService do
|
||||||
.to have_attributes(
|
.to have_attributes(
|
||||||
content: '<p>Hola</p>',
|
content: '<p>Hola</p>',
|
||||||
detected_source_language: 'en',
|
detected_source_language: 'en',
|
||||||
|
language: 'es',
|
||||||
provider: 'Dummy',
|
provider: 'Dummy',
|
||||||
status: status
|
status: status
|
||||||
)
|
)
|
||||||
|
@ -101,6 +102,16 @@ RSpec.describe TranslateStatusService do
|
||||||
expect(media_attachment.description).to eq 'Hola & :highfive:'
|
expect(media_attachment.description).to eq 'Hola & :highfive:'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'target language is regional' do
|
||||||
|
it 'uses regional variant' do
|
||||||
|
expect(service.call(status, 'es-MX').language).to eq 'es-MX'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'uses parent locale for unsupported regional variant' do
|
||||||
|
expect(service.call(status, 'es-XX').language).to eq 'es'
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#source_texts' do
|
describe '#source_texts' do
|
||||||
|
|
Loading…
Reference in a new issue