From 0ff427fab3f5eb53dea3f4979b16994417c52fe4 Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Tue, 15 Oct 2024 14:26:20 +0100 Subject: [PATCH] Translate to regional language variant (e.g. pt-BR) (#32428) --- .../api/v1/statuses/translations_controller.rb | 2 +- app/services/translate_status_service.rb | 10 ++++++++-- spec/services/translate_status_service_spec.rb | 13 ++++++++++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v1/statuses/translations_controller.rb b/app/controllers/api/v1/statuses/translations_controller.rb index 8cf495f78..bd5cd9bb0 100644 --- a/app/controllers/api/v1/statuses/translations_controller.rb +++ b/app/controllers/api/v1/statuses/translations_controller.rb @@ -23,6 +23,6 @@ class Api::V1::Statuses::TranslationsController < Api::V1::Statuses::BaseControl private def set_translation - @translation = TranslateStatusService.new.call(@status, content_locale) + @translation = TranslateStatusService.new.call(@status, I18n.locale.to_s) end end diff --git a/app/services/translate_status_service.rb b/app/services/translate_status_service.rb index e2e076e21..bcd4703be 100644 --- a/app/services/translate_status_service.rb +++ b/app/services/translate_status_service.rb @@ -9,6 +9,8 @@ class TranslateStatusService < BaseService def call(status, target_language) @status = status @source_texts = source_texts + + target_language = target_language.split(/[_-]/).first unless target_languages.include?(target_language) @target_language = target_language raise Mastodon::NotPermittedError unless permitted? @@ -32,11 +34,15 @@ class TranslateStatusService < BaseService def permitted? return false unless @status.distributable? && TranslationService.configured? - languages[@status.language]&.include?(@target_language) + target_languages.include?(@target_language) end 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 def content_hash diff --git a/spec/services/translate_status_service_spec.rb b/spec/services/translate_status_service_spec.rb index cd92fb8d1..ac7a43ff2 100644 --- a/spec/services/translate_status_service_spec.rb +++ b/spec/services/translate_status_service_spec.rb @@ -18,7 +18,7 @@ RSpec.describe TranslateStatusService do describe '#call' do before do 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| texts.map do |text| TranslationService::Translation.new( @@ -37,6 +37,7 @@ RSpec.describe TranslateStatusService do .to have_attributes( content: '

Hola

', detected_source_language: 'en', + language: 'es', provider: 'Dummy', status: status ) @@ -101,6 +102,16 @@ RSpec.describe TranslateStatusService do expect(media_attachment.description).to eq 'Hola & :highfive:' 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 describe '#source_texts' do