Bloom.Book.TranslationGroupManager.UpdateContentLanguageClasses C# (CSharp) Method

UpdateContentLanguageClasses() public static method

We stick 'contentLanguage2' and 'contentLanguage3' classes on editable things in bilingual and trilingual books
public static UpdateContentLanguageClasses ( XmlNode elementOrDom, CollectionSettings settings, string vernacularIso, string contentLanguageIso2, string contentLanguageIso3 ) : void
elementOrDom System.Xml.XmlNode
settings Bloom.Collection.CollectionSettings
vernacularIso string
contentLanguageIso2 string
contentLanguageIso3 string
return void
        public static void UpdateContentLanguageClasses(XmlNode elementOrDom, CollectionSettings settings,
			string vernacularIso, string contentLanguageIso2, string contentLanguageIso3)
        {
            var multilingualClass = "bloom-monolingual";
            var contentLanguages = new Dictionary<string, string>();
            contentLanguages.Add(vernacularIso, "bloom-content1");

            if (!String.IsNullOrEmpty(contentLanguageIso2) && vernacularIso != contentLanguageIso2)
            {
                multilingualClass = "bloom-bilingual";
                contentLanguages.Add(contentLanguageIso2, "bloom-content2");
            }
            if (!String.IsNullOrEmpty(contentLanguageIso3) && vernacularIso != contentLanguageIso3 &&
                contentLanguageIso2 != contentLanguageIso3)
            {
                multilingualClass = "bloom-trilingual";
                contentLanguages.Add(contentLanguageIso3, "bloom-content3");
                Debug.Assert(!String.IsNullOrEmpty(contentLanguageIso2), "shouldn't have a content3 lang with no content2 lang");
            }

            //Stick a class in the page div telling the stylesheet how many languages we are displaying (only makes sense for content pages, in Jan 2012).
            foreach (
                XmlElement pageDiv in
                    elementOrDom.SafeSelectNodes(
                        "descendant-or-self::div[contains(@class,'bloom-page') and not(contains(@class,'bloom-frontMatter')) and not(contains(@class,'bloom-backMatter'))]")
                )
            {
                HtmlDom.RemoveClassesBeginingWith(pageDiv, "bloom-monolingual");
                HtmlDom.RemoveClassesBeginingWith(pageDiv, "bloom-bilingual");
                HtmlDom.RemoveClassesBeginingWith(pageDiv, "bloom-trilingual");
                HtmlDom.AddClassIfMissing(pageDiv, multilingualClass);
            }

            // This is the "code" part of the visibility system: https://goo.gl/EgnSJo
            foreach (XmlElement group in elementOrDom.SafeSelectNodes(".//*[contains(@class,'bloom-translationGroup')]"))
            {
                var dataDefaultLanguages = HtmlDom.GetAttributeValue(group, "data-default-languages").Split(new char[] { ',', ' ' },
                    StringSplitOptions.RemoveEmptyEntries);

                //nb: we don't necessarily care that a div is editable or not
                foreach (XmlElement e in @group.SafeSelectNodes(".//textarea | .//div"))
                {
                    HtmlDom.RemoveClassesBeginingWith(e, "bloom-content");
                    var lang = e.GetAttribute("lang");

                    //These bloom-content* classes are used by some stylesheet rules, primarily to boost the font-size of some languages.
                    //Enhance: this is too complex; the semantics of these overlap with each other and with bloom-visibility-code-on, and with data-language-order.
                    //It would be better to have non-overlapping things; 1 for order, 1 for visibility, one for the lang's role in this collection.
                    string orderClass;
                    if (contentLanguages.TryGetValue(lang, out orderClass))
                    {
                        HtmlDom.AddClass(e, orderClass); //bloom-content1, bloom-content2, bloom-content3
                    }

                    //Enhance: it's even more likely that we can get rid of these by replacing them with bloom-content2, bloom-content3
                    if (lang == settings.Language2Iso639Code)
                    {
                        HtmlDom.AddClass(e, "bloom-contentNational1");
                    }
                    if (lang == settings.Language3Iso639Code)
                    {
                        HtmlDom.AddClass(e, "bloom-contentNational2");
                    }

                    HtmlDom.RemoveClassesBeginingWith(e, "bloom-visibility-code");
                    if (ShouldNormallyShowEditable(lang, dataDefaultLanguages, contentLanguageIso2, contentLanguageIso3, settings))
                    {
                        HtmlDom.AddClass(e, "bloom-visibility-code-on");
                    }

                    UpdateRightToLeftSetting(settings, e, lang);
                }
            }
        }

Usage Example

        /// <summary>
        /// Returns the sequence of bloom-editable divs that would normally be created as the content of an empty bloom-translationGroup,
        /// given the current collection and book settings, with the classes that would normally be set on them prior to editing to make the right ones visible etc.
        /// </summary>
        public static string GetDefaultTranslationGroupContent(XmlNode pageOrDocumentNode, Book currentBook)
        {
            // First get a XMLDocument so that we can start creating elements using it.
            XmlDocument ownerDocument = pageOrDocumentNode?.OwnerDocument;

            if (ownerDocument == null)                  // the OwnerDocument child can be null if pageOrDocumentNode is a document itself
            {
                if (pageOrDocumentNode is XmlDocument)
                {
                    ownerDocument = pageOrDocumentNode as XmlDocument;
                }
                else
                {
                    return("");
                }
            }

            // We want to use the usual routines that insert the required bloom-editables and classes into existing translation groups.
            // To make this work we make a temporary translation group
            // Since one of the routines expects the TG to be a descendant of the element passed to it, we make another layer of temporary wrapper around the translation group as well
            var containerElement = ownerDocument.CreateElement("div");

            containerElement.SetAttribute("class", "bloom-translationGroup");

            var wrapper = ownerDocument.CreateElement("div");

            wrapper.AppendChild(containerElement);

            PrepareElementsInPageOrDocument(containerElement, currentBook.BookData);

            TranslationGroupManager.UpdateContentLanguageClasses(wrapper, currentBook.BookData, currentBook.Language1IsoCode,
                                                                 currentBook.Language2IsoCode, currentBook.Language3IsoCode);

            return(containerElement.InnerXml);
        }