Spring.net sample -> ApplicationContext and IMessageSource

来源:互联网 发布:dns网络服务器未响应 编辑:程序博客网 时间:2024/06/08 14:55

16.3. ApplicationContext and IMessageSource

译文:NcFire (牛超)

16.3.1. Introduction

The example program Spring.Examples.AppContext shows the use the application context for text localization, retrieving objects contained in ResourceSets, and applying the values of embedded resource properties to an object. The values that are retrieved are displayed in a window.

Spring.Examples.AppContext示例程序展示了应用程序上下文关于文本定位方面的使用,从资源集中获取其中的对象,并将嵌入的资源属性值应用到一个对象上。在一个窗口中显示这些被获取的值。

The application context configuration file contains an object definition with the name messageSource. of the type Spring.Context.Support.ResourceSetMessageSource which implements the interface IMessageSource. This interface provides various methods for retrieving localized resources such as text and images as described in Section 3.14.2, “Using the IMessageSource”. When creating an instance of IApplicationContext, an object with the name 'messageSource' is searched for and used as the implementation for the context's IMessageSource functionality.

应用程序上下文的配置文件包含了一个名字为messageSource的对象定义。它属于类型Spring.Context.Support.ResourceSetMessageSource,该类型实现了ImessageSource接口。这个接口提供了大量的方法用于获取本地资源比如文本或者图片,详情见章节3.14.2 “使用ImessageSource”。当创建一个IapplicationContext实例,一个拥有名字”messageSource“的对象被即检索到并且被用于上下文的ImessageSource的功能性实现。

The ResourceSetMessageSource takes a list of ResourceManagers to define the collection of culture-specific resources. The ResourceManager can be contructed in two ways. The first way is to specifying a two part string consisting of the base resource name and the containing assembly. In this example there is an embedded resource file, Images.resx in the project. The second way is to use helper factory class ResourceManagerFactoryObject that takes a resource base name and assembly name as properties. This second way of specifying a ResourceManager is useful if you would like direct access to the ResourceManager in other parts of your application. In the example program an embedded resource file, MyResource.resx and a Spanish specific resource file, MyResources.es.resx are declared in this manner. The corresponding XML fragment is shown below

ResourceSetMessageSource拥有一个用于定义相关资源信息的资源管理器列表。资源管理器可以用两种方式被构建。第一种方式是指定一个由基本资源名与包含的程序集两部分组成的字串。在本例的工程中这有一个嵌入的资源文件,Image.resx。第二种方式是使用帮助者工厂类ResourceManagerFactoryObject,这个类利用一个基本资源名与程序集名作为属性。如果你宁愿在你的应用程序的其它部分直接访问一个ResourceManager,使用第二种指定一个ResourceManager的方法是很有用的。在本例程序中的一个嵌入的资源文件,MyResource.resx和一个指定了西班牙文化的资源文件MyResources.es.resx就是以这种方式被声明的。其相关的XML片断如下:

...
        <object name="messageSource" type="Spring.Context.Support.ResourceSetMessageSource, Spring.Core">
          <property name="resourceManagers">
            <list>
              <value>Spring.Examples.AppContext.Images, Spring.Examples.AppContext</value>
              <ref object="myResourceManager"/>
            </list>
          </property>
        </object>
  
        <object name="myResourceManager" type="Spring.Objects.Factory.Config.ResourceManagerFactoryObject, Spring.Core">
          <property name="baseName">
            <value>Spring.Examples.AppContext.MyResource</value>
          </property>
          <property name="assemblyName">
            <value>Spring.Examples.AppContext</value>
          </property>    
        </object>
...

The main application creates the application context and then retrieves various resources via their key names. In the code all the key names are declared as static fields in the class Keys. The resource file Images.resx contains image data under the key name bubblechamber (aka Keys.BUBBLECHAMBER). The code Image image = ctx.GetResourceObject(Keys.BUBBLECHAMBER) as Image; is used to retrieve the image from the context. The resource files MyResource.resx contains a text resource, Hello {0} {1} under the key name HelloMessage (aka Keys.HELLO_MESSAGE) that can be used for string text formatting purposes. The example code

由主应用程序创建应用程序上下文接着通过关键名来获取大量的资源。这儿的代码所有的关键名字在类Keys中被声明为静态的字段。资源文件Image.resx在关键名bubblechamber(Keys.BUBBLECHAMBER)下包含图像数据。代码Image image = ctx.GetResourceObject(Keys.BUBBLECHAMBER) as Image 被用于从上下文中获取这幅图像。资源文件MyResource.resx在关键名HelloMessage(Keys.HELLO_MESSAGE)下包括一个文本资源,Hello{0} {1}。它可以被用于格式化字符串文本。示例代码:

