API Pre-Translate Task not working for DeepL MT Provider

I have a FileBasedProject with :

  • Language pair: "de-DE" to "en-GB".
  • One file "fruits.xml".
  • One DeepL MT Translation Provider.

Trados Studio Project Settings showing DeepL Translator provider enabled for German to English language pair.

Attempting to run the following code via the API completes without errors but does not pre-translate the file (no translations in the target segment):

    FileBasedProject ThisSdlProject = new FileBasedProject(SdlProjectFilePath);
    
    var settings = ThisSdlProject.GetSettings();
    var preTranslateSettings = settings.GetSettingsGroup();
    preTranslateSettings.NoTranslationMemoryMatchFoundAction.Value = NoTranslationMemoryMatchFoundAction.ApplyAutomatedTranslation;
    preTranslateSettings.TranslationOverwriteMode.Value = TranslationUpdateMode.KeepExistingTranslation;
    preTranslateSettings.MinimumMatchScore.Value = 75;
    ThisSdlProject.UpdateSettings(settings);
    
    ProjectFile[] translatableFiles = ThisSdlProject.GetTargetLanguageFiles(new Core.Globalization.Language(CultureInfo.GetCultureInfo(TargetLocale)));
    
    AutomaticTask preTranslateTask = ThisSdlProject.RunAutomaticTask(
        translatableFiles.GetIds(),
	    AutomaticTaskTemplateIds.PreTranslateFiles
    );
    
    ThisSdlProject.Save();

Pre-translate settings in Trados Studio with minimum match value set to 75 and automated translation applied when no match is found.

However, if I select the file in Studio 2021, Batch Tasks -> Pre-translate -> Finish (without changing any settings), it works:

Trados Studio file list with 'fruits.xml' showing segments translated using NMT (Neural Machine Translation) with DeepL.

I swapped the DeepL MT Translation Provider for a File Based Translation Provider (Test_de-DE_en-GB.sdltm) and tried it again, and it worked as expected:

Trados Studio file list with 'fruits.xml' showing segments translated using a File Based Translation Memory with confirmed matches (CM).

Are there additional settings that must be applied in order for MT Translation Provider pre-translation to be applied via the API?

Thanks in advance,

Samuel



