AI OnAI Off
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
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:
I am than using the following method to convert the XhtmlString to raw HTML:
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?