Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static ResourceBundle

I am currently making resources for an app that is using ResourceBundle. The thing is, with the current code to dispatch the resources I would need to create an instance of the resource bundle every time I need it and I can guess this is not a good idea, since I would end up loading the resources again and again.

The second solution would be to divide the bundle into many, But I would end up with bundles have only 2-3 strings and like 15 bundles.

My question is: Is there a way to simple load all the resources in a single static class and access them from there.

I made this little piece of code that seems to work for me but I doubt its quality.

public class StaticBundle
{
    private final static ResourceBundle resBundle = 
        ResourceBundle.getBundle("com.resources");
    public final static String STRING_A = resBundle.getString("KEY_A");
    public final static String STRING_B = resBundle.getString("KEY_B");
    public final static String STRING_C = resBundle.getString("KEY_C");
}

With this I can call StaticBundle.STRING_A and get the value anywhere in the project but since the bundle is initialized at the same time as the class itself... It is highly possible that the program won't have the time to load the proper local from the preferences.

Is there a good way to do this or any other possible solution?

Thank you

like image 754
AyoyeSnif Avatar asked May 12 '26 21:05

AyoyeSnif


1 Answers

If you intend to have only messages for the default locale then what you have is fine.

Alternatively you could let the caller specify which key it needs instead of having constants, like this:

public static String getMessage(String key) {
    return resBundle.getString(key);
}

If you like to support multiple locales then the usual approach is to have a Map<Locale, ResourceBundle>Map<Locale, Map<String, String> where you load the resources only once for each locale. In that case your class would have a method where the caller can specify the locale:

public static String getMessage(String key, Locale locale) {
    Map<String, String> bundle = bundles.get(locale);   // this is the map with all bundles
    if (bundle == null) {
        // load the bundle for the locale specified
        // here you would also need some logic to mark bundles that were not found so
        // to avoid continously searching bundles that are not present 

        // you could even return the message for the default locale if desirable
    }
    return bundle.get(key);
}

Edit: As correctly pointed out by @JB Nizet (thanks) ResourceBundle already stores a Map. The custom solution I provided in the source example, was about a custom mechanism similar to ResourceBundle that used a Map of Maps to load translations of keys in a property=value format, not only from files but also a database. I have incorrectly thought that we had a Map of ResourceBundle in that solution. The source example is fixed now.

like image 53
c.s. Avatar answered May 14 '26 10:05

c.s.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!