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. 2
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. 3
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();
        }
Esempio n. 4
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);
        }