How to implement multilanguage in Java/Swing applications?
Solution 1
Well, you had to use ResourceBundle
s. But if you are setting the componet text property use instead of human readable text the text for RB.getString()
. Then if the Matisse regenerates form the bundle key will stay and localization will work. Example:
I will use this image from Matisse pages:
(source: myeclipseide.com) .
there you can see the the property text. There is value "My New Label". Instead of this you can use rb.getString("myNewLabel.my.message")
where rb
is ResourceBundle
. The only problem should be too intelligent properties editor going against you. I never work with any wysiwyg editor (personal preference, I do always UI design by hand).
Solution 2
This is how I implemented the internationalization :
- give a name to every component which has an internationalized text
- at runtime, take the container(frame, dialog, applet) ,iterate all the components and build an i18n key for every component using its name and all parent names.
- for every component type(JTextField, JLable, etc) define some keys for every internationalize field(text, label, hint, etc).
- take this i18n key and query your ResourceBundle, take the results and populate the component fields.
It works with generated code or with manual created code.
Edit: Here it is :
public void buildIds() {
buildId(true);
int count = getComponentCount();
if (count == 0) {
return;
}
for (int i = 0; i < count; i++) {
Component component = getComponent(i);
if (component instanceof AbstractComponent) {
((AbstractComponent) component).buildIds();
}
}
}
protected void buildId(boolean fireChange) {
String prevId = this.id;
String computedId;
if (getName() == null) {
computedId = ClassUtilities.shortClassName(getClass()).toLowerCase() + "_" + Long.toHexString(ID_GENERATOR.getAndIncrement());
} else {
java.util.List<Component> parents = null;
Component parent = getParent();
if (parent != null) {
StringBuilder buider = new StringBuilder(80);
parents = new ArrayList<Component>();
while (parent != null) {
if (parent.getName() != null) {
parents.add(parent);
}
parent = parent.getParent();
}
Collections.reverse(parents);
if (parents.size() > 0) {
for (Component component : parents) {
if (buider.length() > 0) {
buider.append('.');
}
buider.append(component.getName());
}
buider.append('.');
}
buider.append(name);
computedId = buider.toString().toLowerCase();
} else {
computedId = name;
}
}
this.id = computedId;
if (fireChange && prevId != null && !prevId.equals(computedId)) {
componentIdChanged(this, prevId);
}
}
user9562401
Software developer and big data architect. Expertise in Java/J2EE, Python, Hadoop, Docker, Spring MVC, SQL.
Updated on July 09, 2022Comments
-
user9562401 almost 2 years
What are the different ways of implementing multilingual support in Swing applications?
Are you using ResourceBundle with property file and implementing it in every frame? Does it work good for you? What if you use some kind of GUI editor? Is there any other way around?
At work we are using Matisse4MyEclipse and the code gets regenerated every time we save the screen, so simply using Externalize Strings won't work here. One way is to define it as custom property for each component, which is very annoying. Another way is to go over the multilanguage components and their properties again after matisse's generated code, which is a pain, too.
-
user9562401 about 15 yearsHow do you assign name/key to every component?
-
Harini about 15 yearsFor every component with a non-null name, including the current component, you will create a unique id which will be equivalent to a Java class package namespace. Example : login_dialog.userName.label=User name login_dialog.password.label=Password login_dialog.login.text=Login login_dialog.login.hint=Press login to .... and so on. This has the advantage that it will be really visible in the resource dialog in which context a userName will be used(based on its parent names) login_dialog, userName, password are component names, login_dialog is the parent of userName & password.
-
user9562401 about 15 yearsWhere do you store the info that userName key is bound to jTextfield1?
-
Harini about 15 yearsWell, in your case you don't/shouldn't store it. In my case is a little bit more complex because I use an abstraction of a UI model and by default this namespace is used to calculate a component id which is unique per application. Based on this id every component will query a I18n service to ask for internationalized text. If you just want to apply the same principle just iterate all components, create the id, query a ResourceBundle and discard the id since you don't need it.
-
user9562401 about 15 yearsWe use abstraction level for UI components, too. How do you generate unique id for each component? Is it some kind of meaningless hash?
-
Harini about 15 yearsNo, as I told you the id is generated based its name and all parents up to the root. Since I cannot post code as a comment I will post it as an answer.