public void Start(string languageCode)
        {
            CustomConsoleLogger.Log("Term Coordinator Started", ConsoleColor.Green);
            var batch = new List <string>();

            _termsRepository.ReadUnstranslatedTerms(languageCode, (term) =>
            {
                batch.Add(term);
                if (batch.Count != 100)
                {
                    return;
                }
                ProcessBatch();
            }, ProcessBatch);

            void ProcessBatch()
            {
                if (!batch.Any())
                {
                    return;
                }
                var result = _termTranslator.Translate(batch, languageCode);

                _termsRepository.SaveTranslations(result, languageCode);
                batch = new List <string>();
            }
        }
Exemplo n.º 2
0
        public Dictionary <string, string> Translate(List <string> terms, string languageCode)
        {
            var strBuilder = new StringBuilder();

            foreach (var term in terms)
            {
                strBuilder.AppendLine(term);
                strBuilder.AppendLine(Barrier);
            }
            var final = strBuilder.ToString();
            var url   = string.Format(TranslatorUrl, languageCode);

            _driver.Navigate().GoToUrl(url);
            var sourceTextArea = _driver.FindElement(By.CssSelector(_sourceTextSelector));

            sourceTextArea.SendKeys(final);
            var wait = new WebDriverWait(_driver, new TimeSpan(0, 0, 0, 20));

            wait.Until(driver =>
            {
                var targetSpan = driver.FindElements(By.XPath(_targetTextSelector));
                return(targetSpan.Any() && targetSpan[0].Text != string.Empty);
            });
            var translatedText = _driver.FindElement(By.XPath(_targetTextSelector)).Text;
            var result         = ProcessResult(terms, translatedText);

            CustomConsoleLogger.Log($"Term Translator Translated {terms.Count} terms", ConsoleColor.Magenta);
            return(result);
        }
        private void DoScan(ScanningOptions options)
        {
            var masterDb = Sitecore.Configuration.Factory.GetDatabase("master");

            ScanItem(options.RootPath);
            void ScanItem(string path)
            {
                var rootContent = masterDb.GetItem(path);

                if (rootContent == null)
                {
                    return;
                }
                rootContent.Fields.ReadAll();
                var includedFields = rootContent.Fields.Where(f =>
                                                              options.ScanFieldTypes.Contains(f.Type) && !string.IsNullOrEmpty(f.Type) && !f.Name.StartsWith("__")).ToArray();

                CustomConsoleLogger.Log($"Reading content {rootContent.Paths.FullPath} found {includedFields.Length} fields", ConsoleColor.DarkMagenta);
                var terms = includedFields.Select(f => f.Value).ToArray();

                _termsRepository.SaveTerms(terms);
                foreach (Item child in rootContent.Children)
                {
                    ScanItem(child.Paths.FullPath);
                }
            }

            Console.Write("Done");
        }
