Esempio n. 1
0
        /// <summary>
        /// Selects the particular server TM as chosen by the user through the dropdown list.
        /// </summary>
        public void SelectServerTm(string tmName, string uri, string userName, string password)
        {
            var tmServer = new TranslationProviderServer(GetUri(uri), false, userName, password);

            ServerTM = tmServer.GetTranslationMemory(tmName,
                                                     TranslationMemoryProperties.None);
            Server = true;
        }
        public void DeleteTM()
        {
            ServerBasedTranslationMemory tm = TMServer.GetTranslationMemory(ParentOrganizationPath + "/APISampleTest", TranslationMemoryProperties.None);

            if (tm != null)
            {
                tm.Delete();
                WriteResult("TM deleted\r\n");
            }
        }
        private ServerBasedTranslationMemoryLanguageDirection GetLanguageDirection(ServerBasedTranslationMemory tm, CultureInfo source, CultureInfo target)
        {
            foreach (ServerBasedTranslationMemoryLanguageDirection item in tm.LanguageDirections)
            {
                if (item.SourceLanguage == source && item.TargetLanguage == target)
                {
                    return(item);
                }
            }

            throw new Exception("Requested direction doesn't exist.");
        }
Esempio n. 4
0
 private async Task <ServerBasedTranslationMemory> GetServerBasedTMs()
 {
     try
     {
         return(await ServerBasedTranslationMemory.CreateAsync(userNameTxtBox.Text, passwordTxtBox.Text, serverNameTxtBox.Text));
     }
     catch (Exception e)
     {
         MessageBox.Show(e.ToString());
         throw;
     }
 }
Esempio n. 5
0
        public void DeleteTm(TranslationProviderServer tmServer, string organizationPath, string tmName)
        {
            string tmPath = organizationPath;

            if (!tmPath.EndsWith("/"))
            {
                tmPath += "/";
            }

            ServerBasedTranslationMemory tm = tmServer.GetTranslationMemory(tmPath + tmName, TranslationMemoryProperties.All);

            tm.Delete();
        }
        public void CreateNewTM()
        {
            //Create a new testing container first
            TranslationMemoryContainer container = CreateContainer("APITest");

            //Test if TM already exists
            if (TMAlreadyExists("APISampleTest"))
            {
                throw new Exception("TM already exists!");
            }

            ServerBasedTranslationMemory newTM = new ServerBasedTranslationMemory(TMServer);

            newTM.Container   = container;
            newTM.Name        = "APISampleTest";
            newTM.Description = "A sample created as example of using TM API.";
            CreateLanguageDirections(newTM.LanguageDirections);
            newTM.LanguageResourcesTemplate = CreateLanguageResouceTemplate();
            newTM.ParentResourceGroupPath   = ParentOrganizationPath;
            newTM.Save();

            WriteResult("TM APISampleTest was created\r\n");
        }
Esempio n. 7
0
        public void Execute()
        {
            var uri = new Uri(@"Add your url");
            var translationProviderServer = new TranslationProviderServer(uri, false, "user name", "password");

            var serverTm = new ServerBasedTranslationMemory(translationProviderServer)
            {
                Name = "Tm from API"
            };
            var resourceTemplate =
                translationProviderServer.GetLanguageResourcesTemplates(LanguageResourcesTemplateProperties.All);
            var containters = translationProviderServer.GetContainers(ContainerProperties.All);
            var container   = containters.FirstOrDefault(c => c.Name.Equals("APSIC_TM_Container"));

            if (container != null)
            {
                serverTm.Container = container;
                serverTm.ParentResourceGroupPath   = container.ParentResourceGroupPath;
                serverTm.LanguageResourcesTemplate = resourceTemplate?[0];
                CreateLanguageDirections(serverTm.LanguageDirections);

                serverTm.Save();
            }
        }