Generated Image Alt-Text
[edited by: Trados AI at 1:27 PM (GMT 0) on 5 Mar 2024]
Parents
  • Hi Samuel,

    I just tested the DeepL translation provider and I was able to translate using the API. I had DeepL provider added to a project and I've run the Pre-Translate batch task for that project.

    Code Sample:

    var selectedProject= SdlTradosStudio.Application?.GetController<ProjectsController>()?.SelectedProjects?.FirstOrDefault();
    if (selectedProject != null)
    {
    var settings = selectedProject.GetSettings();
    var preTranslateSettings = settings.GetSettingsGroup<TranslateTaskSettings>();
    preTranslateSettings.NoTranslationMemoryMatchFoundAction.Value = NoTranslationMemoryMatchFoundAction.ApplyAutomatedTranslation;
    preTranslateSettings.MinimumMatchScore.Value = 75;
    selectedProject.UpdateSettings(settings);
    selectedProject.Save();
    var targetFiles = selectedProject.GetTargetLanguageFiles();

    selectedProject.RunAutomaticTasks(targetFiles.GetIds(), new[]
    {
    AutomaticTaskTemplateIds.PreTranslateFiles,
    });
    selectedProject.Save();
    }

    Can you please try to use the settings above to see if you are able to pre-translate?

    Kind regards,

    Andrea Ghisa

  • Hi Andrea,

    Thanks for your reply.

    I used your sample code but unfortunately now get "Exception has been thrown by the target of an invocation.

    namespace Sdl.Sdk.PreTranslateFile
    {
        using Sdl.ProjectAutomation.Core;
        using Sdl.ProjectAutomation.Settings;
        using Sdl.TranslationStudioAutomation.IntegrationApi;
        using System.Linq;
    
        public class PreTranslateFile
        {
            public void Create()
            {
                var selectedProject = SdlTradosStudio.Application?.GetController()?.SelectedProjects?.FirstOrDefault();
                if (selectedProject != null)
                {
                    var settings = selectedProject.GetSettings();
                    var preTranslateSettings = settings.GetSettingsGroup();
                    preTranslateSettings.NoTranslationMemoryMatchFoundAction.Value = NoTranslationMemoryMatchFoundAction.ApplyAutomatedTranslation;
                    preTranslateSettings.MinimumMatchScore.Value = 75;
                    selectedProject.UpdateSettings(settings);
                    selectedProject.Save();
                    var targetFiles = selectedProject.GetTargetLanguageFiles();
    
                    selectedProject.RunAutomaticTasks(targetFiles.GetIds(), new[]
                    {
                        AutomaticTaskTemplateIds.PreTranslateFiles,
                    }
                    );
                    selectedProject.Save();
                }
            }
        }
    }
    
  • Hi Samuel,

    Can you please give me the stacktrace and more info from Visual Studio for the error you are receiving? Also, are you using this source code in the plugin or in a standalone application outside Studio?

    Have a nice day,

    Andrea

  • Hi Andrea,

    I am using this outside of Studio, in a Windows Command Line.

    I don't have a stacktrace, and no extra data is added to the sdlproj file using your code.

    When I use my code, the sdlproj file is updated with the correct Automatic.Task "Translate" data, just it doesn't translate the file.

    What Visual Studio info do you need?

    Thanks

    Samuel   

  • Apologies, stacktrace shows app crashes here:

    Console.WriteLine("Define selectedProject");
    var selectedProject = SdlTradosStudio.Application?.GetController<ProjectsController>()?.SelectedProjects?.FirstOrDefault();
    Console.WriteLine("Detect selectedProject");
    Define selectedProject
    Exception has been thrown by the target of an invocation.

    Command line error message in Trados Studio showing 'Define selectedProject' followed by 'Exception has been thrown by the target of an invocation.'


    Generated Image Alt-Text
    [edited by: Trados AI at 1:28 PM (GMT 0) on 5 Mar 2024]
  • The way in which I've taken the selected project in my code sample is used in a plugin that is running in Studio, I assumed you are using a plugin and not a standalone application. 

    Can you please use the FileBasedProject ThisSdlProject = new FileBasedProject(SdlProjectFilePath); which you used initially and then use the settings part which I've posted above?

    Andrea

  • Hi Andrea,

    Thanks again for your reply.

    Already tried that, runs all the way through but does not translate the file:

    namespace Sdl.Sdk.PreTranslateFile
    {
        using Sdl.ProjectAutomation.Core;
        using Sdl.ProjectAutomation.FileBased;
        using Sdl.ProjectAutomation.Settings;
        using System;
    
        public class PreTranslateFile
        {
            public void Create(
                string SdlProjectFilePath
            )
            {
                Console.WriteLine("Define selectedProject");
                FileBasedProject selectedProject = new FileBasedProject(SdlProjectFilePath);
                Console.WriteLine("Detect selectedProject");
                if (selectedProject != null)
                {
                    Console.WriteLine("Define settings");
                    var settings = selectedProject.GetSettings();
                    Console.WriteLine("Define preTranslateSettings");
                    var preTranslateSettings = settings.GetSettingsGroup();
                    preTranslateSettings.NoTranslationMemoryMatchFoundAction.Value = NoTranslationMemoryMatchFoundAction.ApplyAutomatedTranslation;
                    preTranslateSettings.MinimumMatchScore.Value = 75;
                    Console.WriteLine("UpdateSettings");
                    selectedProject.UpdateSettings(settings);
                    Console.WriteLine("Save");
                    selectedProject.Save();
                    Console.WriteLine("Define targetFiles");
                    var targetFiles = selectedProject.GetTargetLanguageFiles();
                    Console.WriteLine("RunAutomaticTasks");
                    selectedProject.RunAutomaticTasks(targetFiles.GetIds(), new[]
                    {
                        AutomaticTaskTemplateIds.PreTranslateFiles,
                    }
                    );
                    Console.WriteLine("Save");
                    selectedProject.Save();
                }
            }
        }
    }

    Console:

    • Define selectedProject
    • Detect selectedProject
    • Define settings
    • Define preTranslateSettings
    • UpdateSettings
    • Save
    • Define targetFiles
    • RunAutomaticTasks
    • 2021-06-28 12:48:54,762 [1] INFO ContentProcessingTaskImplementation [(null)] - Batch task max thread count = 3
    • Save
  • Hi Samuel,

    I've managed to reproduce the problem, the credentials are not sent to DeepL service and this is why the pre-translation is not working. If you open Fiddler you'll see that once the pre-translate is hit from DeepL service you'll receive 403.

    While I've investigated this problem I've discovered a small bug in DeepL translation provider in the way we are storing the options. They should be saved in the state of the provider and not in the provider URI, this is why Studio is not able to take the credentials from the Credentials Store.

    We are working to fix the problem in the DeepL plugin however it will take some time to test the new version and publish it on the AppStore.

    In the meantime the workaround for that it will be to add the DeepL Translation provider to the project programmatically (please make sure the project doesn't already have the DeepL added in Settings):

    var studioProject =
    new FileBasedProject(
    @"path to yout projectj");
    var settings = studioProject.GetSettings();

    var preTranslateSettings = settings.GetSettingsGroup<TranslateTaskSettings>();

    preTranslateSettings.NoTranslationMemoryMatchFoundAction.Value = NoTranslationMemoryMatchFoundAction.ApplyAutomatedTranslation;
    preTranslateSettings.MinimumMatchScore.Value = 75;
    studioProject.UpdateSettings(settings);
    studioProject.Save();

    var tpConfig = studioProject.GetTranslationProviderConfiguration();

    var tpUriString = "deepltranslationprovider:///";
    var tpReference = new TranslationProviderReference(new Uri(tpUriString), null, true);
    var tpCascadeEntry = new TranslationProviderCascadeEntry(tpReference, true, true, false);
    tpConfig.Entries.Add(tpCascadeEntry);
    studioProject.UpdateTranslationProviderConfiguration(tpConfig);

    var apiKey = "DeepL Api Key";
    studioProject.Credentials.AddCredential(new Uri(tpUriString), apiKey);
    var targetFiles = studioProject.GetTargetLanguageFiles();
    studioProject.Save(); var test = studioProject.RunAutomaticTasks(targetFiles.GetIds(), new[]
    {
    AutomaticTaskTemplateIds.PreTranslateFiles,
    });
    studioProject.Save();

Reply
  • Hi Samuel,

    I've managed to reproduce the problem, the credentials are not sent to DeepL service and this is why the pre-translation is not working. If you open Fiddler you'll see that once the pre-translate is hit from DeepL service you'll receive 403.

    While I've investigated this problem I've discovered a small bug in DeepL translation provider in the way we are storing the options. They should be saved in the state of the provider and not in the provider URI, this is why Studio is not able to take the credentials from the Credentials Store.

    We are working to fix the problem in the DeepL plugin however it will take some time to test the new version and publish it on the AppStore.

    In the meantime the workaround for that it will be to add the DeepL Translation provider to the project programmatically (please make sure the project doesn't already have the DeepL added in Settings):

    var studioProject =
    new FileBasedProject(
    @"path to yout projectj");
    var settings = studioProject.GetSettings();

    var preTranslateSettings = settings.GetSettingsGroup<TranslateTaskSettings>();

    preTranslateSettings.NoTranslationMemoryMatchFoundAction.Value = NoTranslationMemoryMatchFoundAction.ApplyAutomatedTranslation;
    preTranslateSettings.MinimumMatchScore.Value = 75;
    studioProject.UpdateSettings(settings);
    studioProject.Save();

    var tpConfig = studioProject.GetTranslationProviderConfiguration();

    var tpUriString = "deepltranslationprovider:///";
    var tpReference = new TranslationProviderReference(new Uri(tpUriString), null, true);
    var tpCascadeEntry = new TranslationProviderCascadeEntry(tpReference, true, true, false);
    tpConfig.Entries.Add(tpCascadeEntry);
    studioProject.UpdateTranslationProviderConfiguration(tpConfig);

    var apiKey = "DeepL Api Key";
    studioProject.Credentials.AddCredential(new Uri(tpUriString), apiKey);
    var targetFiles = studioProject.GetTargetLanguageFiles();
    studioProject.Save(); var test = studioProject.RunAutomaticTasks(targetFiles.GetIds(), new[]
    {
    AutomaticTaskTemplateIds.PreTranslateFiles,
    });
    studioProject.Save();

Children