MULTILIZER's Translator component makes ther actual translation job for you. It provides three ways to translate the user interface. They are the build-in translating, translate events, and derived translators.
The easiest way it to use build-in translation technology of the translator component. This means that you just tell translator what kind of properties you want to translate. The translator takes care about rest. All the AWT components, most Swing, and most 3rd party components can be translated this way.
Use either Translator or BidiTranslator translator components.
The following tables describes the targets needed to translate the AWT components.
| Component | Properties | Targets | Notes |
| Button | label | *.label or Button.label | |
| Checkbox | label | *.label or Checkbox.label | |
| CheckboxMenuItem | label | *.label or CheckboxMenuItem.label | |
| Choice | item[] | *.items or Choise.items | |
| Frame | title | *.title or Frame.title | |
| Label | text | *.text or Label.text | |
| List | item[] | *.items or List.items | |
| Menu | item[] | *.label | |
| MenuBar | menu[] | *.label | |
| MenuItem | label | *.label or MenuItem.label | |
| PopupMenu | menu[] | *.label | |
| TextField | text | *.text or TextField.text | The translateTextComponents property must be true. |
The following line of code sets the targets property to cover all AWT components.
translator.setTargets("+*.text;+*.label;+*.title;+*.items");
Use either SwingTranslator or BidiSwingTranslator translator components.
The following tables describes how to translate the Swing components.
| Component | Properties | Targets | Notes |
| JComponent | toolTipText | *.toolTipText | |
| JAbstractButton | text toolTipText |
*.text *.toolTipText |
|
| JToggleButton | text toolTipText |
*.text *.toolTipText |
|
| JButton | text toolTipText |
*.text or JButton.text *.toolTipText or JButton.toolTipText |
|
| JRadioButton | text toolTipText |
*.text or JRadioButton.text *.toolTipText or JRadioButton.toolTipText |
|
| JCheckBox | text toolTipText |
*.text or JCheckBox.text *.toolTipText or JCheckBox.toolTipText |
|
| JLabel | text toolTipText |
*.text or JLabel.text *.toolTipText or JLabel.toolTipText |
|
| JTextComponent | text toolTipText |
*.text *.toolTipText |
The translateTextComponents property must be true |
| JTextField | text toolTipText |
*.text or JTextField.text *.toolTipText or JTextField.toolTipText |
The translateTextComponents property must be true |
| JComboBox | item[] | *.items or JComboBox.items | The MComboBoxModel or DefaultComboBoxModel must be used to enable the automatic translation. |
| JList | item[] | *.items or JList.items | The MListModel must be used to enable the automatic translation. |
| JPanel | title | *.title or JPanel.title | |
| JTabbedPane | label[] | *.label or JTabbedPane.label | |
| JMenuBar | menu[] | *.label | |
| JPopupMenu | menu[] | *.label | |
| JMenu | items[] | *.label | |
| JMenuItem | label | *.label or JMenuItem.label | |
| JCheckBoxMenuItem | label | *.label or JCheckBoxMenuItem.label | |
| JTable | headers | *.items or JTable.items | The MTableModel must be used to enable the automatic translation. |
| JTree | nodes | *.items | The DefaultMutableTreeNode nodes must be used to enable the automatic translation. |
| JOptionPane | whole user interface | not used | Use MOptionPane instead of JOptionPane. |
| JFileChooser | whole user interface | not used | Use MFileChooser instead of JFileChooser. |
| JColorChooser | N/A | N/A | MULTILIZER does not yet support this component. |
The following line of code sets the targets property to cover all AWT and Swing components.
translator.setTargets("+*.text;+*.label;+*.title;+*.items;+*.toolTipText");
The Translator component can translate any String or String[] typed property or any component. If you use 3rd party components add the target(s) that they require to the translator.
For example, if you have MyWidget component that has the titleText (setTitleText) and shortName (setShortName) properties, you can make them translate by adding "*. titleText" and "*.shortName" targets to the targets property of translator.
Sometimes your 3rd party components are so complex that setting just the String or String[] typed properties is not enough to translate the component. In such case you can write a translateObject event handler that translates the component.
The following example demonstates this. First you add a translate object listener. Then you write the code that translates your tabbed pane.
public class Frame1 extends JFrame
{
...
swingTranslator1.addTranslateObjectListener(new multilizer.TranslateObjectListener()
{
public void translateObject(ObjectEventObject e)
{
swingTranslator1_translateObject(e);
}
});
...
void swingTranslator1_translateObject(ObjectEventObject e)
{
if (e.getObject() instanceof MyTabbedPane)
{
MyTabbedPane pane = (MyTabbedPane)e.getObject();
for (int i = 0; i < pane.getTabCount(); i++)
{
pane.setTitleAt(i, doTranslateString(object, "label", pane.getTitleAt(i)));
}
}
}
}
If you have several 3rd party components that require writing TranslateObject events it might be a good idea to derive your own translator and write the translation code inside the translator. This way you do not have to write the event every time that you use the component. To derive a new translator override the translateObject method.
The following example demonstates this. The translateObject method first checks if the component to be translated is an instance of MyTabbedPane. If it is the method translates the titles of the panes. Finally the method call the inherited translateObject method.
public class MyTranslator extends SwingTranslator
{
protected void translateObject(Object object)
{
if (object instanceof MyTabbedPane)
{
MyTabbedPane pane = (MyTabbedPane)object;
for (int i = 0; i < pane.getTabCount(); i++)
{
pane.setTitleAt(i, doTranslateString(object, "label", pane.getTitleAt(i)));
}
}
}
super.translateObject(object);
}