0

ResourceBundles in java. some useful statements

Posted by sanjay on 1:20 PM


Locale userLocale = Locale.getDefault();


Sets default locale.



private static String getFormatString(String formatStr, Object[] args) {
if (args != null) {
// MessageFormat is brain dead in the fact that it won't
// convert unknown or unhandled object types into Strings.
// So we have to do it...
for (int i=0, count=args.length; i Object obj = args[i];

// don't bother with bogus or handled types...
if (obj == null || obj instanceof String ||
obj instanceof Number || obj instanceof Date)
continue;

// convert anything unknown
args[i] = obj.toString();
}

formatStr = MessageFormat.format(formatStr, args);
}

return formatStr;
}


Returns formatted string from given string and argument list


public static String getFormatString(String rscBundleName, String msgKey, Object[] args) {
String formatStr = getString(rscBundleName, msgKey);

return getFormatString(formatStr, args);
}



getsFormatted string


public static String getFormatString(String rscBundleName, String msgKey, Object arg) {

if (arg == null) {
arg = "NULL";
}
Object[] args = {arg};
return getFormatString(rscBundleName, msgKey, args);
}


Gets formatted string with one argument


public static String getString(String rscBundleName, String msgKey) {
ResourceBundle resourceBundle = (ResourceBundle)rscBundlesTable.get(rscBundleName);

if (resourceBundle == null) {
Locale locale = getLocale();

if (locale == null) {
resourceBundle = ResourceBundle.getBundle(rscBundleName);
} else {
resourceBundle = ResourceBundle.getBundle(rscBundleName, locale);
}

// Cache resource bundle
rscBundlesTable.put(rscBundleName, resourceBundle);
}
String str;
try {
str = resourceBundle.getString(msgKey);
} catch (MissingResourceException mRE) {
str = null;
}
return (str == null) ? msgKey : str;
}


Gets string with no arguments

0 Comments

Copyright © 2009 Note my web All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive.