Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Lennart Stoop 304 posts 842 karma points
    Jun 26, 2014 @ 13:18
    Lennart Stoop
    0

    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:

    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);
        }
    }
    

    Grtz

    L

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies