如何强制SWT中的元素以英文显示?

时间:2013-04-18 19:19:20

标签: java localization swt

我是巴西人,我正在开发一个使用SWT的应用程序。但我需要SWT中的元素始终以英文显示给用户。目前他们用葡萄牙语出现,因为我的操作系统是葡萄牙语。那么无论操作系统如何,都可以强制所有元素保留在英语中吗?

2 个答案:

答案 0 :(得分:2)

大多数SWT Widget都依赖于操作系统,这意味着它们使用本机OS元素。所有非模拟小部件都是这种情况。您将无法更改这些语言(即选择与所选操作系统语言不同的语言)。

对于模拟小部件,this链接说明了如何强制使用某种语言。

答案 1 :(得分:0)

通常建议在一个应用程序中支持Internationalization and localization。看看java.util.ResourceBundle及其支持类。

例如,我的操作系统上的默认语言是英语,但使用java.util.ResourceBundle API,我可以定位葡萄牙语或任何其他客户。

代码

消息类

package com.sample;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class Messages {
    private static final String BUNDLE_NAME = "com.sample.messages"; //$NON-NLS-1$
    private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
            .getBundle(BUNDLE_NAME);
    private Messages() {
    }
    public static String getString(String key) {
        try {
            return RESOURCE_BUNDLE.getString(key);
        } catch (MissingResourceException e) {
            return '!' + key + '!';
        }
    }
}

测试类

package com.sample;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Test {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display, SWT.CLOSE);
        shell.setLayout(new GridLayout(1, false));
        shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        shell.setText(Messages.getString("Test.TITLE")); //$NON-NLS-1$
        Button button = new Button(shell, SWT.PUSH);
        button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        button.setText(Messages.getString("Test.GREETINGS")); //$NON-NLS-1$

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

message.properties文件

Test.GREETINGS=Olá Mundo \!\!
Test.TITLE=\u0644\u064A\u0644\u0629 \u0633\u0639\u064A\u062F\u0629

输出

enter image description here

注意 - 标题为阿拉伯语,按钮文字为葡萄牙语。

相关问题