private void UpdateTitle(BookInfo info = null)
{
NamedMutliLingualValue title;
if (_dataset.TextVariables.TryGetValue("bookTitleTemplate", out title))
{
//NB: In seleting from an ordered shopping list of priority entries, this is only
//handling a scenario where a single (title,writingsystem) pair is of interest.
//That's all we've needed thusfar. But we could imagine needing to work through each one.
var form = title.TextAlternatives.GetBestAlternative(WritingSystemIdsToTry);
//allow the title to be a template that pulls in data variables, e.g. "P1 Primer Term{book.term} Week {book.week}"
foreach (var dataItem in _dataset.TextVariables)
{
form.Form = form.Form.Replace("{" + dataItem.Key + "}", dataItem.Value.TextAlternatives.GetBestAlternativeString(WritingSystemIdsToTry));
}
_dom.Title = form.Form;
if (info != null)
info.Title =form.Form.Replace("<br />", ""); // Clean out breaks inserted at newlines.
this.Set("bookTitle", form.Form, form.WritingSystemId);
}
else if (_dataset.TextVariables.TryGetValue("bookTitle", out title))
{
var t = title.TextAlternatives.GetBestAlternativeString(WritingSystemIdsToTry);
_dom.Title = t;
if (info != null)
{
info.Title = TextOfInnerHtml(t.Replace("<br />", "")); // Clean out breaks inserted at newlines.
// Now build the AllTitles field
var sb = new StringBuilder();
sb.Append("{");
foreach (var langForm in title.TextAlternatives.Forms)
{
if (sb.Length > 1)
sb.Append(",");
sb.Append("\"");
sb.Append(langForm.WritingSystemId);
sb.Append("\":\"");
sb.Append(TextOfInnerHtml(langForm.Form).Replace("\\", "\\\\").Replace("\"", "\\\"")); // Escape backslash and double-quote
sb.Append("\"");
}
sb.Append("}");
info.AllTitles = sb.ToString();
}
}
}