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.
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).
This is how I implemented the internationalization :
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);
}
}
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