I started to work in translating a Java program which has tens of dialog classes to deal with. I wonder how is it possible to create a parameter that is understood in all of those classes, without having to declare it in each one.
Actualy are two parameters. Tried with something like:
public Locale currentLocale = new Locale("en");
public ResourceBundle text = ResourceBundle.getBundle("MessagesBundle", currentLocale);
in the launching class of the program but had no luck.
Any tip?
There are two general approaches:
ThreadLocal.Either way, you need to take a lot of caveats into account. Singletons doesn’t work well in environments with multiple classloaders/JVM’s and ThreadLocals doesn’t work well when you spawn multiple threads yourself to process the business task. You need to understand those caveats very well before continuing.
The safe approach would be to create the object only once during application’s startup in some front controller class and pass it as argument into the business/model objects whenever needed.