4

我想用西班牙语显示模型错误消息,我已经在资源文件中定义了这些字符串。我使用 razor 语法对页面上的其他字符串执行了相同的操作,但未选择 ViewModel 注释中的字符串。

它实际上是在选择默认值 - 英语。所以我的猜测是可能没有检测到语言/文化,但页面上的其他字符串以西班牙语显示

//Spanish: El campo {0} se requiere
//English: The {0} field is required  <--- this comes out always irrespective of set language

[Required(ErrorMessageResourceName = "ErrorMessage_Required", 
     ErrorMessageResourceType = typeof(GlobalResources.Resources))]
[Display(Name = "CardNumber", ResourceType = typeof(GlobalResources.Resources) )]
public string CardNumber { get; set; }

我在控制器中设置语言

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    base.OnActionExecuting(filterContext);

    HttpCookie cookie = Request.Cookies["lang"];

    string lang = cookie != null ? cookie.Value : "en-US";
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
}

如何将文化设置扩展到 ViewModel?

更新 一个类似的帖子:MVC3 全球化:在模型绑定之前需要全局过滤器

更新 在我的浏览器设置中更改首选语言使其工作。这意味着模型属性正在使用不受System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);. 有没有办法做到这一点?- 仍在搜索中....

更新:将代码移动到Application_AcquireRequestState似乎可以解决它。

protected void Application_AcquireRequestState()
{
    HttpCookie cookie = Request.Cookies["lang"];

    string lang = cookie != null ? cookie.Value : "en-US";
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
}

我得到的解释,也在这个问题中发布的链接中找到,模型利用在控制器覆盖方法中设置的区域性为时已晚,因为在调用方法之前已经发生了绑定。这个链接很有帮助

4

2 回答 2

3

尝试使用下面的类

public sealed class LanguageManager
{
    /// <summary>
    /// Default CultureInfo
    /// </summary>
    public static readonly CultureInfo DefaultCulture = new CultureInfo("en-US");

    /// <summary>
    /// Available CultureInfo that according resources can be found
    /// </summary>
    public static readonly CultureInfo[] AvailableCultures;

    static LanguageManager()
    {
        List<string> availableResources = new List<string>();
        string resourcespath = Path.Combine(System.Web.HttpRuntime.AppDomainAppPath, "App_GlobalResources");
        DirectoryInfo dirInfo = new DirectoryInfo(resourcespath);
        foreach (FileInfo fi in dirInfo.GetFiles("*.*.resx", SearchOption.AllDirectories))
        {
            //Take the cultureName from resx filename, will be smt like en-US
            string cultureName = Path.GetFileNameWithoutExtension(fi.Name); //get rid of .resx
            if (cultureName.LastIndexOf(".") == cultureName.Length - 1)
                continue; //doesnt accept format FileName..resx
            cultureName = cultureName.Substring(cultureName.LastIndexOf(".") + 1);
            availableResources.Add(cultureName);
        }

        List<CultureInfo> result = new List<CultureInfo>();
        foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
        {
            //If language file can be found
            if (availableResources.Contains(culture.ToString()))
            {
                result.Add(culture);
            }
        }

        AvailableCultures = result.ToArray();

        CurrentCulture = DefaultCulture;
        if (!result.Contains(DefaultCulture) && result.Count > 0)
        {
            CurrentCulture = result[0];
        }
    }

    /// <summary>
    /// Current selected culture
    /// </summary>
    public static CultureInfo CurrentCulture
    {
        get { return Thread.CurrentThread.CurrentCulture; }
        set
        {
            Thread.CurrentThread.CurrentUICulture = value;
            Thread.CurrentThread.CurrentCulture = value;
        }
    }
}

最后设置如下文化。

LanguageManager.CurrentCulture = new CultureInfo("Your culture");

现在覆盖文化

于 2013-04-24T19:33:21.687 回答
1

确保同时 CurrentCulture设置和CurrentUICulture。我怀疑CurrentCulture实际上是资源文件正在查看的内容:

 //I've taken off System.Threading, add a using to it (aids readability)

 Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
 Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(lang);
于 2013-04-24T19:28:24.707 回答