Java Code Examples for java.util.ResourceBundle#getString()

The following examples show how to use java.util.ResourceBundle#getString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageCatalog.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 2
Source File: DatatypeException.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: Util.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 4
Source File: StatusResolver.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: PortletUtilities.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
* 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;
  }
 
Example 6
Source File: Level.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example 7
Source File: Languages.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
/**
 * @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);
}
 
Example 8
Source File: NameGetter.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 9
Source File: ResolvePathHandler.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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);
}
 
Example 10
Source File: Bug6572242.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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\"");
    }
}
 
Example 11
Source File: IIOMetadataFormatImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example 12
Source File: SemanticTags.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
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());
    }
}
 
Example 13
Source File: Functions.java    From MLib with GNU General Public License v3.0 5 votes vote down vote up
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("");
}
 
Example 14
Source File: FolderConnector.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 15
Source File: TNumberFormat.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 16
Source File: AppStrings.java    From jpexs-decompiler with GNU General Public License v3.0 4 votes vote down vote up
public static String translate(String bundle, String key) {
    ResourceBundle b = ResourceBundle.getBundle(bundle);
    return b.getString(key);
}
 
Example 17
Source File: Closure_18_Compiler_t.java    From coming with MIT License 4 votes vote down vote up
/** Returns the compiler version baked into the jar. */
public static String getReleaseVersion() {
  ResourceBundle config = ResourceBundle.getBundle(CONFIG_RESOURCE);
  return config.getString("compiler.version");
}
 
Example 18
Source File: TopicInfoFactoryView.java    From helloiot with GNU General Public License v3.0 4 votes vote down vote up
public TopicInfoFactoryView() {
    ResourceBundle resources = ResourceBundle.getBundle("com/adr/helloiot/fxml/clientlogin");
    name = resources.getString("label.topicinfo.View");
}
 
Example 19
Source File: LocaleResources.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example 20
Source File: StandardResourceDescriptionResolver.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public String getChildTypeDescription(String childType, Locale locale, ResourceBundle bundle) {
    final String bundleKey = useUnprefixedChildTypes ? childType : getBundleKey(childType);
    return bundle.getString(bundleKey);
}