Setting ContentFragment language in an XhtmlString

Vote:
 

I have page that hosts an XhtmlString property and the page has multiple translations. In the XhtmlString area there is also a block, which has multiple translations as well. I am trying to get the XhtmlString in a WebApi context and convert it to raw HTML. StaticFragment items in XhtmlString is being read correctly from the database according the language parameter I send to IContentLoader.Get<>. Example:

var page = _contentLoader.Get<OccupationInfoPage>(page.ContentLink, new CultureInfo("en");

 I am than using the following method to convert the XhtmlString to raw HTML:

        public static string ToRawHtml(this XhtmlString xhtmlstring)
        {
            if (string.IsNullOrWhiteSpace(xhtmlstring?.ToHtmlString()) || xhtmlstring.IsEmpty)
            {
                return string.Empty;
            }
            var routeData = new RouteData();
            routeData.Values.Add("controller", "a");
            routeData.Values.Add("subject", "a");

            var hh = new HtmlHelper(new ViewContext()
            {
                HttpContext = new HttpContextWrapper(HttpContext.Current),
                ViewData = new ViewDataDictionary(),
                TempData = new TempDataDictionary(),
                Controller = new DummyController(),
                RouteData = routeData
            }, new ViewPage());

            string result;
            using (var writer = new StringWriter())
            {
                hh.ViewContext.Writer = writer;
                hh.RenderXhtmlString(xhtmlstring);
                writer.Flush();
                result = writer.ToString();
            }
            return result;
        }

While this method returns the raw HTML I am looking for, with normalized links, it also defaults to master language for ContentFragment items. Is there any way to set the language in HtmlHelper object or somewhere else so I can get the correct version, both for the blocks and the links? 

#210409
Nov 21, 2019 17:48
Vote:
 

Hi,

This should be possible with something like this:

public static string ToRawHtml(this XhtmlString xhtmlString, CultureInfo culture = null)
{
    var contentLanguageAccessor = ServiceLocator.Current.GetInstance<IContentLanguageAccessor>();

    if (string.IsNullOrWhiteSpace(xhtmlString?.ToHtmlString()) || xhtmlString.IsEmpty)
    {
        return string.Empty;
    }

    var routeData = new RouteData();
    routeData.Values.Add("controller", "a");
    routeData.Values.Add("subject", "a");

    CultureInfo currentCulture = null;

    if (culture != null)
    {
        currentCulture = contentLanguageAccessor.Language;
        contentLanguageAccessor.Language = culture;
    }
    
    var hh = new HtmlHelper(new ViewContext
    {
        HttpContext = new HttpContextWrapper(HttpContext.Current),
        ViewData = new ViewDataDictionary(),
        TempData = new TempDataDictionary(),
        Controller = new DummyController(),
        RouteData = routeData
    }, new ViewPage());

    string result;

    using (var writer = new StringWriter())
    {
        hh.ViewContext.Writer = writer;
        hh.RenderXhtmlString(xhtmlString);
        writer.Flush();
        result = writer.ToString();
    }

    if (currentCulture != null)
    {
        contentLanguageAccessor.Language = currentCulture;
    }

    return result;
}

This just uses the IContentLanguageAccessor to set the preferred language for content to whatever you pass in, and then sets it back before returning.

It essence this sets the HttpContext EPiServer:ContentLanguage item to the specified language.

/Jake

#210427
Nov 22, 2019 18:36
Vote:
 

Hi Jake,

This seems to be working, thank you! 

#210430
Nov 22, 2019 20:14
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.