Esempio n. 8
0
        public void Create(TranslationProviderServer tmServer, string organizationPath, string containerName, string tmName)
        {
            #region "CheckExists"
            foreach (ServerBasedTranslationMemory item in tmServer.GetTranslationMemories(TranslationMemoryProperties.None))
            {
                if (item.Name == tmName)
                {
                    throw new Exception("TM with that name already exists.");
                }
            }
            #endregion

            #region "TM"
            ServerBasedTranslationMemory newTM = new ServerBasedTranslationMemory(tmServer);
            newTM.Name        = tmName;
            newTM.Description = "Programmatically created sample TM";
            newTM.Copyright   = "(c) 2010 SDL International";
            #endregion

            string containerPath = organizationPath;
            if (!containerPath.EndsWith("/"))
            {
                containerPath += "/";
            }

            #region "container"
            containerPath += containerName;
            TranslationMemoryContainer container = tmServer.GetContainer(containerPath, GetContainerProperties());
            newTM.Container = container;
            #endregion

            #region "LanguageDirection"
            CreateLanguageDirections(newTM.LanguageDirections);
            #endregion

            #region "org"
            newTM.ParentResourceGroupPath = organizationPath;
            #endregion

            string templatePath = organizationPath;
            if (!templatePath.EndsWith("/"))
            {
                templatePath += "/";
            }

            #region "templates"
            string sampleFieldTemplateName = "MyFieldTemplate";
            foreach (ServerBasedFieldsTemplate template in tmServer.GetFieldsTemplates(FieldsTemplateProperties.All))
            {
                if (template.Name == sampleFieldTemplateName)
                {
                    newTM.FieldsTemplate = tmServer.GetFieldsTemplate(
                        templatePath + sampleFieldTemplateName, FieldsTemplateProperties.Fields);
                    break;
                }
            }

            string sampleLanguageResourcesTemplateName = "MyLanguageResourcesTemplate";
            foreach (ServerBasedLanguageResourcesTemplate template in tmServer.GetLanguageResourcesTemplates(
                         LanguageResourcesTemplateProperties.LanguageResources))
            {
                if (template.Name == sampleLanguageResourcesTemplateName)
                {
                    newTM.LanguageResourcesTemplate = tmServer.GetLanguageResourcesTemplate(
                        templatePath + sampleLanguageResourcesTemplateName, LanguageResourcesTemplateProperties.None);
                    break;
                }
            }
            #endregion


            newTM.Save();
        }
        public void ExportToTmx(TranslationProviderServer tmServer, string orgName, string tmName, string exportFilePath)
        {
            #region "OpenTm"
            if (!orgName.StartsWith("/"))
            {
                orgName = "/" + orgName;
            }
            if (!orgName.EndsWith("/"))
            {
                orgName += "/";
            }
            ServerBasedTranslationMemory tm = tmServer.GetTranslationMemory(
                orgName + tmName, TranslationMemoryProperties.All);
            #endregion

            #region "exporter"
            ScheduledServerTranslationMemoryExport exporter = new ScheduledServerTranslationMemoryExport(
                this.GetLanguageDirection(tm, CultureInfo.GetCultureInfo("en-US"), CultureInfo.GetCultureInfo("de-DE")));
            #endregion

            #region "settings"
            exporter.ChunkSize       = 25;
            exporter.ContinueOnError = true;
            #endregion

            #region "wait"
            exporter.Queue();
            exporter.Refresh();

            bool continueWaiting = true;
            while (continueWaiting)
            {
                switch (exporter.Status)
                {
                case ScheduledOperationStatus.Abort:
                case ScheduledOperationStatus.Aborted:
                case ScheduledOperationStatus.Cancel:
                case ScheduledOperationStatus.Cancelled:
                case ScheduledOperationStatus.Completed:
                case ScheduledOperationStatus.Error:
                    continueWaiting = false;
                    break;

                case ScheduledOperationStatus.Aborting:
                case ScheduledOperationStatus.Allocated:
                case ScheduledOperationStatus.Cancelling:
                case ScheduledOperationStatus.NotSet:
                case ScheduledOperationStatus.Queued:
                case ScheduledOperationStatus.Recovered:
                case ScheduledOperationStatus.Recovering:
                case ScheduledOperationStatus.Recovery:
                    continueWaiting = true;
                    exporter.Refresh();
                    break;

                default:
                    continueWaiting = false;
                    break;
                }
            }
            #endregion

            #region "completed"
            if (exporter.Status == ScheduledOperationStatus.Completed)
            {
                using (Stream outputStream = new FileStream(exportFilePath, FileMode.Create))
                {
                    var result = exporter.DownloadExport(outputStream);
                    if (result)
                    {
                        MessageBox.Show("Export successfuly finished.");
                    }
                }
            }
            else if (exporter.Status == ScheduledOperationStatus.Error)
            {
                MessageBox.Show(exporter.ErrorMessage);
            }
            else
            {
                MessageBox.Show("Export did not finish.");
            }
            #endregion
        }
        public void ImportTmx(TranslationProviderServer tmServer, string orgName, string tmName, string importFilePath)
        {
            if (!orgName.StartsWith("/"))
            {
                orgName = "/" + orgName;
            }
            if (!orgName.EndsWith("/"))
            {
                orgName += "/";
            }
            #region "OpenTm"
            ServerBasedTranslationMemory tm = tmServer.GetTranslationMemory(
                orgName + tmName, TranslationMemoryProperties.All);
            #endregion

            #region "importer"
            ScheduledServerTranslationMemoryImport importer = new ScheduledServerTranslationMemoryImport(
                GetLanguageDirection(tm, CultureInfo.GetCultureInfo("en-US"), CultureInfo.GetCultureInfo("de-DE")));
            #endregion

            #region "params"
            importer.ChunkSize       = 25;
            importer.ContinueOnError = true;
            importer.Source          = new FileInfo(importFilePath);
            GetImportSettings(importer.ImportSettings);
            #endregion

            #region "upload"

            importer.Queue();
            #endregion

            #region "wait"
            bool continueWaiting = true;
            while (continueWaiting)
            {
                switch (importer.Status)
                {
                case ScheduledOperationStatus.Abort:
                case ScheduledOperationStatus.Aborted:
                case ScheduledOperationStatus.Cancel:
                case ScheduledOperationStatus.Cancelled:
                case ScheduledOperationStatus.Completed:
                case ScheduledOperationStatus.Error:
                    continueWaiting = false;
                    break;

                case ScheduledOperationStatus.Aborting:
                case ScheduledOperationStatus.Allocated:
                case ScheduledOperationStatus.Cancelling:
                case ScheduledOperationStatus.NotSet:
                case ScheduledOperationStatus.Queued:
                case ScheduledOperationStatus.Recovered:
                case ScheduledOperationStatus.Recovering:
                case ScheduledOperationStatus.Recovery:
                    continueWaiting = true;
                    importer.Refresh();
                    break;

                default:
                    continueWaiting = false;
                    break;
                }
            }
            #endregion

            #region "completed"
            if (importer.Status == ScheduledOperationStatus.Completed)
            {
                MessageBox.Show("Import successfuly finished.");
            }
            else if (importer.Status == ScheduledOperationStatus.Error)
            {
                MessageBox.Show(importer.ErrorMessage);
            }
            else
            {
                MessageBox.Show("Import didn't finish.");
            }
            #endregion
        }
