根据区域性对数据注释错误消息进行本地化 [英] Localize dataannotation error messages as per culture

查看:64
本文介绍了根据区域性对数据注释错误消息进行本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF应用程序中,我具有如下定义的属性

In my WPF appliation, I have property defined as following

[Required(AllowEmptyStrings =false, ErrorMessageResourceName ="Msg1", ErrorMessageResourceType =typeof(*<AssemblyName>*.Resources.*<ResourceFileName>*))]
public string Name
{
    get
    {
        return _name;
    }

    set
    {
        if (_name == value)
        {
            return;
        }
        _name = value;
    }
}

我在单独的程序集中定义了错误消息,该程序集具有用于不同区域性的资源文件,例如Resources.resx,Resources.en-GB.Resx,Resources.fr-FR.Resx,Resources.en-US.Resx等.

I have my error messages defined in separate assembly which has resource file for different cultures e.g. Resources.resx, Resources.en-GB.Resx, Resources.fr-FR.Resx, Resources.en-US.Resx, etc.

使用上面的代码,我能够从我的附属程序集中的默认资源文件中检索错误消息,但是我看不到任何从区域性特定资源文件中找到字符串资源的规定.我的意思是,如果将CurrentUICluture设置为english(英国),那么我想从文件"Resources.en-GB.Resx"而不是默认文件(即Resources.Resx)中检索资源值.

With above code in place, I'm able to retrieve the error message from default resource file in my satellite assembly but I don't see any provision to find the string resource from culture specific resource file. What I mean is if my CurrentUICluture is set as english(United Kingdom) then I want to retrieve the resource value from the file "Resources.en-GB.Resx" instead of the default file (i.e. Resources.Resx).

我在必填"属性定义中看不到任何传递区域性信息的方法.另外,我已经测试了它并不是根据当前的区域性集固有地查看区域性特定资源文件.

I don't see any way to pass the culture info in the Required attribute definition. Also, I have tested that it is not inherently look into the culture specific resource file based on the current culture set.

我想要的是一种使资源检索机制具有文化意识的方法.

What I want is some way to make the resource retrieval mechanism culture aware.

谢谢

推荐答案

最后,我从在我的应用程序中,我通过在app.xaml.cs文件中放置以下代码来设置启动应用程序时的区域性.

In my app, I was setting the culture at the time of starting the application by putting following code in app.xaml.cs file.

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        InitializeCultures();
    }

    private static void InitializeCultures()
    {
        var culture = ConfigurationManager.AppSettings.Get("Culture");
        if (!String.IsNullOrEmpty(culture))
        {
            Thread.CurrentThread.CurrentCulture =  new CultureInfo(culture);
        }

        var UICulture = ConfigurationManager.AppSettings.Get("UICulture");
        if (!String.IsNullOrEmpty(UICulture))
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(UICulture);
        }
    }

但是,即使我最初设置了区域性,但并不是所有线程最初都将使用我设置的相同区域性.因此,正在为我读取资源值的线程仍在使用默认区域性,这导致总是从默认区域性资源文件而不是区域性特定资源文件中读取资源字符串.

However problem is even if I set the Culture initially, not all threads will use the same culture set by me initially. So the thread which was reading the resource value for me was still using the default culture and this resulted in reading of resource string always from default culture resource file rather then the culture specific resource file.

因此,诀窍是将应用程序中的所有线程设置为使用最初设置的相同区域性.有两个属性

So the trick was to set all the threads in my app to use the same culture which I set initially. There are two properties for this

  1. CultureInfo.DefaultThreadCurrentCulture
  2. CultureInfo.DefaultThreadCurrentUICulture

最初将必需的区域性值设置为这两个属性将确保应用程序中使用的所有后续线程将具有与用户设置的相同区域性.这两个属性在当前应用程序域中的所有线程上设置区域性.

Setting required culture value to these two properties initially will ensure that all the subsequent threads used in the application will have the same culture as set by user. These two properties sets the culture on all the threads in current app domain.

正确设置区域性后,读取资源值将固有地成为文化意识,并从区域性特定的资源文件中读取资源值.

Once culture is set properly, reading of resource values inherently becomes culture aware and reads resource values from culture specific resource files.

因此,以下是InitializeCulture方法的更新版本:

So following is the updated version of InitializeCulture method :

    private static void InitializeCultures()
    {
        var culture = ConfigurationManager.AppSettings.Get("Culture");
        if (!String.IsNullOrEmpty(culture))
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.DefaultThreadCurrentCulture =  new CultureInfo(culture);
        }

        var UICulture = ConfigurationManager.AppSettings.Get("UICulture");
        if (!String.IsNullOrEmpty(UICulture))
        {
            Thread.CurrentThread.CurrentUICulture = CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(UICulture);
        }
    }

P.S.在Web应用程序中使用此修复程序时要小心.请在原始链接上阅读它.

P.S. There is a word of caution of using this fix in web application. Pls read about it on original link.

希望这对您有帮助...

Hope this helps...

这篇关于根据区域性对数据注释错误消息进行本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