Resourcebundle property files




















To create the ResourceBundle , invoke the getBundle method, specifying the base name and Locale :. The getBundle method first looks for a class file that matches the base name and the Locale. If it can't find a class file, it then checks for properties files.

In the PropertiesDemo program we're backing the ResourceBundle with properties files instead of class files. When the getBundle method locates the correct properties file, it returns a PropertyResourceBundle object containing the key-value pairs from the properties file.

To retrieve the translated value from the ResourceBundle , invoke the getString method as follows:. The String returned by getString corresponds to the key specified. The String is in the proper language, provided that a properties file exists for the specified Locale.

This step is optional. When debugging your program, you might want to fetch values for all of the keys in a ResourceBundle. The getKeys method returns an Enumeration of all the keys in a ResourceBundle. You can iterate through the Enumeration and fetch each value with the getString method. The family should have a default resource bundle which simply has the same name as its family - "MyResources" - and will be used as the bundle of last resort if a specific locale is not supported.

Each resource bundle in a family contains the same items, but the items have been translated for the locale represented by that resource bundle. If you want to only modify some of the resources in the specialization, you can do so. The keys uniquely identify a locale-specific object in the bundle. In this example, the keys are "OkKey" and "CancelKey". In the above example, the values are also String s--"OK" and "Cancel"--but they don't have to be.

The values can be any type of object. You retrieve an object from resource bundle using the appropriate getter method. If the object is not found, the getter method throws a MissingResourceException. Besides getString , ResourceBundle also provides a method for getting string arrays, getStringArray , as well as a generic getObject method for any other type of object.

When using getObject , you'll have to cast the result to the appropriate type. PropertyResourceBundle uses a properties file to manage its resources. Your subclasses must override two methods: handleGetObject and getKeys.

The implementation of a ResourceBundle subclass must be thread-safe if it's simultaneously used by multiple threads. The default implementations of the non-abstract methods in this class, and the methods in the direct known concrete subclasses ListResourceBundle and PropertyResourceBundle are thread-safe. Control The ResourceBundle. Control class provides information necessary to perform the bundle loading process by the getBundle factory methods that take a ResourceBundle.

Control instance. You can implement your own subclass in order to enable non-standard resource bundle formats, change the search strategy, or define caching parameters. Refer to the descriptions of the class and the getBundle factory method for details.

For the getBundle factory methods that take no ResourceBundle. Control instance, their default behavior of resource bundle loading can be modified with installed ResourceBundleControlProvider implementations.

Any installed providers are detected at the ResourceBundle class loading time. If any of the providers provides a ResourceBundle. Control for the given base name, that ResourceBundle. Control will be used instead of the default ResourceBundle. If there is more than one service provider installed for supporting the same base name, the first one returned from ServiceLoader will be used. Cache Management Resource bundle instances created by the getBundle factory methods are cached by default, and the factory methods return the same resource bundle instance multiple times if it has been cached.

Refer to the descriptions of the getBundle factory method , clearCache , ResourceBundle. Example The following is a very simple example of a ResourceBundle subclass, MyResources , that manages two resources for a larger number of resources you would probably use a Map. Notice that you don't need to supply a value if a "parent-level" ResourceBundle handles the same key with the same value as for the okKey below.

Since: JDK1. Control ResourceBundle. Control defines a set of callback methods that are invoked by the ResourceBundle. String getBaseBundleName Returns the base name of this bundle, if known, or null if unknown. Control control Returns a resource bundle using the specified base name, target locale, class loader and control. Control control Returns a resource bundle using the specified base name, target locale and control, and the caller's class loader. Control control Returns a resource bundle using the specified base name, the default locale and the specified control.

Locale getLocale Returns the locale of this resource bundle. Object getObject String key Gets an object for the given key from this resource bundle or one of its parents. String getString String key Gets a string for the given key from this resource bundle or one of its parents. String [] getStringArray String key Gets a string array for the given key from this resource bundle or one of its parents. Methods inherited from class java. Object clone , equals , finalize , getClass , hashCode , notify , notifyAll , toString , wait , wait , wait Field Detail parent protected ResourceBundle parent The parent bundle of this bundle.

The parent bundle is searched by getObject when this bundle does not contain a particular resource. For invocation by subclass constructors, typically implicit. If not null, then this is the value of the baseName parameter that was passed to the ResourceBundle. The file without language suffix e. In case no property file is available for the language Locale passed to the ResourceBundle. The other property files with the language code suffixes contain the same keys but with values in different languages.

Thus, the danish property file could look like this:. You can also use a set of classes to contain your resources. Using classes you can use more than just string values. Like with the property files, you create a set of classes with a bundle base name and language suffixes.

Notice the contents array. It consists of a 2-dimensional array of keys and values. Thus, price and currency are keys, and the values to the right of them are the localized values. These two examples contains a price in two different currencies. Here are two examples that obtain an instance of both the default ResourceBundle and the ResourceBundle for the danish language:. Once you have obtained a ResourceBundle instance you can get localized values from it using one of the methods:.

You can also obtain a set of all keys contained in the ResourceBundle using the keySet method, like this:. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name.

Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.

Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled. Accept all cookies Customize settings.



0コメント

  • 1000 / 1000