Exemplo n.º 4
0
 public void Start()
 {
     _driverService = ChromeDriverService.CreateDefaultService(_driverPath);
     _driverService.HideCommandPromptWindow = true;
     _driver = new ChromeDriver(_driverService);
     CustomConsoleLogger.Log("Term Translator Selenium Initialized", ConsoleColor.Green);
 }
        public void SaveTranslations(Dictionary <string, string> translations, string languageCode)
        {
            var insertQuery = new StringBuilder("BEGIN TRANSACTION ");

            foreach (var translation in translations)
            {
                var termId = _dbTermMap[translation.Key];
                var value  = translation.Value;
                insertQuery.AppendLine($"INSERT INTO Translations (Id, TermId, LanguageCode, Translation) VALUES ('{Guid.NewGuid()}','{termId}','{languageCode}', N'{value}');");
            }
            insertQuery.AppendLine("COMMIT;");
            using (var connection = GetDbConnection())
            {
                var query   = insertQuery.ToString();
                var command = new SqlCommand(query, connection);
                command.ExecuteNonQuery();
            }
            foreach (var translation in translations)
            {
                var term  = translation.Key;
                var value = translation.Value;
                if (!_termsLookup.ContainsKey(term))
                {
                    _termsLookup[term] = new Dictionary <string, string>();
                }
                _termsLookup[term][languageCode] = value;
            }
            CustomConsoleLogger.Log($"Term Repository Saved {translations.Count} new terms", ConsoleColor.Cyan);
        }
        public void LoadTerms()
        {
            var terms = new Dictionary <Guid, string>();

            using (var connection = GetDbConnection())
            {
                var getTermQuery   = "SELECT * FROM Terms;";
                var getTermCommand = new SqlCommand(getTermQuery, connection);
                var reader         = getTermCommand.ExecuteReader();
                while (reader.Read())
                {
                    var termId = reader.GetGuid(0);
                    var term   = reader.GetString(1);
                    terms[termId]      = term;
                    _dbTermMap[term]   = termId;
                    _termsLookup[term] = new Dictionary <string, string>();
                }
                reader.Close();
                var getTranslationQuery   = "SELECT * FROM Translations;";
                var getTranslationCommand = new SqlCommand(getTranslationQuery, connection);
                var translationReader     = getTranslationCommand.ExecuteReader();
                while (translationReader.Read())
                {
                    var termId = translationReader.GetGuid(1);
                    var lang   = translationReader.GetString(2);
                    var value  = translationReader.GetString(3);
                    if (!terms.ContainsKey(termId))
                    {
                        continue;
                    }
                    _termsLookup[terms[termId]][lang] = value;
                }
                translationReader.Close();
            }
            CustomConsoleLogger.Log($"Term Repository Initialized, found {_termsLookup.Count} terms", ConsoleColor.Green);
        }
 public void Start(ScanningOptions options)
 {
     CustomConsoleLogger.Log("Content Reader Started", ConsoleColor.Green);
     DoScan(options);
 }
Exemplo n.º 8
0
        private void DoScan(WriteScanningOptions options)
        {
            var language = options.TargetLanguageCode;
            var masterDb = Sitecore.Configuration.Factory.GetDatabase("master");

            using (var disabler = new SecurityDisabler())
            {
                ScanItem(options.RootPath);
            }
            void ScanItem(string path)
            {
                var rootContent = masterDb.GetItem(path, Language.Parse(options.SitecoreLanguageCode));
                var enContent   = masterDb.GetItem(path);

                if (enContent == null || rootContent == null)
                {
                    CustomConsoleLogger.Log($"{path} must have a language version of {language}", ConsoleColor.Red);
                    return;
                }
                if (!options.ShouldWrite(rootContent))
                {
                    goto NEXT;
                }
                rootContent.Fields.ReadAll();
                enContent.Fields.ReadAll();
                var includedFields = enContent.Fields.Where(f =>
                                                            options.ScanFieldTypes.Contains(f.Type) && !string.IsNullOrEmpty(f.Type) &&
                                                            !f.Name.StartsWith("__")).ToArray();

                if (includedFields.Any())
                {
                    var fieldCount = 0;
                    rootContent.Editing.BeginEdit();
                    foreach (var enField in includedFields)
                    {
                        var term        = enField.Value;
                        var translation = _termsRepository.GetTranslation(term, language);
                        if (translation != null)
                        {
                            var matchingField = rootContent.Fields.FirstOrDefault(f => f.ID == enField.ID);
                            if (matchingField != null)
                            {
                                fieldCount++;
                                matchingField.Value = translation;
                            }
                        }
                    }

                    rootContent.Editing.EndEdit();
                    if (fieldCount > 0)
                    {
                        CustomConsoleLogger.Log(
                            $"Updated content {rootContent.Paths.FullPath} found {fieldCount} fields",
                            ConsoleColor.DarkMagenta);
                    }
                }
NEXT:
                foreach (Item child in rootContent.Children)
                {
                    ScanItem(child.Paths.FullPath);
                }
            }

            Console.Write("Done");
        }