Lubos Programming Blog

Lubos Blog about Programming (mainly Java) and Computers

Generating Java Properties String retrieval class

Posted by lubosp on March 4, 2012

Resource file frequency – Source File

Retrieval Class Package Bundle name Properties file
no package, no retrieval class <source file name> + "ResourceStringManager" source file package from properties file <source file name> <source file name>.properties
package, no retrieval class <source file name> + "ResourceStringManager" package from properties file <source file name> <source file name>.properties
no package, retrieval class retrieval class source file package from properties file <source file name> <source file name>.properties
package, retrieval class retrieval class package from properties file <source file name> <source file name>.properties
Notes
  • Generated Retrieval class use bundle with the name of the source file when generate. If the same retrieval class is used for multiple source classes (row 3 and 4), the retrieval class has to be manually extended to use multiple bundles. The retrieval method has to pass the source file name and the method has to extract the bundle name from it. See how to create multiple resource files bundle below.
  • If no retrieval class is specified (row 1 and 2) the retrieval class is generated for each source file/properties file.

Resource file frequency – Project

Retrieval Class Package Bundle name Properties file
no package, no retrieval class capitalized scan name (spaces removed) + "ResourceStringManager" source file package from properties file specified properties file
package, no retrieval class capitalized scan name (spaces removed) + "ResourceStringManager" package from properties file specified properties file
no package, retrieval class retrieval class source file package from properties file specified properties file
package, retrieval class retrieval class package from properties file specified properties file
Notes
  • If no package is specified (row 1 and 3) the source file package is used, which means that Retrieval class will be created in each package.

Notes

How to create Resource file per package

Use Frequency project, externalize one package at the time and after each package modify package and retrieval class.

If "Do not use Retrieval Class" selected (default)

Uses only String retrieval method (getString(key)). This will always generate Java error if the source class definition does not contain getString(key).

How to create multiple resource files bundle

ResourceBundle.Control() controls the list of files for the ResourceBundle. You can overwrite getCandidateLocales and toBundleName. toBundleName converts locale to the "file name" and the list of locales you can control in getCandidateLocales. For example like (It works only in Java 1.6+):

 final String[] variants = new String[]{"your names"};
ResourceBundle.getBundle(baseName, locale,
            new ResourceBundle.Control() {
                public List<Locale> getCandidateLocales(String baseName, Locale locale) {

                        List<Locale> out = new ArrayList<Locale>();
                        String language = locale.getLanguage();
                        String country = locale.getCountry();

                        for (String variant : variants) {
                            out.add(new Locale(language, country, variant));
                        }
                        out.addAll(super.getCandidateLocales(baseName, locale));
                        return out;
                }

                public String toBundleName(String baseName, Locale locale) {
                        Locale l = new Locale(locale.getLanguage(), locale.getCountry());
                        return locale.getVariant() + "." + super.toBundleName(baseName, l);
                }
            });

Links

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: