I'm refactoring an existing Java desktop application to load internationalized UI text labels from a .properties file. When in the application lifecycle is the appropriate time to load the properties file and populate the strings in memory? The existing implementation defines each component's label as e.g.: public static final String foo = "bar";
It seems inefficient to load the text in the constructor (since it'll be shared among all instances), but doing everything in a static {...}
block doesn't seem remotely sane either. What's the best practice here?
In most applications i've seen, the properties are loaded from a file before starting to construct the main UI. These properties are loaded in a ResourceBundle which will either be
Another way to do so, without changing the code of panels, is to use some kind of "resource injection".
The following libraries perform that, among other things:
In most cases, resource injection is based on the name given to each component (this is how Swing App Framework and Guts-GUI work, for Fuse I am not sure). If components are not named properly, you also have some options to automatically name them (without changing the source code of panels). I have implemented such an approach in a Guts-GUI package, you may also want to take a look at it.
Then resource injection is normally called just before displaying your frame or dialog (that would be the only code location that have to be modified in your current source, easy if code for opening dialogs/frames has been centralized in one class and not spread all around).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With