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

Fix synchronous translation code sample error. #148

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ def sample_single_document_translation():

# absolute path to your document
file_path = "C:/{your-file-path}/document-translation-sample.docx"
file_name = os.path.path.basename(file_path)
file_name = os.path.basename(file_path)
file_type = (
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)
print(f"File for translation: {file_name}")

with open(file_name, "r") as file:
with open(file_path, "rb") as file:
file_contents = file.read()

document_content = (file_name, file_contents, file_type)
Expand All @@ -168,8 +168,12 @@ def sample_single_document_translation():
response_stream = client.document_translate(
body=document_translate_content, target_language=target_language
)
translated_response = response_stream.decode("utf-8-sig") # type: ignore[attr-defined]
print(f"Translated response: {translated_response}")
# Save the response_stream to a file
output_file_path = "./translated-document.docx"
with open(output_file_path, "wb") as output_file:
output_file.write(response_stream)

print(f"Translated document saved to: {output_file_path}")


if __name__ == "__main__":
Expand Down