Swing国际化-如何在运行时更新语言 [英] Swing Internationalization - How to update language at runtime

查看:116
本文介绍了Swing国际化-如何在运行时更新语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照 Google的Window Builder Pro扩展指南进行了操作国际化我的应用程序.现在,将在标签中显示的字符串存储在外部字符串"向导创建的属性文件中(我使用了经典的日食消息文件).

I've followed Googles howto for its Window Builder Pro extension to internationalize my app. Strings that will be shown in labels are now stored in property-files that were created by the "Externalize Strings" wizard (I've used classic eclipse message files).

在我的initialize方法中,所有我的标签都已初始化,其文本设置如下:

Inside my initialize method, all my lables are initialized and their text is set like this:

JLabel lblLanguage = new JLabel(Messages.getString("App.lblLanguage.text")); //$NON-NLS-1$

我已经在包含GUI的类App中创建了一个枚举类型:

I have created an enum type inside my class App which holds the GUI:

private enum Lang { German(Locale.GERMAN), English(Locale.ENGLISH);

    private Locale loc;
    Lang (Locale l) {
        loc = l;
    }

    Locale getLocale() {
        return loc;
    }
}

将使用组合框设置语言,该组合框使用枚举类型Lang来显示可用的语言:

The language will be set with a combobox that uses the enum type Lang to show the languages available:

    JComboBox cboLanguage = new JComboBox();
    cboLanguage.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox)e.getSource();
            Lang l = (Lang)cb.getSelectedItem();
            // TODO: update language
        }
    });
    cboLanguage.setModel(new DefaultComboBoxModel(Lang.values()));


我发现了许多其他的howto和教程,它们涵盖了swing应用程序的国际化,但是都没有涉及如何更新所有标签(以及可能包含文本的其他控件).因此,SO上有一个此答案,如果链接没有中断,这可能会有所帮助.
因为我是Java GUI编程的新手,所以我现在真的不知道该怎么办,这是我的问题:


I've found many other howtos and tutorials covering the internationalization of swing applications but none of them covers how to update all labels (and possible other controls with text in them). There's this answer here on SO that might have been helpful if the links weren't dead.
Because I'm kind of new to GUI programming in java, I don't really know what to do now and here are my questions:

  • 如果设置了新语言,最好的方法是在运行时更改所有控件的语言?
  • 可以(建议)将所有控件声明为App类的私有成员,以让方法更新其text属性(-> updateLanguage方法可以做到这一点)
  • What is the best way, to change the language of all controls at runtime if a new language is set?
  • Is it ok (recommended) to declare all controls as private members of my App class to have a method update their text property (-> an updateLanguage method would do that)

推荐答案

我通过扩展JLabel并覆盖getText以返回对语言选择的评估来解决此问题.

I solve this by extending JLabel and overwriting getText to return the evaluation of the language selection.

您还需要一些发布/订阅机制来告知"标签语言已更改.

You will also need some pub/sub mechanism to 'tell' your labels that the language has changed.

这里我正在使用 Guava事件总线

import com.google.common.eventbus.EventBus;

public class EventBusHolder {

    private static EventBus bus = new EventBus();

    public static EventBus get() {
        return bus;
    }
}

当用户更改语言时,您将触发事件:

When the user change the language you fire the event:

JComboBox cboLanguage = new JComboBox();
cboLanguage.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        JComboBox cb = (JComboBox)e.getSource();
        Lang l = (Lang)cb.getSelectedItem();
        // this is the event fire
        ChangeEvent event = getChangeEvent(l);
        EventBusHolder.get().post(event);
    }
});

要使用的标签组件:

public class MyLabel extends JLabel {

    private static final long serialVersionUID = 1L;

    public MyLabel (String key) {
        super(key);
        // register this in event bus
        EventBusHolder.get().register(this);
    }

    @Override
    public String getText() {
        return Messages.getString(super.getText());
    }

    @Subscribe 
    public void recordCustomerChange(ChangeEvent e) {
        revalidate();
    }

}

当您实例化Label时,必须传递密钥进行翻译:

And when you instanciate the Label you must pass the key for translation:

JLabel lbl = new JLabel("App.lblLanguage.text");

番石榴事件总线上的更多用法示例

这篇关于Swing国际化-如何在运行时更新语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