string msg = ctx.GetMessage(Keys.HELLO_MESSAGE,
                            CultureInfo.CurrentCulture,
                            "Mr.", "Anderson");

retrieves the text string and replaces the placeholders in the string with the passed argument values resulting in the text, "Hello Mr. Anderson". The current culture is used to select the resource file MyResource.resx. If instead the Spanish culture is specified

获取这个文本字串并且用密码参数值替代占位符导致了文本“Hello Mr.Anderson”的产生。当前的culture被用于选择资源文件MyResource.resx。如果想替代成指定为西班牙culture如下:

CultureInfo spanishCultureInfo = new CultureInfo("es");
string esMsg = ctx.GetMessage(Keys.HELLO_MESSAGE,
                              spanishCultureInfo,
                              "Mr.", "Anderson");

Then the resource file MyResource.es.resx is used instead as in standard .NET localization. Spring is simply delegating to .NET ResourceManager to select the appropriate localized resource. The Spanish version of the resource differs from the English one in that the text under the key HelloMessage is Hola {0} {1} resulting in the text "Hola Mr. Anderson".

因此与前面不同的是资源文件MyResource.es.resx被用于标准的.NET地方化。Spring.NETResourceManager提供了简单的代理以用于选择适当的本地资源。在关键名为HelloMessage下的定义,西班牙版本的资源不同的英文版的,它被定义成Hola {0} {1} 所以生成的文本为”Hola Mr. Anderson”

As you can see in this example, the title "Mr." should not be used in the case of the spanish localization. The title can be abstracted out into a key of its own, called FemaleGreeting (aka Keys.FEMALE_GREETING). The replacement value for the message argument {0} can then be made localization aware by wrapping the key in a convenience class DefaultMessageResolvable. The code

正如你在本例中所看到的,在使用西班牙本土化的案例中头部”Mr.”不应该被使用。这个头部可以被抽象到它自己的关键名中,命名为FemaleGreeting(Keys.FEMALE_GREETING)。消息参数{0}的替代值可以使通过在一个方便的类DefaultMessageResolvable中包装关键名使本地有所发觉。代码

string[] codes = {Keys.FEMALE_GREETING};
DefaultMessageResolvable dmr = new DefaultMessageResolvable(codes, null);
 
msg = ctx.GetMessage(Keys.HELLO_MESSAGE,
                     CultureInfo.CurrentCulture,
                     dmr, "Anderson");

will assign msg the value, Hello Mrs. Anderson, since the value for the key FemaleGreeting in MyResource.resx is 'Mrs.' Similarly, the code

将会将值Hello Mrs.Anderson分配给msg,因为MyResource.resx中的关键名FemaleGreeting’Mrs.’,相似地,代码

esMsg = ctx.GetMessage(Keys.HELLO_MESSAGE,
                       spanishCultureInfo,
                       dmr, "Anderson");

will assign esMsg the value, Hola Senora Anderson, since the value for the key FemaleGreeting in MyResource.es.resx is 'Senora'.

将分配值Hola Senora Anderson esMsg,因为MyResource.es.resx中的关键名FemaleGreeting’ Senora’

Localization can also apply to object and not just strings. The .NET 1.1 framework provides the utility class ComponentResourceManager that can apply multiple resource values to object properties in a performant manner. (VS.NET 2005 makes heavy use of this class in the code it generates for winform applications.) The example program has a simple class, Person, that has an integer property Age and a string property Name. The resource file, Person.resx contains key names that follow the pattern, person.<PropertyName>. In this case it contains person.Name and person.Age. The code to assign these resource values to an object is shown below

本土化也可以将非字符串应用于对象上。.NET 1.1框架提供了优化类ComponentResourceManager,这个类能够在执行任务中应用多个资源值到多个对象的属性上(VS.NET2005很大功夫地在代码中利用这个类来生成winform应用程序。)这个例程有一个简单的类,Person,它有一个integer属性Age和一个string属性Name。资源文件,Person.resx遵循了模式person<PropertyName>包含了关键名。在这个例子中它包含了person.Nameperson.Age.用于为一个对象指定这些资源文件值的代码见下面:

Person p = new Person();
ctx.ApplyResources(p, "person", CultureInfo.CurrentUICulture);

While you could also use the Spring itself to set the properties of these objects, the configuration of simple properties using Spring will not take into account localization. It may be convenient to combine approaches and use Spring to configure the Person's object references while using IApplicationContext inside an AfterPropertiesSet callback (see IInitializingObject) to set the Person's culture aware properties.

当你也能够使用Spring本身来为这些对象设置属性,这个使用Spring的简单属性的配置文件将不会考虑本土化。当在一个AfterPropertiesSet回调(查看IinitializingOjbect)中设置Personculture感知属性时,它可能被很方便地组合方法并且使用Spring配置Person的对象引用。

 
原创粉丝点击