Is there a way to properly localize Web API controllers? It would be useful if we could somehow fetch the current visitor's locale, and use this locale to set the thread's UI culture (i.e. for using dictionary).
For now I have worked around by providing the culture via query string, and an action filter:
public class LocalizableByCultureParameterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.Request.Properties.ContainsKey("MS_QueryNameValuePairs"))
{
var queryStrings = actionContext.Request.Properties["MS_QueryNameValuePairs"] as IEnumerable<KeyValuePair<string, string>>;
if (queryStrings != null && queryStrings.Count(q => q.Key == "culture") == 1)
{
try
{
var culture = queryStrings.Single(q => q.Key == "culture").Value;
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
}
catch (CultureNotFoundException cnf)
{
LogHelper.Error(typeof(LocalizableByCultureParameterAttribute), "Culture not found", cnf);
}
}
}
base.OnActionExecuting(actionContext);
}
}
Localizing Umbraco Web API controllers
Hi guys,
Is there a way to properly localize Web API controllers? It would be useful if we could somehow fetch the current visitor's locale, and use this locale to set the thread's UI culture (i.e. for using dictionary).
For now I have worked around by providing the culture via query string, and an action filter:
Grtz
L
is working on a reply...