如何使用LWUIT解决本地化问题

问题描述 投票:1回答:1

我用一个textField和一个命令制作了一个简单的表单,我想使用两种语言:波斯语和英语,以便我可以在运行时根据需要使用它们。

我使用ResourceEditor进行了一些翻译,并以English.resFarsi.res的名称保存,然后将它们添加到我的资源中。

现在,我在调用Localization方法时遇到问题,因为我不知道该怎么做。我将在此处发布我的代码,请纠正我。

这是我的代码:

public class Midlet extends javax.microedition.midlet.MIDlet {

    private Hashtable locale;

    public void startApp() {
        Display.init(this);

        try {
            Resources res = Resources.open("/Lang.res");    
            //Lang.res is resource file where these languages are stored
            // using resoureEditor.
            locale = res.getL10N("English.res", "en");

           /* See text below. */
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        Form main = new Form((String) locale.get("FORM"));
        main.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        Label label = new Label((String) locale.get("NAME"));
        TextField tf1 = new TextField("");
        Button button = new Button((String) locale.get("OK"));

        main.addComponent(label);
        main.addComponent(tf1);
        main.addComponent(button);

        main.addCommand(new Command((String) locale.get("BACK")) {
            public void actionPerformed(ActionEvent evt) {}
        });
        main.show();
    }
    public void pauseApp() {}
    public void destroyApp(boolean unconditional) {}
}

这是我的English.res

#Export locale from the Theme Creator
#Tue Aug 02 19:35:55 IRDT 2011
FORM=form
OK=ok
BACK=back
NAME=name

try块中的这两行是否正确?

       Resources res = Resources.open("/Lang.res");    
       locale = res.getL10N("English.res", "en");

Lang.res是使用resoureEditor存储这些语言的资源文件。

我还需要做什么?

第一行出现异常。 en是所需语言区域的名称,与res文件中的value列匹配。

我仍然不确定它是否正确,但是,第一个论点是什么?语言环境?

java java-me localization internationalization lwuit
1个回答
0
投票

我建议使用“资源编辑器”来创建L10N资源,它可以与gui同步,从而使此任务更容易实现,并且可以在代码中轻松检索。

try {
        Constants.res = Resources.open("/Lang.res");
    } catch (Exception e){
        System.err.println("can't load resource file:" + e);
    }
Hashtable h = Constants.res.getL10N("English.res","en");
© www.soinside.com 2019 - 2024. All rights reserved.