开发者

Setting thread Culture to a neutral culture

When try to save data to database with db_ent.SaveChanges() I have this error:

Culture 'sr' is a neutral culture. It cannot be used in formatting and parsing and therefore cannot be set as the thread's current culture. Any solutions for this?

Edit:

My RouteLocalization Helper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Threading;
using System.Globalization;

namespace darns.Helpers
{
public class MultiCultureMvcRouteHandler : MvcRouteHandler
{
    protected override IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext requestContext)
    {
        String culture = requestContext.RouteData.Values["culture"].ToString();
        var ci = new CultureInfo(culture);
        Thread.CurrentThread.CurrentUICulture = ci;
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
        return base.GetHttpHandler(requestContext);
    }
}

public class SingleCultureMvcRouteHandler : MvcRouteHandler {}

public class CultureConstraint : IRouteConstraint
{
    private string[] _values;
    public CultureConstraint(params string[] values)
    {
        this._values = values;
    }

    public bool Match(HttpContextBase httpContext,Route route,string parameterName,
                    RouteValueDictionary values, RouteDirection routeDirection)
    {
        // Get the value called "parameterName" from the 
        // RouteValueDictionary called "value"
        string value = values[parameterName].ToString();
        // Return true is the list of allowed values contains 
        // this value.
        return _values.Contains(value);
    }
}

public enum Culture
{       
    sr = 1,
    hr = 2,
    en = 3,
    ru = 4
}

/// <summary>
/// Util class for storing error message pairs
/// </summary>
/// <typeparam 开发者_如何转开发name="E"></typeparam>
/// <typeparam name="T"></typeparam>
public class ErrorPair<E, T>
{
    public E Key { get; set; }
    public T Message { get; set; }
    public ErrorPair(E key, T message)
    {
        Key = key;
        Message = message;
    }
}
}


I had a similar problem. User's browser was sending 'en' neutral culture.

So I used this to get .Net to pick the closest 'specific' culture:

//replace '$' in string with current user's culture's Currency Symbol
//if neutral culture use .Net to set SpecificCulture
//, e.g. 'en', .Net assumes 'en-US'
private static MvcHtmlString currencySubstitution(MvcHtmlString htmlString)
    {
        var ci = System.Threading.Thread.CurrentThread.CurrentUICulture;
        System.Globalization.RegionInfo ri;
        if (ci.IsNeutralCulture)
        {
            ci = System.Globalization.CultureInfo.CreateSpecificCulture(ci.TwoLetterISOLanguageName);
        }

        ri = new System.Globalization.RegionInfo(ci.LCID);

        string currencySymbol = ri.CurrencySymbol;//en-AU: $,or // AUD = ri.ISOCurrencySymbol;
        return MvcHtmlString.Create(htmlString.ToHtmlString().Replace("$", currencySymbol));
    }


You can't use a neutral culture (sr, en, fr, etc...) because there may be formatting differences in the specific cultures. For example, en-US and en-GB may format dates differently and just using en won't be able to ensure that formatting will occur correctly.

You have to use a specific culture. In your case instead of just 'sr' try 'sr-Cyrl-CS' (Serbia, Cyrillic) or 'sr-Latn-CS' (Serbia, Latin) cultures instead or perhaps another sr-specific culture name that you know should work.


Try sr-Cyrl-RS for cyrillic or sr-Latn-RS for latin.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