public abstract class ResourceBundle extends Object
String
例如,你的程序可以加载从资源包,为当前用户的区域设置是合适的。这样,可以编写程序代码,在很大程度上是独立于用户的区域设置隔离的,如果不是全部,在资源包中的区域设置特定的信息。
这可以让你写的程序,可以:
资源束属于家庭成员共享一个基地的名字,但他们的名字也有额外的组件,确定他们的地方。例如,一个家庭的资源束的基名称可能是“MyResources”。家里应该有一个默认的资源束,只是具有相同的名称作为其家族“MyResources”将作为捆在万不得已的一个特定的地点是不支持的。家庭可以提供尽可能多的区域设置特定的成员的需要,例如德国一个名为“myresources_de”。
在一个家庭中的每个资源包都包含相同的项目,但这些项目已被转换为该资源包所表示的区域设置。例如,“MyResources”和“myresources_de”可能会有String
是用于取消操作按钮。在“MyResources”String
可能包含“取消”和“myresources_de”可能包含“abbrechen”。
如果有不同国家的资源,你可以让专业:例如,“myresources_de_ch“包含德国语言对象(德)瑞士(CH)。如果你只想修改一些专业化的资源,你可以这样做。
当你的程序需要一个区域设置特定的对象,它加载的ResourceBundle
类使用getBundle
方法:
ResourceBundle myResources = ResourceBundle.getBundle("MyResources", currentLocale);
资源捆绑包含键/值对。该键唯一地标识捆绑中的特定区域特定对象。这是一个ListResourceBundle
包含两键/值对的一个例子:
键总是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 }; } }
String
s。在这个例子中,关键是“OK”和“cancelkey”。在上面的例子中,这个值也
String
s --“确定”和“取消”,但他们不必。该值可以是任何类型的对象。
你检索使用相应的getter方法的资源束的对象。因为“OK”和“cancelkey”均为字符串,你可以使用getString
取回:
getter方法都需要密钥作为参数,如果发现返回的对象。如果对象没有找到,getter方法抛出一个button1 = new Button(myResources.getString("OkKey")); button2 = new Button(myResources.getString("CancelKey"));
MissingResourceException
。
此外getString
,ResourceBundle
还提供了一个获取字符串数组,方法getStringArray
,以及一个通用的getObject
方法其他任何类型的对象。使用getObject
时,你必须将结果适当的类型。例如:
int[] myIntegers = (int[]) myResources.getObject("intList");
java平台提供两类ResourceBundle
,ListResourceBundle
和PropertyResourceBundle
,提供了一个相当简单的方法来创建资源。当你看到在前一个简单的例子,ListResourceBundle
管理其资源为键/值对列表。PropertyResourceBundle
使用一个属性文件来管理其资源。
如果ListResourceBundle
或PropertyResourceBundle
不适合你的需要,你可以写你自己的ResourceBundle
类。你的子类必须重写方法:handleGetObject
和getKeys()
。
一个ResourceBundle
子类的实现必须是同时被多个线程使用是线程安全的。在这一类的非抽象方法的默认实现,并在直接知道具体的子类ListResourceBundle
和PropertyResourceBundle
方法是线程安全的。
ResourceBundle.Control
类提供了必要的信息,以
getBundle
工厂方法,采取
ResourceBundle.Control
实例执行束加载过程。您可以实现您自己的子类,以启用非标准的资源包格式,更改搜索策略,或定义缓存参数。指类和细节
getBundle
工厂方法的描述。
For the getBundle
factory方法不ResourceBundle.Control
实例,其资源包加载 default behavior可以安装ResourceBundleControlProvider
实现改性。任何安装商在ResourceBundle
类加载时间检测。如果供应商提供了一个ResourceBundle.Control
对于给定的基本名称,ResourceBundle.Control
将代替默认的ResourceBundle.Control
。如果有多个服务提供商安装支持相同的基名称,第一个返回ServiceLoader
将使用。
getBundle
工厂方法创建缓存的默认情况下,与工厂方法返回相同的资源束实例多次是否已缓存。
getBundle
客户可能会清除缓存,缓存的资源包管理实例使用生存时间值的一生,或不指定缓存的资源束的实例。指的是
getBundle
factory method,
clearCache
,
ResourceBundle.Control.getTimeToLive
的描述,和
ResourceBundle.Control.needsReload
详情。
ResourceBundle
类,
MyResources
例子,管理资源(一个更大的数你可能使用
Map
资源)。注意,你不需要提供一个值,如果“父级”
ResourceBundle
处理相同的值相同的键(如下面的OK)。
你不必限制自己使用一个单一的家庭// default (English language, United States) public class MyResources extends ResourceBundle { public Object handleGetObject(String key) { if (key.equals("okKey")) return "Ok"; if (key.equals("cancelKey")) return "Cancel"; return null; } public Enumeration<String> getKeys() { return Collections.enumeration(keySet()); } // Overrides handleKeySet() so that the getKeys() implementation // can rely on the keySet() value. protected Set<String> handleKeySet() { return new HashSet<String>(Arrays.asList("okKey", "cancelKey")); } } // German language public class MyResources_de extends MyResources { public Object handleGetObject(String key) { // don't need okKey, since parent level handles it. if (key.equals("cancelKey")) return "Abbrechen"; return null; } protected Set<String> handleKeySet() { return new HashSet<String>(Arrays.asList("cancelKey")); } }
ResourceBundle
s。例如,你可以有一套异常信息包,
ExceptionResources
(
ExceptionResources_fr
,
ExceptionResources_de
,…),和一个小部件,
WidgetResource
(
WidgetResources_fr
,
WidgetResources_de
,…);打破资源但是你喜欢。
ListResourceBundle
,
PropertyResourceBundle
,
MissingResourceException
Modifier and Type | Class and Description |
---|---|
static class |
ResourceBundle.Control
ResourceBundle.Control 定义回调方法被调用的方法
ResourceBundle.getBundle 厂包加载过程中。
|
Modifier and Type | Field and Description |
---|---|
protected ResourceBundle |
parent
这束的母束。
|
Constructor and Description |
---|
ResourceBundle()
唯一的构造函数。
|
Modifier and Type | Method and Description |
---|---|
static void |
clearCache()
从调用调用方的类加载程序加载已加载的缓存中的所有资源包。
|
static void |
clearCache(ClassLoader loader)
从给定的类加载程序中删除已加载的缓存中的所有资源包。
|
boolean |
containsKey(String key)
确定了
key 包含在本
ResourceBundle 或其父母管束。
|
String |
getBaseBundleName()
如果已知返回该束的基本名称,或
null 如果未知。
|
static ResourceBundle |
getBundle(String baseName)
使用指定的基名称、默认区域设置和调用方的类加载程序获取资源捆绑。
|
static ResourceBundle |
getBundle(String baseName, Locale locale)
获取使用指定的基名称和区域设置的资源包,以及调用方的类加载程序。
|
static ResourceBundle |
getBundle(String baseName, Locale locale, ClassLoader loader)
使用指定的基名称、区域设置和类加载程序获取资源包。
|
static ResourceBundle |
getBundle(String baseName, Locale targetLocale, ClassLoader loader, ResourceBundle.Control control)
使用指定的基名称、目标区域设置、类装载器和控件返回一个资源包。
|
static ResourceBundle |
getBundle(String baseName, Locale targetLocale, ResourceBundle.Control control)
使用指定的基名称、目标区域设置和控件,以及调用方的类加载程序返回资源包。
|
static ResourceBundle |
getBundle(String baseName, ResourceBundle.Control control)
使用指定的基名称、默认区域设置和指定控件返回资源包。
|
abstract Enumeration<String> |
getKeys()
返回键的枚举。
|
Locale |
getLocale()
返回此资源束的区域设置。
|
Object |
getObject(String key)
从这个资源包或它的一个父母中获取给定密钥的对象。
|
String |
getString(String key)
从这个资源包或它的一个家长的给定键中获取一个字符串。
|
String[] |
getStringArray(String key)
从这个资源包或它的父母的一个给定的密钥得到一个字符串数组。
|
protected abstract Object |
handleGetObject(String key)
从这个资源包中获取给定密钥的对象。
|
protected Set<String> |
handleKeySet()
返回一个
Set 的钥匙只有在这
ResourceBundle 。
|
Set<String> |
keySet()
返回包含在这
ResourceBundle 及其母束
Set 所有钥匙。
|
protected void |
setParent(ResourceBundle parent)
设置此捆绑的父包。
|
protected ResourceBundle parent
getObject
当这个包不包含一个特定的资源。
public String getBaseBundleName()
null
如果未知。如果不为空,那么这是价值的
baseName
参数被传递给方法时
ResourceBundle.getBundle(...)
资源包加载。
ResourceBundle.getBundle(...)
方法。
getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader)
public final String getString(String key)
(String) getObject
(key)
.
key
-给定字符串的关键
null
key
NullPointerException
MissingResourceException
-如果没有对象的给定的键可以发现
ClassCastException
-如果对象发现对于给定的关键不是一个字符串
public final String[] getStringArray(String key)
(String[]) getObject
(key)
.
key
-所需的字符串数组的关键
null
key
NullPointerException
MissingResourceException
-如果没有对象的给定的键可以发现
ClassCastException
-如果对象发现对于给定的关键不是一个字符串数组
public final Object getObject(String key)
handleGetObject
获得对象。如果不成功,与家长资源包是无效的,需要家长的
getObject
方法。如果还不成功,它抛出一个missingresourceexception。
key
-预期目标的关键
null
key
NullPointerException
MissingResourceException
-如果没有对象的给定的键可以发现
public Locale getLocale()
protected void setParent(ResourceBundle parent)
getObject
当这个包不包含一个特定的资源。
parent
这束束的母。
public static final ResourceBundle getBundle(String baseName)
getBundle(baseName, Locale.getDefault(), this.getClass().getClassLoader())
,
除了
getClassLoader()
运行与
ResourceBundle
安全权限。看到
getBundle
为一个完整的描述的搜索和实例化策略。
baseName
-资源束的基名称、完全限定的类名
null
baseName
NullPointerException
MissingResourceException
-如果指定基的名字没有资源包可以发现
public static final ResourceBundle getBundle(String baseName, ResourceBundle.Control control)
getbundle(basename,getdefault()现场,这个getclass() getclassloader(),控制),除了
getClassLoader()
运行与
ResourceBundle
安全权限。看到了一
ResourceBundle.Control
资源包加载过程的完整描述
getBundle
。
baseName
-资源束的基名称、完全限定的类名
control
-这对于资源包加载过程对信息的控制
NullPointerException
-如果
baseName
或
control
是
null
MissingResourceException
-如果指定基的名字没有资源包可以发现
IllegalArgumentException
-如果给定的
control
不正确执行(例如,
control.getCandidateLocales
返回null。)注意
control
是进行验证。
public static final ResourceBundle getBundle(String baseName, Locale locale)
getBundle(baseName, locale, this.getClass().getClassLoader())
,
除了
getClassLoader()
运行与
ResourceBundle
安全权限。看到
getBundle
为一个完整的描述的搜索和实例化策略。
baseName
-资源束的基名称、完全限定的类名
locale
-现场的资源束所需的
NullPointerException
-如果
baseName
或
locale
是
null
MissingResourceException
-如果指定基的名字没有资源包可以发现
public static final ResourceBundle getBundle(String baseName, Locale targetLocale, ResourceBundle.Control control)
getbundle(basename,targetlocale,这getclassloader()。getclass(),控制),除了
getClassLoader()
运行与
ResourceBundle
安全权限。看到了一
ResourceBundle.Control
资源包加载过程的完整描述
getBundle
。
baseName
-资源束的基名称、完全限定的类名
targetLocale
-现场的资源束所需的
control
-这对于资源包加载过程对信息的控制
locales
一
Locale
资源包
NullPointerException
-如果
baseName
,
locales
或
control
是
null
MissingResourceException
-如果没有资源束为指定的基名称的任何
locales
可以发现。
IllegalArgumentException
-如果给定的
control
不正确执行(例如,
control.getCandidateLocales
返回null。)注意
control
是进行验证。
public static ResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader)
该方法具有相同的调用getBundle(String, Locale, ClassLoader, Control)
通过ResourceBundle.Control
默认实例,除非另一个ResourceBundle.Control
设有ResourceBundleControlProvider
SPI。指的是modifying the default behavior描述。
The following describes the default behavior。
getBundle
使用基本名称,指定的地点,和默认的地区(从Locale.getDefault
)产生一个序列的candidate bundle names。如果指定的语言、脚本、国家和变体都是空的字符串,则基名称是唯一的候选包名称。否则,一个列表中的候选地点是从指定的区域设置属性值生成的(语言、脚本、国家及变种)和附加到基地名称。通常情况下,这将看起来像以下:
“_ basename + +语言+“_”+脚本+“_”+国+“_”+变种“_ basename + +语言+“_”+脚本+“_”+国“_ basename +“+ +”_”+脚本语言“_ basename + +语言+“_”+国+“_”+变种“_ basename + +语言+“_”+国“_ basename + +语言
候选捆绑名称,其中最后一个组件是一个空字符串被省略,以及下划线。例如,如果一个国家是一个空字符串,第二个和第五个候选包的名字将被省略。此外,如果脚本是一个空字符串,则省略了包含脚本的候选名称。例如,一个区域的语言“的”变“java”会产生候选人的名字叫“myresource”下面的基地。
myresource_de__javamyresource_de在的情况下,变量包含一个或多个下划线('_ '),序列的名称被截断的最后一束下划线和后面的部分是插入一个与原变种的候选人名字后产生的束。例如,对于一个语言“en”现场,“latn脚本,国家“美国”变“windows_vista”,和myresource”名“基地束,产生的候选束的名字下面的列表:
myresource_en_latn_us_windows_vistamyresource_en_latn_us_windowsmyresource_en_latn_usmyresource_en_latnmyresource_en_us_windows_vistamyresource_en_us_windowsmyresource_en_usmyresource_en
Note: For someLocale
s, the list of candidate bundle names contains extra names, or the order of bundle names is slightly modified. See the description of the default implementation ofgetCandidateLocales
for details.
getBundle
然后遍历候选束名称找到第一个可以实例化实际资源包。它使用默认的控件的getFormats
方法,产生的每个生成的名字两束的名字,第一类名称和第二属性文件的名字。对于每个候选的包名称,它试图创建一个资源包:
getBundle
创建该类的新实例并使用它作为结果的资源包。getBundle
试图使用生成的属性文件名查找资源文件。它生成一个路径名从候选束名称取代所有”。“与”/“添加字符串“字符。属性”。它试图找到一个“具有该名称的使用ClassLoader.getResource
资源”。(注意,“资源”在getResource
感无关的资源包,内容是数据容器,如一个文件。)如果它找到一个“资源”,它试图从内容创建一个新的PropertyResourceBundle
实例。如果成功,此实例将成为结果资源包。直到一个结果的资源束实例化或候选名单了束。如果没有找到匹配的资源束,默认控制的getFallbackLocale
方法被调用时,返回当前的默认区域设置。使用此区域设置了一个新的候选区域名称序列,并再次搜索,如上所述。
如果还没有结果包被发现,则单独的基本名称是抬头看的。如果仍然失败,一MissingResourceException
抛出。
Once a result resource bundle has been found, its parent chain is instantiated。如果结果捆绑已经有一个父(可能是因为它是从缓存返回)的链是完整的。
否则,getBundle
考察候选区域列表,生成结果资源束传递期间用剩下的。(例如,在最后一个组件是一个空字符串的候选包名称省略。)当它涉及到候选列表的结束时,它尝试了普通的包名称。每个候选人的名字试图捆绑实例化一个资源包(先找一个班然后一个属性文件,如上所述)。
每当它成功了,它调用之前实例化资源束的setParent
方法与新的资源包。这种情况一直持续到列表的名称已用尽,或者当前的包已经有一个非空的父。
父链完成后,返回包。
注: getBundle
缓存实例化资源束会返回相同的资源束实例多次。
注:the baseName
论点应该是完全限定的类名。然而,与早期版本的兼容性,Sun的java运行环境不验证这一点,所以它是可以通过指定一个路径名访问PropertyResourceBundle
s(用“/”)而不是完全限定的类名(使用“。”)。
提供以下类和属性文件:
z-3a75ef9f—d645-4598-8223-435891eb91a1所有文件的内容都是有效的(即非抽象子类的公共ResourceBundle
为“类”的文件,语法正确”属性”文件)。默认设置是
Locale("en", "GB")
。
下面的现场参数调用getBundle
将实例化资源束如下:
Locale("fr", "CH") | MyResources_fr_CH.class, parent MyResources_fr.properties, parent MyResources.class |
Locale("fr", "FR") | MyResources_fr.properties, parent MyResources.class |
Locale("de", "DE") | MyResources_en.properties, parent MyResources.class |
Locale("en", "US") | MyResources_en.properties, parent MyResources.class |
Locale("es", "ES") | MyResources_es_ES.class, parent MyResources.class |
文件myresources_fr_ch.properties是因为它是隐藏的myresources_fr_ch.class从未使用。同样,myresources.properties也隐藏在MyResources.class。
baseName
-资源束的基名称、完全限定的类名
locale
-现场的资源束所需的
loader
-从加载资源包中的类装载器
NullPointerException
-如果
baseName
,
locale
,或
loader
是
null
MissingResourceException
-如果指定基的名字没有资源包可以发现
public static ResourceBundle getBundle(String baseName, Locale targetLocale, ClassLoader loader, ResourceBundle.Control control)
getBundle
factory methods with no control
argument,给定的
control
指定如何定位和实例化资源束。从概念上讲,束加载过程与给定的
control
按以下步骤进行。
baseName
缓存中的资源包,targetLocale
和loader
。如果在缓存中找到所请求的资源束实例和实例的生存期间的时间,并且它的所有父实例都没有过期,则将实例返回给调用方。否则,该工厂方法将在下面的加载过程中进行。control.getFormats
方法来获得资源包格式产生束或资源名称。弦"java.class"
和"java.properties"
指定类和property-based资源束,分别。其他字符串开始的"java."
留给将来的扩展和不能用于应用程序定义的格式。其他字符串指定应用程序定义的格式。control.getCandidateLocales
方法被调用的目标区域获得候选人名单的地区,资源束搜索。control.newBundle
方法来实例化一个基础包的名称ResourceBundle
,候选区域,和格式。(请参阅下面。缓存查找到注)这一步迭代上的候选地点和格式的所有组合,直到newBundle
方法返回一个ResourceBundle
实例或迭代已经用完了所有的组合。例如,如果候选区域Locale("de", "DE")
,Locale("de")
和Locale("")
和格式"java.class"
和"java.properties"
,之后现场格式组合被用来调用control.newBundle
序列。
Locale |
format |
Locale("de", "DE") |
java.class |
Locale("de", "DE") |
java.properties |
Locale("de") |
java.class |
Locale("de") |
java.properties |
Locale("") |
java.class |
Locale("") |
java.properties |
Locale("")
),和候选区域列表仅包含Locale("")
,返回给调用者的束。如果一束已被发现是一个基础的捆绑,但候选人名单中,除了现场现场现场(“”),把束按兵不动,继续执行步骤6。如果一个包被发现,不是一个基本束,继续步骤7。control.getFallbackLocale
方法来得到一个回退区域(替代当前的目标区域)试图进一步寻找资源束。如果该方法返回一个非空的区域设置,它将成为下一个目标区域设置和加载过程从步骤3开始。否则,如果一个基包被发现并在前面的步骤5中放上,它现在返回给调用方。否则,一个missingresourceexception抛出。资源包加载过程以上时,该工厂方法查找缓存control.newBundle
方法之前调用。如果存在时间段的资源包缓存中找到已过期,工厂方法调用的control.needsReload
方法确定资源包需要重新加载。如果重装时,工厂方法调用control.newBundle
重新加载资源包。如果control.newBundle
返回null
,工厂方法提出一个虚拟的资源束,缓存作为标记不存在资源束为了避免后续请求查找的开销。这样的虚拟资源束相同的有效控制下所指定的control
。
默认情况下缓存加载的所有资源包。参考control.getTimeToLive
详情。
以下是使用默认的ResourceBundle.Control
实施束加载过程的一个例子。
条件:
foo.bar.Messages
束Locale.ITALY
Locale
Locale
:Locale.FRENCH
foo/bar/Messages_fr.properties
和foo/bar/Messages.properties
首先,getBundle
试图在以下序列资源包加载。
foo.bar.Messages_it_IT
foo/bar/Messages_it_IT.properties
foo.bar.Messages_it
foo/bar/Messages_it.properties
foo.bar.Messages
foo/bar/Messages.properties
在这一点上,getBundle
发现foo/bar/Messages.properties
,这被搁置,因为它是基束。getBundle
电话control.getFallbackLocale("foo.bar.Messages", Locale.ITALY)
返回Locale.FRENCH
。接下来,getBundle
尝试按以下顺序一捆装载。
foo.bar.Messages_fr
foo/bar/Messages_fr.properties
foo.bar.Messages
foo/bar/Messages.properties
getBundle
发现foo/bar/Messages_fr.properties
并创建一个ResourceBundle
实例。然后,getBundle
建立其母链从候选地区名单。只有foo/bar/Messages.properties
发现列表中的getBundle
创建一个ResourceBundle
实例,成为foo/bar/Messages_fr.properties
实例的父。
baseName
-资源束的基名称、完全限定的类名
targetLocale
-现场的资源束所需的
loader
-从加载资源包中的类装载器
control
-这对于资源包加载过程对信息的控制
NullPointerException
-如果
baseName
,
targetLocale
,
loader
,或
control
是
null
MissingResourceException
-如果指定基的名字没有资源包可以发现
IllegalArgumentException
-如果给定的
control
不正确执行(例如,
control.getCandidateLocales
返回null。)注意
control
是进行验证。
public static final void clearCache()
ResourceBundle.Control.getTimeToLive(String,Locale)
public static final void clearCache(ClassLoader loader)
loader
的类装载器
NullPointerException
-如果
loader
是空的
ResourceBundle.Control.getTimeToLive(String,Locale)
protected abstract Object handleGetObject(String key)
key
-预期目标的关键
null
key
NullPointerException
public abstract Enumeration<String> getKeys()
ResourceBundle
及其母束中的关键一
Enumeration
。
public boolean containsKey(String key)
key
包含在本
ResourceBundle
或其父母管束。
key
-资源
key
true
如果给定的
key
包含在本
ResourceBundle
或其母束;
false
否则。
null
key
NullPointerException
public Set<String> keySet()
ResourceBundle
及其母束
Set
所有钥匙。
ResourceBundle
及其父束包含
Set
所有钥匙。
protected Set<String> handleKeySet()
Set
的钥匙只有在这
ResourceBundle
。
默认的实现返回的getKeys
方法返回一个Set
的关键除了那些handleGetObject
方法返回null
。一旦Set
已经创建,值保持在这ResourceBundle
为了避免后续的调用产生相同的Set
。子类可以重写此方法以更快的处理。
ResourceBundle
包含
Set
的钥匙
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.