下面列出了java.util.ResourceBundle#getString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Get a message localized to the specified locale, using the message ID
* and package name if no message is available. The locale is normally
* that of the client of a service, chosen with knowledge that both the
* client and this server support that locale. There are two error
* cases: first, when the specified locale is unsupported or null, the
* default locale is used if possible; second, when no bundle supports
* that locale, the message ID and package name are used.
*
* @param locale The locale of the message to use. If this is null,
* the default locale will be used.
* @param messageId The ID of the message to use.
* @return The message, localized as described above.
*/
public String getMessage(Locale locale,
String messageId) {
ResourceBundle bundle;
// cope with unsupported locale...
if (locale == null)
locale = Locale.getDefault();
try {
bundle = ResourceBundle.getBundle(bundleName, locale);
} catch (MissingResourceException e) {
bundle = ResourceBundle.getBundle(bundleName, Locale.ENGLISH);
}
return bundle.getString(messageId);
}
static String message(ELContext context, String name, Object... props) {
Locale locale = null;
if (context != null) {
locale = context.getLocale();
}
if (locale == null) {
locale = Locale.getDefault();
if (locale == null) {
return "";
}
}
ResourceBundle bundle = ResourceBundle.getBundle(
"javax.el.LocalStrings", locale);
try {
String template = bundle.getString(name);
if (props != null) {
template = MessageFormat.format(template, props);
}
return template;
} catch (MissingResourceException e) {
return "Missing Resource: '" + name + "' for Locale "
+ locale.getDisplayName();
}
}
/**
* Gets a localized message given its code and bundle
* information. If there isn't any message matching to these infromation, a
* warning is traced.
*
* @param code The message's code string
* @param bundle The message's bundel string
*
* @return A string containing the message
*/
public static String getMessage(String code, String bundle) {
Locale locale = getLocaleForMessage();
ResourceBundle messages = ResourceBundle.getBundle(bundle, locale);
if (messages == null) {
return null;
}
String message = code;
try {
message = messages.getString(code);
}
catch (Exception ex) {
logger.warn("code [" + code + "] not found ", ex);
}
return message;
}
/**
* @param key The key (treated as a direct format string if not present)
* @param values An optional collection of value substitutions for {@link java.text.MessageFormat}
* @return The localised text with any substitutions made
*/
public static String safeText(MessageKey key, Object... values) {
// Simplifies processing of empty text
if (key == null) {
return "";
}
ResourceBundle rb = currentResourceBundle();
final String message;
if (!rb.containsKey(key.getKey())) {
// If no key is present then use it direct
message = key.getKey();
} else {
// Must have the key to be here
message = rb.getString(key.getKey());
}
return MessageFormat.format(message, values);
}
private String computeLocalizedLevelName(Locale newLocale) {
ResourceBundle rb = ResourceBundle.getBundle(resourceBundleName, newLocale);
final String localizedName = rb.getString(name);
final boolean isDefaultBundle = defaultBundle.equals(resourceBundleName);
if (!isDefaultBundle) return localizedName;
// This is a trick to determine whether the name has been translated
// or not. If it has not been translated, we need to use Locale.ROOT
// when calling toUpperCase().
final Locale rbLocale = rb.getLocale();
final Locale locale =
Locale.ROOT.equals(rbLocale)
|| name.equals(localizedName.toUpperCase(Locale.ROOT))
? Locale.ROOT : rbLocale;
// ALL CAPS in a resource bundle's message indicates no translation
// needed per Oracle translation guideline. To workaround this
// in Oracle JDK implementation, convert the localized level name
// to uppercase for compatibility reason.
return Locale.ROOT.equals(locale) ? name : localizedName.toUpperCase(locale);
}
public static String resolveMessage(ResourceBundle bundle, StatusCode statusCode, Object[] placeHolders) {
String message = null;
if (bundle != null && statusCode != null) {
try {
message = bundle.getString(statusCode.toString());
message = MessageFormat.format(message, placeHolders);
} catch (MissingResourceException var5) {
message = getDefaultMessage(bundle, statusCode);
}
} else {
message = getDefaultMessage(bundle, statusCode);
}
LOG.info("StatusResolver - RESOURSEBUNDLE - Status message: " + message);
return message;
}
/**
* Overrides this method to get the formatted&localized error message.
*
* REVISIT: the system locale is used to load the property file.
* do we want to allow the appilcation to specify a
* different locale?
*/
public String getMessage() {
ResourceBundle resourceBundle = null;
resourceBundle = SecuritySupport.getResourceBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLSchemaMessages");
if (resourceBundle == null)
throw new MissingResourceException("Property file not found!", "com.sun.org.apache.xerces.internal.impl.msg.XMLSchemaMessages", key);
String msg = resourceBundle.getString(key);
if (msg == null) {
msg = resourceBundle.getString("BadMessageKey");
throw new MissingResourceException(msg, "com.sun.org.apache.xerces.internal.impl.msg.XMLSchemaMessages", key);
}
if (args != null) {
try {
msg = java.text.MessageFormat.format(msg, args);
} catch (Exception e) {
msg = resourceBundle.getString("FormatFailed");
msg += " " + resourceBundle.getString(key);
}
}
return msg;
}
private String localize( String key ) {
ResourceBundle rb;
if(locale==null)
rb = ResourceBundle.getBundle(NameGetter.class.getName());
else
rb = ResourceBundle.getBundle(NameGetter.class.getName(),locale);
return rb.getString(key);
}
public static NumberFormat getCurrencyInstance(final Locale aLocale) {
final ResourceBundle bundle = ResourceBundle.getBundle("localedata", aLocale);
final DecimalFormatSymbols theSymbols = DecimalFormatSymbols.getInstance(aLocale);
final DecimalFormat theFormat = new DecimalFormat(bundle.getString("numberformat.currency"), theSymbols);
// TODO: Set currency
return theFormat;
}
@Override
public UITemplate getTemplate(Locale locale) {
ResourceBundle bundle = ResourceBundle.getBundle("FolderBigResource", locale);
List<UITemplate.Argument> arguments = new ArrayList<>();
arguments.add(new UITemplate.StringArgument(P_ROOT_FOLDER, bundle.getString("folder.rootFolder"), true){
@Override
public String getHint() {
return bundle.getString("folder.hint");
}
});
arguments.add(new UITemplate.BooleanArgument(P_FOLDER_SPLIT_FOLDERS, bundle.getString("folder.splitFolders")));
arguments.add(new UITemplate.StringArgument(P_FOLDER_SPLIT_SIZE, bundle.getString("folder.splitSize")));
arguments.add(new UITemplate.BooleanArgument(P_FOLDER_CLEANUP, bundle.getString("folder.cleanup")));
return new UITemplate(getType(), bundle.getString("folder"), arguments);
}
public static Version getProgVersion() {
String TOKEN_VERSION = "VERSION";
try {
ResourceBundle.clearCache();
ResourceBundle rb = ResourceBundle.getBundle(RBVERSION);
if (rb.containsKey(TOKEN_VERSION)) {
return new Version(rb.getString(TOKEN_VERSION));
}
} catch (Exception e) {
Log.errorLog(134679898, e);
}
return new Version("");
}
public static List<String> getLabelAndSynonyms(Class<? extends Tag> tag, Locale locale) {
ResourceBundle rb = ResourceBundle.getBundle(TAGS_BUNDLE_NAME, locale);
try {
String entry = rb.getString(tag.getAnnotation(TagInfo.class).id());
return Arrays.asList(entry.toLowerCase(locale).split(","));
} catch (MissingResourceException e) {
return Collections.singletonList(tag.getAnnotation(TagInfo.class).label());
}
}
private String getResource(String key, Locale locale) {
if (locale == null) {
locale = Locale.getDefault();
}
/**
* If an applet supplies an implementation of IIOMetadataFormat and
* resource bundles, then the resource bundle will need to be
* accessed via the applet class loader. So first try the context
* class loader to locate the resource bundle.
* If that throws MissingResourceException, then try the
* system class loader.
*/
ClassLoader loader = (ClassLoader)
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction() {
public Object run() {
return Thread.currentThread().getContextClassLoader();
}
});
ResourceBundle bundle = null;
try {
bundle = ResourceBundle.getBundle(resourceBaseName,
locale, loader);
} catch (MissingResourceException mre) {
try {
bundle = ResourceBundle.getBundle(resourceBaseName, locale);
} catch (MissingResourceException mre1) {
return null;
}
}
try {
return bundle.getString(key);
} catch (MissingResourceException e) {
return null;
}
}
public static void main(String[] args) {
ResourceBundle rb = ResourceBundle.getBundle("bug6572242");
String data = rb.getString("data");
if (!data.equals("type")) {
throw new RuntimeException("got \"" + data + "\", expected \"type\"");
}
}
@Override
public String getOperationParameterDescription(final String operationName, final String paramName, final Locale locale, final ResourceBundle bundle) {
if (this.operationName.equals(operationName)) {
return bundle.getString(getKey(paramName));
}
return super.getOperationParameterDescription(operationName, paramName, locale, bundle);
}
public Object[] getDecimalFormatSymbolsData() {
Object[] dfsdata;
removeEmptyReferences();
ResourceReference data = cache.get(DECIMAL_FORMAT_SYMBOLS_DATA_CACHEKEY);
if (data == null || ((dfsdata = (Object[]) data.get()) == null)) {
// Note that only dfsdata[0] is prepared here in this method. Other
// elements are provided by the caller, yet they are cached here.
ResourceBundle rb = localeData.getNumberFormatData(locale);
dfsdata = new Object[3];
// NumberElements look up. First, try the Unicode extension
String numElemKey;
String numberType = locale.getUnicodeLocaleType("nu");
if (numberType != null) {
numElemKey = numberType + ".NumberElements";
if (rb.containsKey(numElemKey)) {
dfsdata[0] = rb.getStringArray(numElemKey);
}
}
// Next, try DefaultNumberingSystem value
if (dfsdata[0] == null && rb.containsKey("DefaultNumberingSystem")) {
numElemKey = rb.getString("DefaultNumberingSystem") + ".NumberElements";
if (rb.containsKey(numElemKey)) {
dfsdata[0] = rb.getStringArray(numElemKey);
}
}
// Last resort. No need to check the availability.
// Just let it throw MissingResourceException when needed.
if (dfsdata[0] == null) {
dfsdata[0] = rb.getStringArray("NumberElements");
}
cache.put(DECIMAL_FORMAT_SYMBOLS_DATA_CACHEKEY,
new ResourceReference(DECIMAL_FORMAT_SYMBOLS_DATA_CACHEKEY, (Object) dfsdata, referenceQueue));
}
return dfsdata;
}
public TopicInfoFactoryView() {
ResourceBundle resources = ResourceBundle.getBundle("com/adr/helloiot/fxml/clientlogin");
name = resources.getString("label.topicinfo.View");
}
public static String translate(String bundle, String key) {
ResourceBundle b = ResourceBundle.getBundle(bundle);
return b.getString(key);
}
/** Returns the compiler version baked into the jar. */
public static String getReleaseVersion() {
ResourceBundle config = ResourceBundle.getBundle(CONFIG_RESOURCE);
return config.getString("compiler.version");
}
/** {@inheritDoc} */
@Override
public String getChildTypeDescription(String childType, Locale locale, ResourceBundle bundle) {
final String bundleKey = useUnprefixedChildTypes ? childType : getBundleKey(childType);
return bundle.getString(bundleKey);
}