Sdl Sdk Project Automation Use Template and PreTranslateFiles using Server Based Translation Memories

How is it possible to run AutomaticTask.PreTranslateFiles, using server based Translation Memories?

MainTranslationProviderItem Uri="sdltm.gsdemo.gs.sdlproducts.com/ Global Solution Consultants%2FBC Translation Productivity%2FSDL EMEA LSP%2FSDL LSP Trials%2F{CompanyName}&tmName={TmName}" Enabled="true
private void RunAutomaticTask(FileBasedProject ThisSdlProject, string TargetLanguageCode){
    ProjectFile[] targetFiles = ThisSdlProject.GetTargetLanguageFiles(new Language(CultureInfo.GetCultureInfo(TargetLanguageCode)));
    AutomaticTask preTranslate = ThisSdlProject.RunAutomaticTask(targetFiles.GetIds(), AutomaticTaskTemplateIds.PreTranslateFiles);
}

I get the error: "The current parameters are not compatible with the Translation Provider Uri Schema"

Parents
  • Hi ,

    Thank you for reporting this issue.

    I have been able to diagnose two potential issues with the project automation API and will report them to the dedicated team so that they can address them appropriately.

    In the mean time I have a possible workaround that should be sufficient in getting you past this exception thrown by the plugin framework.

    The issue you reported is basically associated with how the credentials are recovered from the Studio instance.  There have been some changes to how we manage the credential identity since the initial release of the ‘ProjectCreator’ code example, to demonstrate how to automate the creation of an SDL project, which includes, adding both server and file based translation resources.

    The solution is to provide Studio with some additional information to ensure the credentials provided are associated correctly in the language platform, adding them to temporary identity cache that is required by the content processor in communicating with the server based resource in the background.
    Simply add the prefix ‘ps.’ to the server address; ‘ps’ denotes the URI as a project server.  So, if the URI saved in your project template resembles (sdltm.http://gs2017dev.sdl.com), then use (ps.http://gs2017dev.sdl.com) when adding the project credential.

    Example:
    <MainTranslationProviderItem Uri="sdltm.http://gs2017dev.sdl.com/?orgPath=%2FTest%20API%2FJonckers&amp;tmName=TM%20API_en-US_de-DE" Enabled="true" />
    Note: You don't need to change this.  The URI in the template is correct with the prefix 'sdltm'.  The credential identity requires the host part of the URI http://gs2017dev.sdl.com; (i.e. without the schema)

    You would add the following credential to the automation API, ensuring Studio can access and communicate with the server based resource correctly.

    var serverAddress = "ps.http://gs2017dev.sdl.com"
    var userName = "MyUserName";

    var password = "MyPassword";

    project.Credentials.AddCredential(new Uri(serverAddress), false, userName, password);

    I have provided here and more complete example; let me know how it goes.


    Full Example

    using Sdl.Core.Globalization;
    using Sdl.ProjectAutomation.Core;
    using Sdl.ProjectAutomation.FileBased;
    using System;
    using System.Globalization;
    using System.Xml;

    namespace ProjectAutomation_UseTemplate
    {
        public class ProjectCreator
        {
            public void Create(string sourceFilesDirectory, string projectDirectory, string projectReportDiectory, string projectTemplateFile)
            {
                GetLanguageDirectionFromTemplate(projectTemplateFile, out var sourceLanguage, out var targetLanguage);
                if (sourceLanguage == null || targetLanguage == null)
                {
                    return;
                }

                var projectInfo = GetProjectInformation(sourceLanguage, targetLanguage, projectDirectory);
                var templateReference = new ProjectTemplateReference(projectTemplateFile);
                var project = new FileBasedProject(projectInfo, templateReference);

                AddFilesToProject(project, sourceFilesDirectory);
               

                                // Example of how you would need to construct the URI when adding the credential (e.g. ps.[host])
                var serverAddress = "ps.http://gs2017dev.sdl.com";
                var userName = "MyUserName";
                var password = "MyPassword";

                project.Credentials.AddCredential(new Uri(serverAddress), false, userName, password);
                project.Save();

                ConvertFiles(project);
                RunPreTranslateFiles(project, targetLanguage);

                project.Save();
            }


            private static void GetLanguageDirectionFromTemplate(string projectTemplateFile, out string sourceLanguage, out string targetLanguage)
            {
                var xmlTemplateFile = new XmlDocument();
                xmlTemplateFile.Load(projectTemplateFile);

                var languageDirectionPath = "/ProjectTemplate/LanguageDirections/LanguageDirection";
                var languageDirectionNode = xmlTemplateFile.DocumentElement?.SelectSingleNode(languageDirectionPath);
                if (languageDirectionNode?.Attributes != null)
                {
                    sourceLanguage = languageDirectionNode.Attributes["SourceLanguageCode"].InnerText;
                    targetLanguage = languageDirectionNode.Attributes["TargetLanguageCode"].InnerText;
                    return;
                }

                sourceLanguage = null;
                targetLanguage = null;
            }

            private static ProjectInfo GetProjectInformation(string sourceLanguage, string targetLanguage, string projectDirectory)
            {
                var info = new ProjectInfo
                {
                    Name = "TestProject",
                    LocalProjectFolder = projectDirectory
                };

                var srcLang = new Language(CultureInfo.GetCultureInfo(sourceLanguage));
                info.SourceLanguage = srcLang;

                var trgLang = new[] { new Language(CultureInfo.GetCultureInfo(targetLanguage)) };
                info.TargetLanguages = trgLang;

                return info;
            }

            private static void AddFilesToProject(IProject project, string sourceFilesDirectory)
            {
                project.AddFolderWithFiles(sourceFilesDirectory, true);

                var projectFiles = project.GetSourceLanguageFiles();

                var scanResult = project.RunAutomaticTask(
                    projectFiles.GetIds(),
                    AutomaticTaskTemplateIds.Scan
                );

                var files = project.GetSourceLanguageFiles();

                for (var i = 0; i < project.GetSourceLanguageFiles().Length; i++)
                {
                    Guid[] currentFileId = { files[i].Id };

                    var currentFileName = files[i].Name;
                    project.SetFileRole(currentFileId, FileRole.Translatable);
                }

                var preScan = project.RunAutomaticTask(
                    projectFiles.GetIds(),
                    AutomaticTaskTemplateIds.Scan
                );
            }

            private static void ConvertFiles(IProject project)
            {
                var files = project.GetSourceLanguageFiles();

                for (var i = 0; i < project.GetSourceLanguageFiles().Length; i++)
                {
                    if (files[i].Role == FileRole.Translatable)
                    {
                        Guid[] currentFileId = { files[i].Id };
                        var convertTask = project.RunAutomaticTask(
                            currentFileId,
                            AutomaticTaskTemplateIds.ConvertToTranslatableFormat
                        );

                        var copyTask = project.RunAutomaticTask(
                            currentFileId,
                            AutomaticTaskTemplateIds.CopyToTargetLanguages
                        );
                    }
                }
            }

            private static void RunPreTranslateFiles(IProject project, string targetLanguage)
            {
                var targetFiles = project.GetTargetLanguageFiles(new Language(CultureInfo.GetCultureInfo(targetLanguage)));

                var preTranslate = project.RunAutomaticTask(
                    targetFiles.GetIds(),
                    AutomaticTaskTemplateIds.PreTranslateFiles);
            }
        }
    }

  • Hmmm, I would expect properly generic example, i.e. including the part for extracting the server address from the project template.

    Your example uses hardcoded server address, but in real life one doesn't know this; it's exactly the thing which is hidden inside the project template and needs to be obtained programmatically.

    Moreover, how does one obtain the username and password? These also cannot be hardcoded - these need to be obtained somehow from the user (or the user's machine) where the code is actually running.
    The programmer won't know these...

  • Hi ,

    This work-around example is provided for clarity, putting into evidence the area in question; what to change in the URI.

    How this is eventually implemented, is entirely up to the developer and should not be seen as a substitute for your very own perfect coding practices.

Reply Children
No Data