You are not logged in.
Hi,
Internationalisation support is important for an app I'm currently developing. I know how to use ResourceBundles fine to remove hard coded strings out of the code and into individual files per language. In fact, my program currently works fine with resource bundles.
My program comprises of many classes, and many of them represent GUI components, like dialogs and panels. Each one needs access to the ResourceBundle to get the labels for the current language. My problem is that I've been bad and actually initialise a bundle for each class that needs it. This strikes me inefficient.
Two ways I can think of to get a single instance:
1. Have the ResourceBundle in the parent class, and explicitly pass the object to child classes.
2. Intialise the ResourceBundle in the parent class and make it public.
The latter approach seems bad. Even if I wrap it in a getter, I'm not sure. The former requires a lot of small modifications to all the necessary classes. I don't like the idea of all my GUI classes requiring a ResourceBundle to be passed via its constructure before it can display properly.
Does anyone else have any suggestions before I embark on the big change?
Cheers
Offline
I've never done internationalization, but from your problem description, you might consider using a singleton.
Dusty
Offline
If you want to get Fancy:
http://jakarta.apache.org/commons/resources/
Hobbes : Shouldn't we read the instructions?
Calvin : Do I look like a sissy?
Offline
Possible even fancier:
http://jakarta.apache.org/commons/sandbox/i18n/
Hobbes : Shouldn't we read the instructions?
Calvin : Do I look like a sissy?
Offline
wdemoss: I've seen Resources before, but it's really only a more elaborate ResourceBundle. It doesn't really help will the bigger picture, in making that class easily accessible to all the classes that need it.
Dusty: I've never used the singleton pattern before, but if I understand it correctly, it simply ensures that only one instance of that class is initialised at any one time. To clarify on your suggestion, do you mean that I should wrap the ResourceBundle in a singleton class, and then initialise this in every class that requires it? This makes sense to me.
Offline
You could do something like this:
public class I18nManager {
private ResourceBundle rb;
public static void getResourceBundle() {
if (rb == null) {
rb = new ResourceBundle();
}
return rb;
}
}
Then any class that wants to access it can just call I18nManager.getResourceBundle(). Since its static, you don't need to reference the argument.
The problem with this is that any class can access and possibly mutilate the ResourceBundle. Don't know if that's a concern. If it is, then you should make something like this:
public class I18nManager {
private static I18nManager man;
private ResourceBundle rb;
public static void initI18nManager(Object... args) {
man = new I18nManager(args);
}
public static void getI18nManager() {
if (man == null) {
throw new IllegalStateException();
}
return man;
}
//private constructor to ensure singleton
private I18nManager(Object... args) {
//set up the resource bundle and stuff
rb = new ResourceBundle()
}
public void getSomething() {
}
public void setSomething() {
}
//etc
}
You can manipulate the ResourceBundle inside the class without ever giving external access to it.
Dusty
Offline
Hmm... that seems like a damn fine idea. I shall try that! Thanks!
PS, for those interested in my Java articles, my lastest is an interview with Steve Northover, lead dev of SWT. Check it out here.
Offline