Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve document encoding in TranslateDocument #2900

Merged
merged 6 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gems/aws-sdk-translate/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased Changes
------------------

* Issue - Preserve Document encoding in `TranslateDocument` for text documents (#2897).

1.57.0 (2023-07-18)
------------------

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module Aws::Translate
module Plugins
# @api private
# The translated_documented returned by the translate_document api is modeled
# as a blob, since it may contain binary data (eg: word doc). However,
# when the input document is text (eg: text/plain or text/html) the encoding
# should be preserved.
class TranslateDocumentEncoding < Seahorse::Client::Plugin
class Handler < Seahorse::Client::Handler
def call(context)
# detect encoding
document = context.params[:document]
encoding =
if document[:content_type].start_with?('text/') && document[:content].is_a?(String)
document[:content].encoding
end
resp = @handler.call(context)
if encoding
resp.translated_document.content = resp.translated_document.content.force_encoding(encoding)
end
resp
end
end
def add_handlers(handlers, _config)
handlers.add(Handler, step: :initialize, operations: [:translate_document])
end
end
end
end
60 changes: 60 additions & 0 deletions gems/aws-sdk-translate/spec/translate_document_encoding_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

require_relative 'spec_helper'

module Aws::Translate
module Plugins
describe TranslateDocumentEncoding do
let(:client) { Aws::Translate::Client.new(stub_responses: true) }

let(:text_document) do
{ content_type: 'text/plain', content: 'plain text document' }
end

let(:translated_text_document) do
Base64.encode64('translated text document')
end

let(:word_document) do
{
content_type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
content: "ZmFrZSB3b3JkIGRvYw==\n"
}
end

let(:translated_word_document) do
"dHJhbnNsYXRlZCBmYWtlIHdvcmQgZG9j\n"
end

describe '#translate_document' do
it 'preserves the encoding on text documents' do
client.stub_responses(:translate_document, {
translated_document: { content: translated_text_document },
source_language_code: 'en',
target_language_code: 'es'
})
resp = client.translate_document(
document: text_document,
source_language_code: 'en',
target_language_code: 'es'
)
expect(resp.translated_document.content.encoding).to eq(text_document[:content].encoding)
end

it 'does not change the encoding on non-text documents' do
client.stub_responses(:translate_document, {
translated_document: { content: translated_word_document },
source_language_code: 'en',
target_language_code: 'es'
})
resp = client.translate_document(
document: word_document,
source_language_code: 'en',
target_language_code: 'es'
)
expect(resp.translated_document.content.encoding).to eq(Encoding.find('binary'))
end
end
end
end
end
5 changes: 4 additions & 1 deletion services.json
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,10 @@
"models": "transfer/2018-11-05"
},
"Translate": {
"models": "translate/2017-07-01"
"models": "translate/2017-07-01",
"addPlugins": [
"Aws::Translate::Plugins::TranslateDocumentEncoding"
]
},
"VPCLattice": {
"models": "vpc-lattice/2022-11-30"
Expand Down