Automated translation with project templates from API not working

Dear all, 

what I'm trying to solve is to create translation projects through the Studio API, and I use a project template as the basis for the new project.

The project template contains a TM and the SDL BeGlobal Automated Translation provider as translation sources. In the template, I set up "Apply automated translation" when no match is found. If I use this template to create a new project from Studio, the BeGlobal is used prefectly during the pretranslation. However, if I do the same through the API, BeGlobal is not applied and this is the problem. It is added to the project as a translation provider, but it is not used in the Pretranslation phase, just the TM. If I open the created project in studio, and click on "Batch task -> Pretranslate files -> Finish" on the target file, then BeGlobal is applied, and the target segments are filled. This is all the sample code I use:

var pi = new ProjectInfo();
pi.Name = "Test";
pi.LocalProjectFolder = @"d:\temp\sdlprojects\Test5";
var ptl = new ProjectTemplateReference(@"C:\Users\mesztam\Documents\Studio 2011\Project Templates\Test.sdltpl");
var fbp = new FileBasedProject(pi, ptl);
var pf = fbp.AddFiles(new string[] { @"d:\test2.txt" })[0];
fbp.SetFileRole(new Guid[] { pf.Id }, FileRole.Translatable);
fbp.Save();
fbp.RunAutomaticTasks(new[] {pf.Id}, new[] {
"Sdl.ProjectApi.AutomaticTasks.Scan", "Sdl.ProjectApi.AutomaticTasks.Conversion", "Sdl.ProjectApi.AutomaticTasks.Split"});
var tlf= fbp.GetTargetLanguageFiles()[0];
fbp.RunAutomaticTasks(new[] { tlf.Id }, new[] { "Sdl.ProjectApi.AutomaticTasks.Analysis", "Sdl.ProjectApi.AutomaticTasks.Translate" });
fbp.Save();

I would apprechiate any ideas. Or is this an API bug?

thanks in advance,

Tamas