Esempio n. 11
0
        private int UpdateCustomFields(ProgressDialogContext context, TmFile tmFile, List <TmTranslationUnit> translationUnits,
                                       IReadOnlyCollection <TmTranslationUnit> units, ServerBasedTranslationMemory serverBasedTm)
        {
            var updatedCount = 0;

            decimal iCurrent    = 0;
            decimal iTotalUnits = translationUnits.Count;
            var     groupsOf    = 100;

            var tusGroups = new List <List <TmTranslationUnit> > {
                new List <TmTranslationUnit>(units)
            };

            if (units.Count > groupsOf)
            {
                tusGroups = translationUnits.ChunkBy(groupsOf);
            }

            foreach (var tus in tusGroups)
            {
                iCurrent = iCurrent + tus.Count;
                if (context != null && context.CheckCancellationPending())
                {
                    break;
                }

                var progress = iCurrent / iTotalUnits * 100;
                context?.Report(Convert.ToInt32(progress), "Updating: " + iCurrent + " of " + iTotalUnits + " Translation Units");

                foreach (var languageDirection in serverBasedTm.LanguageDirections)
                {
                    var tusToUpdate = new List <TranslationUnit>();
                    foreach (var tu in tus)
                    {
                        if (languageDirection.SourceLanguage.Name.Equals(tu.SourceSegment.Language) &&
                            languageDirection.TargetLanguage.Name.Equals(tu.TargetSegment.Language))
                        {
                            var unit = _tmService.CreateTranslationUnit(tu, languageDirection);
                            tusToUpdate.Add(unit);
                        }
                    }

                    if (tusToUpdate.Count > 0)
                    {
                        var results = languageDirection.UpdateTranslationUnits(tusToUpdate.ToArray());
                        foreach (var result in results)
                        {
                            if (result.Action != LanguagePlatform.TranslationMemory.Action.Error)
                            {
                                updatedCount++;
                            }
                        }
                    }
                }
            }

            foreach (var languageDirection in tmFile.TmLanguageDirections)
            {
                _tmService.SaveTmCacheStorage(context, tmFile, languageDirection);
            }

            return(updatedCount);
        }
        public void ImportTMX(string importFilePath)
        {
            ServerBasedTranslationMemory tm = TMServer.GetTranslationMemory(ParentOrganizationPath + "/APISampleTest", TranslationMemoryProperties.All);
            ScheduledTranslationMemoryImportOperation importer =
                new ScheduledTranslationMemoryImportOperation(
                    GetLanguageDirection(tm, CultureInfo.GetCultureInfo("en-US"), CultureInfo.GetCultureInfo("de-DE")));

            importer.ChunkSize       = 25; //25 is minimum
            importer.ContinueOnError = true;
            importer.Source          = new FileInfo(importFilePath);
            AdaptImportSettigns(importer.ImportSettings);

            //create import placeholder in the TM server
            importer.Create();
            //Uploads the source file
            importer.Upload(importer_Uploaded);
            //add the import into the queue
            importer.Queue();

            //Wait until the import is finished
            bool continueWaiting = true;

            while (continueWaiting)
            {
                switch (importer.Status)
                {
                case ScheduledOperationStatus.Abort:
                case ScheduledOperationStatus.Aborted:
                case ScheduledOperationStatus.Cancel:
                case ScheduledOperationStatus.Cancelled:
                case ScheduledOperationStatus.Completed:
                case ScheduledOperationStatus.Error:
                    continueWaiting = false;
                    break;

                case ScheduledOperationStatus.Aborting:
                case ScheduledOperationStatus.Allocated:
                case ScheduledOperationStatus.Cancelling:
                case ScheduledOperationStatus.NotSet:
                case ScheduledOperationStatus.Queued:
                case ScheduledOperationStatus.Recovered:
                case ScheduledOperationStatus.Recovering:
                case ScheduledOperationStatus.Recovery:
                    continueWaiting = true;
                    importer.Refresh();     //refresh the state of the import
                    break;

                default:
                    continueWaiting = false;
                    break;
                }
            }

            if (importer.Status == ScheduledOperationStatus.Completed)
            {
                WriteResult("Import successfuly finished.\r\n");
            }
            else if (importer.Status == ScheduledOperationStatus.Error)
            {
                WriteResult(importer.ErrorMessage);
            }
            else
            {
                WriteResult("Import didn't finish.\r\n");
            }
        }
