protected Dictionary <string, List <string> > GetTranslations(IArtifactDirectory directory) { var name = HarvesterNameAttribute.GetShort(typeof(THarvester)); var translations = new Dictionary <string, List <string> >(); foreach (var file in directory.GetFiles($"{name}.??.txt")) { Logger.Info("Read translation from file {filename}", Path.GetFileName(file)); var language = Regex.Match(file, $@"{name}\.(?<name>(.+))\.txt$").Groups["name"].Value.ToLowerInvariant(); using (var reader = directory.ReadTextFile(file)) { Func <string, string> transformationFunc = null; if (TranslationTransformings?.TryGetValue(language, out transformationFunc) == false) { Logger.Warn("No translation transformation function found for {lang}", language); } translations.Add(language, ReadTranslation(reader, transformationFunc)); } } return(translations); }
private static async Task RunHarvester(IHarvester harvester) { var name = HarvesterNameAttribute.GetName(harvester.GetType()); using (var writer = XmlWriter.Create($"{name}.xml", _xmlWriterSettings)) { await harvester.Harvest(writer, new DiskArtifactDirectory(name), _httpClient); } }
public override async Task Harvest(XmlWriter xmlWriter, IArtifactDirectory directory, HttpClient httpClient) { var originalTextsTask = GetOriginalTexts(httpClient); var name = HarvesterNameAttribute.GetShort(typeof(THarvester)); var translations = GetTranslations(directory); var(originalLanguage, originalTexts) = await originalTextsTask; var counter = 0; var hashIds = CardIdProvider.GetHashIds(); using (var translatableWriter = directory.CreateTextFile($"{name}.translatable.txt")) { for (var i = 0; i < originalTexts.Count; i++) { var originalText = HtmlUtilities.RemoveTags(originalTexts[i]); translatableWriter.WriteLine(TransformForTranslation(originalText)); translatableWriter.WriteLine(); xmlWriter.WriteStartElement(CardName); xmlWriter.WriteAttributeString("id", hashIds.Encode(SourceId, counter)); WriteAttributes(xmlWriter, originalText); WriteElements(xmlWriter); IEnumerable <(string lang, string text)> GetTexts() { yield return(originalLanguage, originalText); foreach (var translation in translations) { if (translation.Value.Count == counter - 1) { Logger.Error( "Translation for language {lang} has not enough lines for the site. The site may have received an update.", translation.Key); continue; } if (translation.Value.Count <= counter) { continue; } yield return(translation.Key, translation.Value[counter]); } } foreach (var(lang, text) in GetTexts()) { xmlWriter.WriteStartElement("Text"); xmlWriter.WriteAttributeString("lang", lang); xmlWriter.WriteString(text); xmlWriter.WriteEndElement(); } xmlWriter.WriteEndElement(); counter++; } } Logger.Info("{counter} {name} texts were created!", counter, name); }