private void SetUpDisplayOfTopicInBook(DataSet data)
{
var topicPageElement = this._dom.SelectSingleNode("//div[@data-derived='topic']");
if (topicPageElement == null)
{
//old-style. here we don't have the data-derived, so we need to avoid picking from the datadiv
topicPageElement = this._dom.SelectSingleNode("//div[not(id='bloomDataDiv')]//div[@data-book='topic']");
if (topicPageElement == null)
{
//most unit tests do not have complete books, so this not surprising. It just means we don't have anything to do
return;
}
}
//clear it out what's there now
topicPageElement.RemoveAttribute("lang");
topicPageElement.InnerText = "";
NamedMutliLingualValue topicData;
var parentOfTopicDisplayElement = ((XmlElement)(topicPageElement.ParentNode));
//this just lets us have css rules that vary if there is a topic (allows other text to be centered instead left-aligned)
//we'll change it later if we find there is a topic
parentOfTopicDisplayElement.SetAttribute("data-have-topic", "false");
//if we have no topic element in the data-div
//leave the field in the page with an empty text.
if (!data.TextVariables.TryGetValue("topic", out topicData))
{
return;
}
//we use English as the "key" for topics.
var englishTopic = topicData.TextAlternatives.GetExactAlternative("en");
//if we have no topic, just clear it out from the page
if (string.IsNullOrEmpty(englishTopic) || englishTopic == "NoTopic")
return;
parentOfTopicDisplayElement.SetAttribute("data-have-topic", "true");
var stringId = "Topics." + englishTopic;
//get the topic in the most prominent language for which we have a translation
var langOfTopicToShowOnCover = _collectionSettings.Language1Iso639Code;
if (LocalizationManager.GetIsStringAvailableForLangId(stringId, _collectionSettings.Language1Iso639Code))
{
langOfTopicToShowOnCover = _collectionSettings.Language1Iso639Code;
}
else if (LocalizationManager.GetIsStringAvailableForLangId(stringId, _collectionSettings.Language2Iso639Code))
{
langOfTopicToShowOnCover = _collectionSettings.Language2Iso639Code;
}
else if (LocalizationManager.GetIsStringAvailableForLangId(stringId, _collectionSettings.Language3Iso639Code))
{
langOfTopicToShowOnCover = _collectionSettings.Language3Iso639Code;
}
else
{
langOfTopicToShowOnCover = "en";
}
var bestTranslation = LocalizationManager.GetDynamicStringOrEnglish("Bloom", stringId, englishTopic,
"this is a book topic", langOfTopicToShowOnCover);
//NB: in a unit test environment, GetDynamicStringOrEnglish is going to give us the id back, which is annoying.
if (bestTranslation == stringId)
bestTranslation = englishTopic;
topicPageElement.SetAttribute("lang", langOfTopicToShowOnCover);
topicPageElement.InnerText = bestTranslation;
}