Parents
  • Hi Tamas,

    I have scheduled some time for an SDL developer to look into this and we will be back in touch.

    Regards, Ian

    Paul Filkin | RWS Group

    ________________________
    Design your own training!

    You've done the courses and still need to go a little further, or still not clear? 
    Tell us what you need in our Community Solutions Hub

  • Hi Tamas, hi Ian,

    We have a similar problem (see "Automatic tasks and project template" posted on 10th of April).

    This is quite critical for us because we have to integrate with an exisiting workflow engine.

    KR,

    Alain

  • Chaps,

    Tamas's request is currently still in progress with the analysing SDL development lead.  Alain I'll look into your request and get back to you.  Any further infornation please send to sdk-feedback@sdl.com

    Paul Filkin | RWS Group

    ________________________
    Design your own training!

    You've done the courses and still need to go a little further, or still not clear? 
    Tell us what you need in our Community Solutions Hub

  • APS said:
    (see "Automatic tasks and project template" posted on 10th of April).

    Hi Alain,

    Where is this post? It would be helpful if you add the link as we can't find this at all.

    Regards

    Paul

    Paul Filkin | RWS Group

    ________________________
    Design your own training!

    You've done the courses and still need to go a little further, or still not clear? 
    Tell us what you need in our Community Solutions Hub

  • Hi Tamás,

    By default the project template does not store the task settings. Anyway you can configure the task settings using the following code:

               FileBasedProject project = ...

               var settingsBundle = project.GetSettings();

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

               settings.NoTranslationMemoryMatchFoundAction.Value = NoTranslationMemoryMatchFoundAction.ApplyAutomatedTranslation;

               project.UpdateSettings(settingsBundle);

               project.Save();

    Studio cached some info which are not available for the SDK. The important one for machine translation is the log on credential for beglobal. If you know your credential for the machine translation, You can set the credential using code:

               var uri = new Uri("beglobalcommunity:///");

               project.Credentials.AddCredential(uri, credential);

    Otherwise you might read from the credential file (In my installation this is at C:\Users\XXX\AppData\Roaming\SDL\SDL Trados Studio\11.0.0.0\TranslationProviderCredentialStore.bin). This is the credential in encrypted format. Here is the complete code I tested the machine translation from pre-translate (Besides the project automation api, Sdl.Core.Settings.dll, Sdl.LanguagePlatform.TranslationMemoryApi.dll should be added to the project reference too):

    class MachineTranslationTest

    {

       public void Test()

       {

           MachineTranslate();

       }

       public void MachineTranslate()

       {

           var projectTemplatePath = @"D:\Test\TestMachineTranslationWithProjectTemplate.sdltpl";

           var projectFilePath = @"D:\Test\simpleTest.docx";

           var workFolder = @"d:\tout\A";

           ClearFolder(workFolder);

           var projectInfo = new ProjectInfo()

           {

               Name = "TestMachineTranslationWithProjectTemplate",

               LocalProjectFolder = workFolder

           };

           var projectTemplate = new ProjectTemplateReference(projectTemplatePath);

           var project = new FileBasedProject(projectInfo, projectTemplate);

           SetCredentials(project);

           var projectFiles = project.AddFiles(new string[] { projectFilePath });

           var settingsBundle = project.GetSettings();

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

           settings.NoTranslationMemoryMatchFoundAction.Value = NoTranslationMemoryMatchFoundAction.ApplyAutomatedTranslation;

           project.UpdateSettings(settingsBundle);

           project.Save();

           project.RunAutomaticTasks(projectFiles.GetIds(), new string[]

                                                                   {

                                                                       "Sdl.ProjectApi.AutomaticTasks.Scan",

                                                                       "Sdl.ProjectApi.AutomaticTasks.Conversion",

                                                                       "Sdl.ProjectApi.AutomaticTasks.Split",

                                                                       "Sdl.ProjectApi.AutomaticTasks.Analysis",

                                                                       "Sdl.ProjectApi.AutomaticTasks.Translate"

                                                                   }, ProcessTaskStatusEvent, ProcessTaskMessageEvent

                                                                   );

           project.Save();

       }

       private void SetCredentials(FileBasedProject project)

       {

           string translationProviderCredentialStoreFilePath = @"C:\Users\xliu\AppData\Roaming\SDL\SDL Trados Studio\11.0.0.0\TranslationProviderCredentialStore.bin";

           using (SymmetricAlgorithm algorithm = Rijndael.Create())

           {

               string encryptionKeyFilePath = translationProviderCredentialStoreFilePath + "2";

               byte[] encryptionEntropy = new byte[] { 14, 81, 46, 254, 197, 222, 127, 151, 123, 91, 216, 20, 67, 137, 175, 241 };

               byte[] encryptionKey = ProtectedData.Unprotect(File.ReadAllBytes(encryptionKeyFilePath), encryptionEntropy, DataProtectionScope.CurrentUser);

               byte[] encryptionIV =  new byte[] { 16, 241, 204,  11, 208, 203, 126, 200, 153, 110,   6,  81,  32, 251, 207,  50};

               using (ICryptoTransform decryptor = algorithm.CreateDecryptor(encryptionKey, encryptionIV))

               {

                   using (FileStream fileStream = new FileStream(translationProviderCredentialStoreFilePath, FileMode.Open))

                   {

                       using (CryptoStream cryptoStream = new CryptoStream(fileStream, decryptor, CryptoStreamMode.Read))

                       {

                           XmlSerializer xmlSerializer = new XmlSerializer(typeof(TranslationProviderCredentialStore));

                           var store = (TranslationProviderCredentialStore)xmlSerializer.Deserialize(cryptoStream);

                           var uri = new Uri("beglobalcommunity:///");

                           Console.WriteLine(store.GetCredential(uri).Credential);

                           project.Credentials.AddCredential(uri, store.GetCredential(uri).Credential);

                       }

                   }

               }

           }

       }

       static void ProcessTaskStatusEvent(object sender, TaskStatusEventArgs e)

       {

           Console.WriteLine("Tasks: " + string.Join(", ", e.TaskTemplateIds) + " --" + e.Status);

           Console.WriteLine(e.PercentComplete);

           Console.WriteLine(e.StatusMessage);

       }

       static void ProcessTaskMessageEvent(object sender, TaskMessageEventArgs e)

       {

           Console.WriteLine(e.TaskTemplateIds);

       }

       // clear the folder

       static void ClearFolder(string foler)

       {

           if (Directory.Exists(foler))

           {

               try

               {

                   Directory.Delete(foler, true);

               }

               catch (Exception)

               {

                   Thread.Sleep(0);

                   Directory.Delete(foler, false);

               }

           }

       }

    }

    Regards,

    Xingzeng

Reply Children
  • Hi Alain,

    Your problem to access the initial tasks is not supported for the moment. The current API does not expose those info. For a workaround, you might just read the reference file or your project file as xml to get a list of the initial task templates. The following is the part in my project template file.

      <InitialTaskTemplate Description="ComplexTask_Prepare_NoProjectTM_Description" Name="ComplexTask_Prepare_NoProjectTM_Name" Id="Sdl.ProjectApi.AutomaticTask.Prepare.NoProjectTM">    
    <SubTaskTemplates>
          <SubTaskTemplate TaskTemplateId="Sdl.ProjectApi.AutomaticTasks.Conversion" />
          <SubTaskTemplate TaskTemplateId="Sdl.ProjectApi.AutomaticTasks.Split" />
          <SubTaskTemplate TaskTemplateId="Sdl.ProjectApi.AutomaticTasks.Analysis" />
          <SubTaskTemplate TaskTemplateId="Sdl.ProjectApi.AutomaticTasks.Translate" />
        </SubTaskTemplates>
      </InitialTaskTemplate>

    As long as you get the list of the task template Ids, you can call RunAutomaticTasks with the list of the tasks.

    Regards,

    Xingzeng