Translate a Document

Contents
[ ]

Document translation is a frequently needed option in the age of high digitalization. Aspose.Words supports document translation using Google generative language models, which allows developers to translate texts content into more than 300 languages.

Use the Translate method to translate your documents into any language represented in the Language enumeration. Note that if the source document contains several languages, the Google AI-based model will be able to translate all supported languages. If the model cannot recognize the language in some text fragments, then you will be returned a document with these untranslated fragments and with the rest of the text translated.

The following code example shows how to use the Gemini 1.5 Flash model in Aspose.Words to translate a document into Arabic:

doc = aw.Document(file_name=MY_DIR + "Document.docx")

api_key = system_helper.environment.Environment.get_environment_variable("API_KEY")
# Use Google generative language models.
model = aw.ai.AiModel.create(aw.ai.AiModelType.GEMINI_15_FLASH).with_api_key(api_key).as_google_ai_model()

translated_doc = model.translate(doc, aw.ai.Language.ARABIC)
translated_doc.save(file_name=ARTIFACTS_DIR + "AI.AiTranslate.docx")

FAQ

  1. Q: Which languages can be used with the Translate method?
    A: The method supports any language defined in the aw.ai.Language enumeration, which currently includes over 300 languages provided by Google generative models.

  2. Q: Do I need a Google API key to use document translation?
    A: Yes. The AI model must be created with a valid Google API key using with_api_key. Without the key the translation request will fail.

  3. Q: Can a single document contain multiple source languages and still be translated correctly?
    A: Yes. The Google AI model attempts to detect each language segment in the source document and translates each fragment to the target language. Segments it cannot recognise remain untranslated.

  4. Q: Is translation available for all file formats supported by Aspose.Words?
    A: Translation works on any document that can be loaded into an aw.Document object (e.g., DOCX, DOC, RTF, HTML, PDF). The output format is determined by the format you save the translated document in.

  5. Q: How can I change the AI model used for translation?
    A: Create the model with a different AiModelType (e.g., AiModelType.GEMINI_15_FLASH, AiModelType.GEMINI_1_0_PRO) before calling translate. The rest of the code remains the same.