/// ------------------------------------------------------------------------------------ /// <summary> /// Returns an enumerator that iterates through the collection of questions. /// </summary> /// <returns> /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to /// iterate through the collection. /// </returns> /// ------------------------------------------------------------------------------------ public static void GenerateAlternateForms(QuestionSections sections, Dictionary <string, string[]> alternatives) { string[] altForms; foreach (Section section in sections.Items) { foreach (Category category in section.Categories) { if (category.Questions == null) { continue; } foreach (Question q in category.Questions) { if (alternatives != null && alternatives.TryGetValue(q.Text, out altForms)) { q.AlternateForms = altForms; } else { QuestionProviderAlternateFormBuilder bldr = new QuestionProviderAlternateFormBuilder(q.Text); if (bldr.Alternatives.Count > 1) { q.AlternateForms = new string[bldr.Alternatives.Count]; for (int index = 0; index < bldr.Alternatives.Count; index++) { q.AlternateForms[index] = bldr.Alternatives[index]; } } } } } } }
/// ------------------------------------------------------------------------------------ /// <summary> /// Returns an enumerator that iterates through the collection of questions. /// </summary> /// <returns> /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to /// iterate through the collection. /// </returns> /// ------------------------------------------------------------------------------------ public static void GenerateAlternateForms(QuestionSections sections, Dictionary <string, string[]> alternatives) { foreach (Section section in sections.Items) { foreach (Category category in section.Categories) { if (category.Questions == null) { continue; } foreach (Question q in category.Questions) { if (alternatives != null && alternatives.TryGetValue(q.Text, out var altForms)) { q.Alternatives = altForms.Select(f => new AlternativeForm { Text = f }).ToArray(); } else { var bldr = new QuestionProviderAlternateFormBuilder(q.Text); if (bldr.Alternatives.Count > 1) { q.Alternatives = bldr.Alternatives.Select(f => new AlternativeForm { Text = f }).ToArray(); } } } } } }
internal void GenerateOrUpdateFromMasterQuestions(QuestionSections questions, List <XmlTranslation> existingTxlTranslations = null, bool retainOnlyTranslatedStrings = false) { InitializeLookupTable(); // Note: there are two possible sources for existing localized translations of strings: either a Transcelerator project // (the list passed into this method), or the content read from a previous version of the file represented by this accessor. var existingLocalizations = XliffRoot.File.Body; existingLocalizations.DeleteGroupsWithoutLocalizations(); if (existingLocalizations.Groups == null) { existingLocalizations = null; } if (existingTxlTranslations == null && retainOnlyTranslatedStrings) { return; } InitializeLocalizations(); if (existingTxlTranslations == null) { if (existingLocalizations == null) { AddTranslationUnit = (group, data) => group.AddTranslationUnit(data); } else { AddTranslationUnit = (group, data) => { var tu = existingLocalizations.GetStringLocalization(data); if (tu == null) { group.AddTranslationUnit(data); } else { group.AddTranslationUnit(tu); } }; } } else { if (existingLocalizations == null) { AddTranslationUnit = (group, data) => { group.AddTranslationUnit(data, LookupTranslation(existingTxlTranslations, data)); }; } else { AddTranslationUnit = (group, data) => { var tu = existingLocalizations.GetStringLocalization(data); if (tu == null) { group.AddTranslationUnit(data, LookupTranslation(existingTxlTranslations, data)); } else { group.AddTranslationUnit(tu); } }; } } UIDataString key; foreach (var section in questions.Items) { var sectionGroup = new Group { Id = FileBody.GetSectionId(section) }; Localizations.Groups.Add(sectionGroup); key = new UISectionHeadDataString(new SectionInfo(section)); AddTranslationUnit(sectionGroup, key); foreach (Category category in section.Categories) { var categoryGroup = sectionGroup.AddSubGroup(category.Type); if (category.Type != null) { if (!Localizations.Categories.TranslationUnits.Any(tu => tu.English == category.Type)) { key = new UISimpleDataString(category.Type, LocalizableStringType.Category); AddTranslationUnit(Localizations.Categories, key); } } foreach (Question q in category.Questions.Where(q => !String.IsNullOrWhiteSpace(q.Text))) { if (q.ScriptureReference == null) { q.ScriptureReference = section.ScriptureReference; q.StartRef = section.StartRef; q.EndRef = section.EndRef; } // The following line handles the unusual case of the same question twice in the same verse. var questionGroup = categoryGroup.SubGroups?.SingleOrDefault(qg => qg.Id == $"{FileBody.kQuestionIdPrefix}{q.ScriptureReference}+{q.PhraseInUse}"); if (questionGroup == null) { questionGroup = categoryGroup.AddSubGroup($"{FileBody.kQuestionIdPrefix}{q.ScriptureReference}+{q.PhraseInUse}"); key = new UIQuestionDataString(q, true, false); AddTranslationUnit(questionGroup, key); } AddAlternatesSubgroupAndLocalizableStringsIfNeeded(q, questionGroup); AddAnswerOrNoteSubgroupAndLocalizableStringsIfNeeded(q, questionGroup, LocalizableStringType.Answer, qu => qu.Answers); AddAnswerOrNoteSubgroupAndLocalizableStringsIfNeeded(q, questionGroup, LocalizableStringType.Note, qu => qu.Notes); } } } if (retainOnlyTranslatedStrings) { Localizations.DeleteGroupsWithoutLocalizations(); } AddTranslationUnit = null; }
public static QuestionSections Generate(IEnumerable <string> sourceLines, Dictionary <string, string[]> alternatives) { m_canonicalBookNumbers = new HashSet <int>(); // Initialize the ID textbox. Category currCat = null; string currRef = null; int startRef = 0, endRef = 0; Section currSection = null; bool currSectionRefSet = false; bool currQuestionRefBasedOnAnswer = false; Question currQuestion = null; List <Section> sections = new List <Section>(); List <Question> currentQuestions = new List <Question>(); int cAnswers = 0, cComments = 0, cCategories = 0; int kSectHeadMarkerLen = s_kSectionHead.Length; int kRefMarkerLen = s_kRefMarker.Length; int kQMarkerLen = s_kQuestionMarker.Length; int kAMarkerLen = s_kAnswerMarker.Length; int kCommentMarkerLen = s_kCommentMarker.Length; Debug.Assert(s_kDetailsMarker.Length == s_kOverviewMarker.Length); int kCategoryMarkerLen = s_kDetailsMarker.Length; Regex regexVerseNum = new Regex(@"\(((?<chapter>\d+):)?((?<startVerse>\d+)(a|b)?)(.*((, )|-)(?<endVerse>\d+)(a|b)?)?\)", RegexOptions.Compiled); foreach (string sLine in SourceFields(sourceLines)) { if (sLine.StartsWith(s_kQuestionMarker)) { if (currQuestion != null && cAnswers == 0 && cComments == 0 && (sLine.ToLowerInvariant() == s_kQuestionMarker + " -or-" || (currQuestion.Text != null && currQuestion.Text.ToLowerInvariant().EndsWith("-or-")))) { // Question continued in a subsequent field. Just append the text to the existing question. currQuestion.Text += " " + sLine.Substring(kQMarkerLen).Trim(); } else { currQuestion = new Question(); currentQuestions.Add(currQuestion); currQuestion.Text = sLine.Substring(kQMarkerLen).Trim(); if (currRef != currSection.ScriptureReference) { currQuestion.ScriptureReference = currRef; currQuestion.StartRef = startRef; currQuestion.EndRef = endRef; currQuestionRefBasedOnAnswer = false; } cAnswers = 0; cComments = 0; } } else if (sLine.StartsWith(s_kAnswerMarker)) { if (currQuestion == null) { Debug.Fail("Answer \"" + sLine + "\" does not have a corresponding question. (Probably \\tqref line is misplaced.)"); } string currAnswer = sLine.Substring(kAMarkerLen).Trim(); if (!String.IsNullOrEmpty(currAnswer)) { if (!currCat.IsOverview) { AdjustDetailQuestionRefBasedOnAnswer(regexVerseNum, currAnswer, currSection, currQuestion, ref currQuestionRefBasedOnAnswer); } string[] source = currQuestion.Answers; currQuestion.Answers = new string[cAnswers + 1]; if (source != null) { Array.Copy(source, currQuestion.Answers, cAnswers); } currQuestion.Answers[cAnswers++] = currAnswer; } } else if (sLine.StartsWith(s_kCommentMarker)) { if (currQuestion != null) { var note = sLine.Substring(kCommentMarkerLen).Trim(); if (!String.IsNullOrWhiteSpace(note)) { string[] source = currQuestion.Notes; currQuestion.Notes = new string[cComments + 1]; if (source != null) { Array.Copy(source, currQuestion.Notes, cComments); } currQuestion.Notes[cComments++] = note; } } } else { if (sLine.StartsWith(s_kRefMarker)) { currRef = sLine.Substring(kRefMarkerLen).Trim(); Parse(currRef, out startRef, out endRef); if (!currSectionRefSet) { currSection.ScriptureReference = currRef; currSection.StartRef = startRef; currSection.EndRef = endRef; currSectionRefSet = true; } } else if (sLine.StartsWith(s_kSectionHead)) { if (currentQuestions.Count > 0) { currCat.Questions.Clear(); currCat.Questions.AddRange(currentQuestions); currentQuestions.Clear(); } currSection = new Section(); sections.Add(currSection); cCategories = 1; currSection.Categories = new Category[cCategories]; currSection.Categories[0] = currCat = new Category(); currSection.Heading = sLine.Substring(kSectHeadMarkerLen).Trim(); currSectionRefSet = false; } else { bool isOverviewMarker = sLine.StartsWith(s_kOverviewMarker); if (isOverviewMarker || sLine.StartsWith(s_kDetailsMarker)) { if (currentQuestions.Count > 0) { currCat.Questions.Clear(); currCat.Questions.AddRange(currentQuestions); currentQuestions.Clear(); } if (currCat.Type != null || currCat.Questions.Count > 0) { currCat = new Category(); Category[] source = currSection.Categories; currSection.Categories = new Category[cCategories + 1]; if (source != null) { Array.Copy(source, currSection.Categories, cCategories); } currSection.Categories[cCategories++] = currCat; } currCat.Type = sLine.Substring(kCategoryMarkerLen).Trim(); currCat.IsOverview = isOverviewMarker; } } if (currQuestion != null) { currQuestion = null; cAnswers = 0; cComments = 0; } } } if (currCat != null && currentQuestions.Count > 0) { currCat.Questions.Clear(); currCat.Questions.AddRange(currentQuestions); } QuestionSections questionSections = new QuestionSections(); questionSections.Items = sections.ToArray(); GenerateAlternateForms(questionSections, alternatives); return(questionSections); }