如何在Java中实现国际化 [英] How to implement internationalization in java

查看:174
本文介绍了如何在Java中实现国际化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Info的类,并且其中有一堆static String变量.

I have a class called Info, and i have a bunch of static String variables described in it.

public class Info{

    public static stringOne= "Hello";
    public static stringTwo = "world";

}

,我希望从其他类中以Info.stringTwo的形式访问这些变量.

and i'm hoping to access these variables as Info.stringTwo from other classes.

1.)我需要知道这是否是我在这里应用的java-Internationalization吗? (我拥有将在此类分配的应用程序中显示的所有消息.而且,我也希望该应用程序也支持不同的语言)

1.) I need to know if this is java-Internationalization that i have applied here ? (I have all the messages that i will display in the application assigned in this class. And, i am hoping to have different languages support to the app as well)

推荐答案

看看文档中的复制粘贴:

当程序需要特定于语言环境的对象时,它将使用getBundle方法加载ResourceBundle类:

When your program needs a locale-specific object, it loads the ResourceBundle class using the getBundle method:

 ResourceBundle myResources =
 ResourceBundle.getBundle("MyResources", currentLocale);

资源束包含键/值对.这些键唯一地标识捆绑软件中特定于语言环境的对象.

Resource bundles contain key/value pairs. The keys uniquely identify a locale-specific object in the bundle.

这是一个包含两个键/值对的ListResourceBundle的示例:

Here's an example of a ListResourceBundle that contains two key/value pairs:

 public class MyResources extends ListResourceBundle {
     protected Object[][] getContents() {
         return new Object[][] {
             // LOCALIZE THE SECOND STRING OF EACH ARRAY (e.g., "OK")
             {"OkKey", "OK"},
             {"CancelKey", "Cancel"},
             // END OF MATERIAL TO LOCALIZE
        };
     }
 }

键始终是字符串.在此示例中,键为"OkKey"和"CancelKey".在上面的示例中,值也是Strings("OK"和"Cancel"),但不一定必须如此.值可以是任何类型的对象. 您可以使用适当的getter方法从资源包中检索对象.因为"OkKey"和"CancelKey"都是字符串,所以您将使用getString来检索它们:

Keys are always Strings. In this example, the keys are "OkKey" and "CancelKey". In the above example, the values are also Strings--"OK" and "Cancel"--but they don't have to be. The values can be any type of object. You retrieve an object from resource bundle using the appropriate getter method. Because "OkKey" and "CancelKey" are both strings, you would use getString to retrieve them:

button1 = new Button(myResources.getString("OkKey"));
button2 = new Button(myResources.getString("CancelKey"));

这篇关于如何在Java中实现国际化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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