在 Swing 应用程序中实现多语言支持有哪些不同的方法?

您是否将 ResourceBundle 与属性文件一起使用并在每个框架中实现它?这对你有用吗?如果您使用某种 GUI 编辑器怎么办?还有其他办法吗?

在工作中,我们使用 Matisse4MyEclipse,每次保存屏幕时都会重新生成代码,因此简单地使用Externalize Strings 在这里不起作用。一种方法是将其定义为每个组件的自定义属性,这非常烦人。另一种方法是在 Matisse 生成代码后再次检查多语言组件及其属性,这也很痛苦。

有帮助吗?

解决方案

那么,你只好用ResourceBundles。但是,如果你设置文本及部件使用属性,而不是人类可读的文本的文本RB.getString()。这时如果马蒂斯再生形成束键将保持和定位将工作。例如:

我将使用这个形象从马蒂斯页面:点击 “插图” 结果 <子>(来源: myeclipseide.com

那里你可以看到属性的文本。有值“我的新标签”。取而代之的是,其中rb.getString("myNewLabel.my.message")rb可以使用ResourceBundle。唯一的问题应该是太聪明的属性编辑器对你不利。我从来没有与任何所见即所得的编辑工作(个人喜好,我的手一直UI设计)。

其他提示

这就是我实现国际化的方式:

  • 为每个具有国际化文本的组件命名
  • 在运行时,获取容器(框架、对话框、小程序),迭代所有组件并使用其名称和所有父级名称为每个组件构建一个 i18n 键。
  • 为每个组件类型(JTextField、JLable 等)为每个国际化字段(文本、标签、提示等)定义一些键。
  • 使用此 i18n 键并查询您的 ResourceBundle,获取结果并填充组件字段。

它适用于生成的代码或手动创建的代码。

编辑:这里是 :

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);
    }
}

我不知道除了ResourceBundle的任何方式。

你为什么老是再生代码?我可以想象,这将是一次罚款你有一个页面开始,但之后,它会是大可不必。听起来像代码生成是你真正的问题。它不保存你任何东西。

你尝试撰写页面了几个部分组成?我能想象一个共同的页眉,页脚和菜单,将不必改变所有的时间。这可能是一个设计问题。

Spring对国际化非常好的支持。也许它可以帮助你在这里。

使用的ResourceBundle并设置语言本地:

以下是在Java代码

私有静态地图把ResourceBundle;

private static ResourceBundle loadBundle(String language) {
    if (resourceBundles == null) {
        resourceBundles = new HashMap<String, ResourceBundle>();
    }

    ResourceBundle currentBundle = resourceBundles.get(language);

    if (currentBundle == null) {

        Locale locale = new Locale(language);
        currentBundle = ResourceBundle.getBundle("i18n.Messages", locale);
        resourceBundles.put(language, currentBundle);
    }

    return currentBundle;
}

public static String messageForKey(String key, String language) {
    ResourceBundle currentBundle = loadBundle(language);
    return currentBundle.getString(key);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top