Esempio n. 13
0
        private int UpdateSystemFields(ProgressDialogContext context, TmFile tmFile, List <User> uniqueUsers,
                                       List <TmTranslationUnit> translationUnits, ServerBasedTranslationMemory serverBasedTm)
        {
            var     updatedCount = 0;
            decimal iCurrent     = 0;
            decimal iTotalUnits  = translationUnits.Count;
            var     groupsOf     = 100;

            var tusGroups = new List <List <TmTranslationUnit> > {
                new List <TmTranslationUnit>(translationUnits)
            };

            if (translationUnits.Count > groupsOf)
            {
                tusGroups = translationUnits.ChunkBy(groupsOf);
            }

            foreach (var tus in tusGroups)
            {
                iCurrent = iCurrent + tus.Count;
                if (context != null && context.CheckCancellationPending())
                {
                    break;
                }

                var progress = iCurrent / iTotalUnits * 100;
                context?.Report(Convert.ToInt32(progress), string.Format(StringResources.Updating_0_of_1_Translation_Units, iCurrent, iTotalUnits));

                var filteredTusToUpdate = new List <TmTranslationUnit>();
                foreach (var tu in tus)
                {
                    if (UpdateSystemFields(uniqueUsers, tu))
                    {
                        filteredTusToUpdate.Add(tu);
                    }
                }

                if (filteredTusToUpdate.Count > 0)
                {
                    foreach (var languageDirection in serverBasedTm.LanguageDirections)
                    {
                        var tusToUpdate = new List <TranslationUnit>();
                        foreach (var tu in filteredTusToUpdate)
                        {
                            if (languageDirection.SourceLanguage.Name.Equals(tu.SourceSegment.Language) &&
                                languageDirection.TargetLanguage.Name.Equals(tu.TargetSegment.Language))
                            {
                                var unit = _tmService.CreateTranslationUnit(tu, languageDirection);
                                tusToUpdate.Add(unit);
                            }
                        }

                        if (tusToUpdate.Count > 0)
                        {
                            var results = languageDirection.UpdateTranslationUnits(tusToUpdate.ToArray());
                            updatedCount += results.Count(result => result.Action != LanguagePlatform.TranslationMemory.Action.Error);
                        }
                    }
                }
            }

            serverBasedTm.Save();

            foreach (var languageDirection in tmFile.TmLanguageDirections)
            {
                _tmService.SaveTmCacheStorage(context, tmFile, languageDirection);
            }

            return(updatedCount);
        }