简体   繁体   中英

I want to change the language of a SWT Application at Runtime. How to do that?

I'm developing an application to my software engineering class and one of the requisites is that it has 2 or more languages in the interface. I already implemented that. On the other hand, it is required that the user can change the language on its own will by choosing from a combobox or something like that.

As i'm using AWT and Netbeans, I can't edit the initComponents method of the form, as it is automatically generated. I have the option below in mind, but don't know if they would even work, so I'm asking for help.

-Edit the constructor of my class so that it looks like this:

public JFmyConstructor() {
    initComponents(); //auto-generated
    myInitMethod();
}

The problem I think i'm going to find is that I need to call the constructor one time after the interface is already running (and thus, all objects instantiated. Is there a workaround for this?

Probably my answer comes a little late (too bad you put wrong title)...

Obviously you do not have to call any constructor in order to switch language at runtime. In Java Desktop applications it is as ease as:

Locale brazilian = new Locale("pt", "BR");
Locale.setDefault(brazilian);

Of course if you want to switch it via JComboBox, you would probably do it a bit different. What I would do, I would actually create Model (Swing promotes MVC Design Pattern) that will hold data (different Locales) and I would set default Locale in correct ActionListener. It is not easy though.

Of course I would rather use native names for Locales. If you have them constructed, you can easily get native name Locale name by simply calling getDisplayName() passing itself as a parameter:

String brazilianNativeName = brazilian.getDisplayName(brazilian);

The result might be unsatisfactory for certain Locales (I believe Brazilian Portuguese is unfortunately one of them), but there is not much you can do about it (unless you use your own string).

To be honest, for just two languages, I would probably use some links instead. You can use simple JLabels to do that, just set correct font properties and color and assign common ActionListener. You can then query which one was actually clicked and create and set Locale appropriately. For homework that should be sufficient.

SWT and NetBeans is a weird combination. If you can do things the "normal" way with SWT and use Eclipse, then there's a powerful set of tools for managing localization, all built right in. See, for example, this introduction.

The best way is to use resource injection for all components (rather than getting i18n strings from a Bundle at panel construction time).

Resource Injection is available in a number of open source libraries or frameworks.

Since you use NetBeans, you should be able to generate an application based on Swing Application Framework (SAF), which already supports Resource Injection.

Otherwise, there are also libraries out there that do just that; simply google for "swing" "resource injection" and you'll find, eg Fuse library.

Edit: You should also take a look at this SO question that deals exactly with the same problem, and which has more detailed answers (including my own).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM