下面列出了java.util.ResourceBundle#getKeys ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Map<String, String> getMessages(Locale locale) {
Map<String, String> map = new HashMap<String, String>();
for (String b : bundles) {
try {
ResourceBundle bundle = ResourceBundle.getBundle(b, locale);
Enumeration<String> keys = bundle.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
if (!bundle.containsKey(key))
continue;
String val = bundle.getString(key);
if (val != null && !val.isEmpty())
map.put(key, bundle.getString(key));
}
} catch (Throwable t) {
}
}
return map;
}
@Test
public void testAllKeysPresentInPropertiesFiles() {
Class<? extends Enum<?>> c = getFormatsClass();
final String path = c.getName().replaceAll("\\.", "/");
for (final String language : new String[] { "fr" } ) {
ResourceBundle bundle =
ResourceBundle.getBundle("assets/" + path, new Locale(language), c.getClassLoader());
for (Localizable message : getValues()) {
final String messageKey = message.toString();
boolean keyPresent = false;
for (final Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements();) {
keyPresent |= messageKey.equals(keys.nextElement());
}
Assert.assertTrue("missing key \"" + message.toString() + "\" for language " + language,
keyPresent);
}
Assert.assertEquals(language, bundle.getLocale().getLanguage());
}
}
private static Map<String, String> getList(
ResourceBundle rs, Boolean getCountryList) {
char beginChar = 'a';
char endChar = 'z';
if (getCountryList) {
beginChar = 'A';
endChar = 'Z';
}
Map<String, String> hm = new HashMap<String, String>();
Enumeration<String> keys = rs.getKeys();
while (keys.hasMoreElements()) {
String s = keys.nextElement();
if (s.length() == 2 &&
s.charAt(0) >= beginChar && s.charAt(0) <= endChar) {
hm.put(s, rs.getString(s));
}
}
return hm;
}
@Test
public void testAllPropertiesCorrespondToKeys() {
final String path = LocalizedFormats.class.getName().replaceAll("\\.", "/");
for (final String language : new String[] { "fr" } ) {
ResourceBundle bundle =
ResourceBundle.getBundle("assets/" + path, new Locale(language));
for (final Enumeration<String> keys = bundle.getKeys(); keys.hasMoreElements();) {
final String propertyKey = keys.nextElement();
try {
Assert.assertNotNull(LocalizedFormats.valueOf(propertyKey));
} catch (IllegalArgumentException iae) {
Assert.fail("unknown key \"" + propertyKey + "\" in language " + language);
}
}
Assert.assertEquals(language, bundle.getLocale().getLanguage());
}
}
private List<String> getTipsAndTricks() {
if (tipsAndTricks == null) {
// Need to load them in
tipsAndTricks = new ArrayList<String>();
ResourceBundle rb = Constant.messages.getMessageBundle(PREFIX);
Enumeration<String> enm = rb.getKeys();
while (enm.hasMoreElements()) {
String key = enm.nextElement();
if (key.startsWith(TIPS_PREFIX)) {
tipsAndTricks.add(/*Constant.messages.getString(key)*/ rb.getString(key));
}
}
if (tipsAndTricks.size() == 0) {
this.getMenuTipsAndTricks().setEnabled(false);
}
}
return this.tipsAndTricks;
}
/**
* Returns a Map of the known resources for the given locale.
*/
private Map<String, Object> getResourceCache(Locale l) {
Map<String, Object> values = resourceCache.get(l);
if (values == null) {
values = new TextAndMnemonicHashMap();
for (int i=resourceBundles.size()-1; i >= 0; i--) {
String bundleName = resourceBundles.get(i);
try {
Control c = CoreResourceBundleControl.getRBControlInstance(bundleName);
ResourceBundle b;
if (c != null) {
b = ResourceBundle.getBundle(bundleName, l, c);
} else {
b = ResourceBundle.getBundle(bundleName, l);
}
Enumeration keys = b.getKeys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
if (values.get(key) == null) {
Object value = b.getObject(key);
values.put(key, value);
}
}
} catch( MissingResourceException mre ) {
// Keep looking
}
}
resourceCache.put(l, values);
}
return values;
}
private Map<String, String> convertResourceBundleToMap(ResourceBundle resource) {
Map<String, String> map = new HashMap<String, String>();
Enumeration<String> keys = resource.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
map.put(key, resource.getString(key));
}
return map;
}
private Map<String, String> convertResourceBundleToMap(ResourceBundle resource) {
Map<String, String> map = new HashMap<String, String>();
Enumeration<String> keys = resource.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
map.put(key, resource.getString(key));
}
return map;
}
private Map<String, String> convertResourceBundleToClientMessagesMap(ResourceBundle resourceBundle) {
Map<String, String> map = new HashMap<String, String>();
Enumeration<String> keys = resourceBundle.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
// only load the messages that are specifically used by the client code for performance reasons
// strip off the _rave_client. part of the key
if (key.startsWith(CLIENT_MESSAGE_IDENTIFIER)) {
map.put(key.replaceFirst(CLIENT_MESSAGE_IDENTIFIER, ""), StringEscapeUtils.escapeEcmaScript(resourceBundle.getString(key)));
}
}
return map;
}
/**
* Returns a Map of the known resources for the given locale.
*/
private Map<String, Object> getResourceCache(Locale l) {
Map<String, Object> values = resourceCache.get(l);
if (values == null) {
values = new TextAndMnemonicHashMap();
for (int i=resourceBundles.size()-1; i >= 0; i--) {
String bundleName = resourceBundles.get(i);
try {
Control c = CoreResourceBundleControl.getRBControlInstance(bundleName);
ResourceBundle b;
if (c != null) {
b = ResourceBundle.getBundle(bundleName, l, c);
} else {
b = ResourceBundle.getBundle(bundleName, l);
}
Enumeration keys = b.getKeys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
if (values.get(key) == null) {
Object value = b.getObject(key);
values.put(key, value);
}
}
} catch( MissingResourceException mre ) {
// Keep looking
}
}
resourceCache.put(l, values);
}
return values;
}
/**
* Construct a JSONObject from a ResourceBundle.
*
* @param baseName
* The ResourceBundle base name.
* @param locale
* The Locale to load the ResourceBundle for.
* @throws JSONException
* If any JSONExceptions are detected.
*/
public JSONObject(String baseName, Locale locale) throws JSONException {
this();
ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale,
Thread.currentThread().getContextClassLoader());
// Iterate through the keys in the bundle.
Enumeration<String> keys = bundle.getKeys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (key != null) {
// Go through the path, ensuring that there is a nested JSONObject for each
// segment except the last. Add the value using the last segment's name into
// the deepest nested JSONObject.
String[] path = ((String) key).split("\\.");
int last = path.length - 1;
JSONObject target = this;
for (int i = 0; i < last; i += 1) {
String segment = path[i];
JSONObject nextTarget = target.optJSONObject(segment);
if (nextTarget == null) {
nextTarget = new JSONObject();
target.put(segment, nextTarget);
}
target = nextTarget;
}
target.put(path[last], bundle.getString((String) key));
}
}
}
/**
* Returns a Map of the known resources for the given locale.
*/
private Map<String, Object> getResourceCache(Locale l) {
Map<String, Object> values = resourceCache.get(l);
if (values == null) {
values = new TextAndMnemonicHashMap();
for (int i=resourceBundles.size()-1; i >= 0; i--) {
String bundleName = resourceBundles.get(i);
try {
ResourceBundle b;
if (isDesktopResourceBundle(bundleName)) {
// load resource bundle from java.desktop module
b = ResourceBundle.getBundle(bundleName, l, UIDefaults.class.getModule());
} else {
b = ResourceBundle.getBundle(bundleName, l, ClassLoader.getSystemClassLoader());
}
Enumeration<String> keys = b.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
if (values.get(key) == null) {
Object value = b.getObject(key);
values.put(key, value);
}
}
} catch( MissingResourceException mre ) {
// Keep looking
}
}
resourceCache.put(l, values);
}
return values;
}
/**
* Implementation of ResourceBundle.getKeys.
*/
@Override
public Enumeration<String> getKeys() {
ResourceBundle parentBundle = this.parent;
return new ResourceBundleEnumeration(handleKeySet(),
(parentBundle != null) ? parentBundle.getKeys() : null);
}
/**
* Construct a JSONObject from a ResourceBundle.
*
* @param baseName The ResourceBundle base name.
* @param locale The Locale to load the ResourceBundle for.
* @throws JSONException If any JSONExceptions are detected.
*/
public JSONObject(String baseName, Locale locale) throws JSONException {
this();
ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale,
Thread.currentThread().getContextClassLoader());
// Iterate through the keys in the bundle.
Enumeration<String> keys = bundle.getKeys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (key != null) {
// Go through the path, ensuring that there is a nested JSONObject for each
// segment except the last. Add the value using the last segment's name into
// the deepest nested JSONObject.
String[] path = ((String) key).split("\\.");
int last = path.length - 1;
JSONObject target = this;
for (int i = 0; i < last; i += 1) {
String segment = path[i];
JSONObject nextTarget = target.optJSONObject(segment);
if (nextTarget == null) {
nextTarget = new JSONObject();
target.put(segment, nextTarget);
}
target = nextTarget;
}
target.put(path[last], bundle.getString((String) key));
}
}
}
/**
* 根据Locale加载控件文本
*
* @param locale
*/
public static void loadLanguage(Locale locale) {
ResourceBundle resourceBundle = ResourceBundle.getBundle("config/language/language", locale);
Enumeration<String> keys = resourceBundle.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
if (LANGUAGE.get(key) == null) {
LANGUAGE.put(key, new SimpleStringProperty(resourceBundle.getString(key)));
} else {
LANGUAGE.get(key).set(resourceBundle.getString(key));
}
}
}
public static void dumpResourceBundle(String pathName, ResourceBundle bundle,
PrintStream out) throws Exception {
Enumeration keys = bundle.getKeys();
while(keys.hasMoreElements()) {
String key = (String)(keys.nextElement());
dumpResource(pathName + "/" + key, bundle.getObject(key), out);
}
}
/**
* Implementation of ResourceBundle.getKeys.
*/
@Override
public Enumeration<String> getKeys() {
ResourceBundle parentBundle = this.parent;
return new ResourceBundleEnumeration(handleKeySet(),
(parentBundle != null) ? parentBundle.getKeys() : null);
}
public static void dumpResourceBundle(String pathName, ResourceBundle bundle,
PrintStream out) throws Exception {
Enumeration keys = bundle.getKeys();
while(keys.hasMoreElements()) {
String key = (String)(keys.nextElement());
dumpResource(pathName + "/" + key, bundle.getObject(key), out);
}
}
/**
* Construct a JSONObject from a ResourceBundle.
*
* @param baseName
* The ResourceBundle base name.
* @param locale
* The Locale to load the ResourceBundle for.
* @throws JSONException
* If any JSONExceptions are detected.
*/
public JSONObject(String baseName, Locale locale) throws JSONException {
this();
ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale,
Thread.currentThread().getContextClassLoader());
// Iterate through the keys in the bundle.
Enumeration<String> keys = bundle.getKeys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (key != null) {
// Go through the path, ensuring that there is a nested
// JSONObject for each
// segment except the last. Add the value using the last
// segment's name into
// the deepest nested JSONObject.
String[] path = ((String) key).split("\\.");
int last = path.length - 1;
JSONObject target = this;
for (int i = 0; i < last; i += 1) {
String segment = path[i];
JSONObject nextTarget = target.optJSONObject(segment);
if (nextTarget == null) {
nextTarget = new JSONObject();
target.put(segment, nextTarget);
}
target = nextTarget;
}
target.put(path[last], bundle.getString((String) key));
}
}
}
protected Set<String> getReservedWords() {
final Set<String> reservedWords = new HashSet<String>();
final ResourceBundle bundle = ResourceBundle.getBundle(this.getClass().getPackage().getName() + ".reserved_word");
final Enumeration<String> keys = bundle.getKeys();
while (keys.hasMoreElements()) {
reservedWords.add(keys.nextElement().toUpperCase());
}
return reservedWords;
}