You are not logged in.

#1 2005-06-16 18:50:13

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Java - effecient i18n support across a large Swing app

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

#2 2005-06-16 19:03:40

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Java - effecient i18n support across a large Swing app

I've never done internationalization, but from your problem description, you might consider using a singleton.

Dusty

Offline

#3 2005-06-16 20:08:36

wdemoss
Member
From: WV - USA
Registered: 2004-01-18
Posts: 222

Re: Java - effecient i18n support across a large Swing app


Hobbes : Shouldn't we read the instructions?
Calvin : Do I look like a sissy?

Offline

#4 2005-06-16 20:24:27

wdemoss
Member
From: WV - USA
Registered: 2004-01-18
Posts: 222

Re: Java - effecient i18n support across a large Swing app


Hobbes : Shouldn't we read the instructions?
Calvin : Do I look like a sissy?

Offline

#5 2005-06-16 20:28:13

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: Java - effecient i18n support across a large Swing app

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

#6 2005-06-16 20:41:22

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Java - effecient i18n support across a large Swing app

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

#7 2005-06-16 21:08:45

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: Java - effecient i18n support across a large Swing app

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

Board footer

Powered by FluxBB