📄 package.html.svn-base
字号:
file to identify the same message.</p><p>For example, you might prepare files in French, Spanish, and English that contain language-specific versions of the word "Hello". The French file would be named <code>Messages_fr.properties</code> and contain the following:</p><pre> hi=Bonjour</pre><p>while the Spanish and English files would be named <code>Messages_es.properties</code> and <code> Messages_en.properties</code> respectively. The corresponding message string definitions would say <code>hi=Hola</code> and <code>hi=Hello</code> in these files.</p><p>Second, place these properties files into the class path for your application, exactly as you would with class files themselves. The name actually used to load resources will look like a fully qualified Java class name (with appropriate package prefixes), so the file should be nested inside a directory structure that matches the packaging (either in an unpacked directory, or within a JAR file, as appropriate). For example, assume you place directory "foo" on your classpath, and stored the above properties files in directory "foo/com/mycompany/mypackage". (If you were using a JAR file like "foo.jar" instead, the files would be in directory "com/mycompany/mypackage" within the JAR file).<p>Third, initialize a <code>MessageResources</code> object that corresponds to the set of properties files for a particular name, within a particular package. The easiest way to do this is to initialize a variable in your main application class, like this:</p><pre> public static MessageResources messages = MessageResources.getMessageResources("com.mycompany.mypackage.Messages");</pre><p>Note that the "com.mycompany.mypackage" part of the name matches the package directory into which you placed your properties files, and "Messages" is the filename prefix for the particular family of properties files supported by this <code>MessageResources</code> instance. Depending on your development process, you might find it convenient to store all message strings for an entire application in a single properties file family, or to have several families - in Struts, for example, there is a family of properties files for each Java package.</p><p>To access a message string with a particular Locale, execute a statement like this:</p><pre> Locale locale = ... select the locale to be used ... String message = messages.getMessage(locale, "hi");</pre><p>In this case, the variable <code>message</code> will contain the message string corresponding to the key "hi", in the language that corresponds to the locale that was selected.</p><p>For an example of message formatting with replaceable parameters, assume that the message strings looked like this, instead (only the English version is shown - corresponding changes would be made in the other files):</p><pre> hi=Hello {0}</pre><p>Now, you can personalize the retrieved message like this:</p><pre> Locale locale = ... select the locale to be used ... String name = "Joe"; String message = messages.getMessage(locale, "hi", name);</pre><p>and the marker "{0}" will have been replaced by the specified name (Joe), no matter which language is in use. See the JavaDoc API documentation for the <code>java.text.MessageFormat</code> class for more advanced uses of the parameter replacement mechanism.</p><h5>Developing Your Own MessageResources Implementation</h5><p>In the above example, we were using the default <code>MessageResources</code> implementation supplied by Struts, which uses property files to store the message strings. It is also possible to create customized mechanisms to retrieve messages (such as loading them on demand from a database). The steps required are as follows:</p><ul> <li>Create a customized subclass of <code>org.apache.struts.util.MessageResources</code> that implements message lookup operations as you require.</li> <li>Create a customized subclass of <code>org.apache.struts.util.MessageResourcesFactory</code> that will create an instance of your custom <code>MessageResources</code> class when the <code>createResources</code> method is called. Note that the "config" argument to this method can be used to select families of messages in any manner appropriate to your needs - you are not required to emulate the "fully qualified Java class name" approach that is used by the standard <code>PropertyMessageResourcesFactory</code> class. </li> <li>Tell the <code>MessageResourcesFactory</code> class the name of the customized <code>MessageResourcesFactory</code> implementation to use when creating new factory instances.</li> <li>Create a new factory instance.</li> <li>Ask the new factory instance to create a <code>MessageResources</code> instance for you.</li></ul><p>A code example that illustrates this technique is:</p><pre> MessageResourcesFactory.setFactoryClass("com.mycompany.mypkg.MyFactory"); MessageResourcesFactory factory = MessageResourcesFactory.createFactory(); MessageResources resources = factory.createResources("configuration information");</pre><p>Once you have created your custom MessageResources instance, you utilize it to access message strings (with or without parameter replacement objects), exactly as we illustrated with the standard implementation in the previous section.</p><h5>Using MessageResources With Struts</h5><p>If your application uses the Struts controller servlet, you can optionally configure Struts to load an application-specific message resources instance for you, and make it available as a servlet context attribute (in JSP terms, an application-scope bean). This mechanism is managed by setting the following servlet initialization parameters in the web application deployment descriptor:</p><ul> <li><strong>application</strong> - The configuration string that will be passed to the <code>createResources()</code> method of the message resources factory, in order to identify the family of resources to be supported. If you use the standard message resources factory, this must be the base fully qualified name of the property resources files used to contain these messages, as illustrated above.</li> <li><strong>factory</strong> - Fully qualified Java class name of the <code>MessageResourcesFactory</code> to be used. By default, the standard implementation provided by Struts (<code>org.apache.struts.util.PropertyMessageResourcesFactory</code>) will be used.</li></ul><p>Struts provides several JSP custom tags that assume the existence of a <code>java.util.Locale</code> attribute in the user's session, under the key named by the constant string value of <code>Action.LOCALE_KEY</code>. Your own application logic can set this attribute at any time, or you can ask Struts to set it automatically (if not already set) based on the <code>Accept-Language</code> HTTP header included with the request. There are two mechanisms by which you request Struts to perform this service:</p><ul> <li>To have this service performed on every request submitted to the controller servlet, set the servlet initialization parameter <code>locale</code> to the value <code>true</code> in the application deployment descriptor. </li> <li>To have this service performed by a JSP page when it is accessed directly by a user, utilize a <code><form:html ... locale="true" ... /></code> tag at the top of each page.</li></ul></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -