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

我使用ResourceEditor进行了一些翻译,并将其保存为English.res和Farsi.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列匹配。

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

最佳答案

我建议使用“资源编辑器”来创建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");

10-07 19:22
查看更多