将线索文化设置为中性文化

时间:2011-03-30 15:58:38

标签: asp.net-mvc

尝试使用db_ent.SaveChanges()将数据保存到数据库时出现此错误:

  

文化'sr'是一种中立的文化。它不能用于格式化和解析,因此不能设置为线程的当前文化。对此有何解决方案?

编辑:

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

3 个答案:

答案 0 :(得分:3)

我有类似的问题。用户的浏览器正在发送'en'中性文化。

所以我用它来让.Net选择最接近的'特定'文化:

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

答案 1 :(得分:1)

您不能使用中性文化(sr,en,fr等),因为特定文化中可能存在格式差异。例如,en-US和en-GB可能会以不同方式格式化日期,只使用en将无法确保格式化将正确发生。

您必须使用特定的文化。在你的情况下,而不是只是'sr'尝试'sr-Cyrl-CS'(塞尔维亚,西里尔文)或'sr-Latn-CS'(塞尔维亚,拉丁文)文化,或者你知道另一个特定文化名称应该工作

答案 2 :(得分:1)

尝试sr-Cyrl-RS用于拉丁语的西里尔语或sr-Latn-RS。