/** * Returns the formatted custom message if one is configured. * @return the formatted custom message or {@code null} * if there is no custom message */ private String getCustomMessage() { String message = null; if (customMessage != null) { final MessageFormat formatter = new MessageFormat(customMessage, Locale.ROOT); message = formatter.format(args); } return message; }
/** * Translate a text string based on our i18n files. * @since 3.1 */ public static String i18n(ResourceBundle messages, String key, Object... messageArguments) { MessageFormat formatter = new MessageFormat(""); formatter.applyPattern(messages.getString(key).replaceAll("'", "''")); return formatter.format(messageArguments); }
/** * Get a string from the underlying resource bundle and format * it with the given set of arguments. * * @param key * @param args */ public String getString(final String key, final Object... args) { String value = getString(key); if (value == null) { value = key; } MessageFormat mf = new MessageFormat(value); mf.setLocale(locale); return mf.format(args, new StringBuffer(), null).toString(); }
format = new MessageFormat(((ResourceBundle) object).getString(key)); format.setLocale(getBundle().getLocale()); formats.put(key, format); return format.format(params);
public void setInitialContext(InitialRenderContext context) { super.setInitialContext(context); String outputTemplate = outputMessages.getString(getLocaleKey() + ".print"); formatter = new MessageFormat(""); formatter.applyPattern(outputTemplate); }
FalseFriendRuleHandler(Language textLanguage, Language motherTongue) { messages = ResourceBundle.getBundle( JLanguageTool.MESSAGE_BUNDLE, motherTongue.getLocale()); formatter = new MessageFormat(""); formatter.setLocale(motherTongue.getLocale()); this.textLanguage = textLanguage; this.motherTongue = motherTongue; }
/** * Gets the translated message. * @return the translated message */ public String getMessage() { String message = getCustomMessage(); if (message == null) { try { // Important to use the default class loader, and not the one in // the GlobalProperties object. This is because the class loader in // the GlobalProperties is specified by the user for resolving // custom classes. final ResourceBundle resourceBundle = getBundle(bundle); final String pattern = resourceBundle.getString(key); final MessageFormat formatter = new MessageFormat(pattern, Locale.ROOT); message = formatter.format(args); } catch (final MissingResourceException ignored) { // If the Check author didn't provide i18n resource bundles // and logs error messages directly, this will return // the author's original message final MessageFormat formatter = new MessageFormat(key, Locale.ROOT); message = formatter.format(args); } } return message; }
private String getFormatRuntime() { final long millisecond = 1; final long second = 1000 * millisecond; final long minute = 60 * second; final long hour = 60 * minute; final long day = 24 * hour; final MessageFormat messageFormat = new MessageFormat("[ {0} days, {1} hours, {2} minutes, {3} seconds ]"); long time = System.currentTimeMillis() - this.messageStoreBootTimestamp; long days = time / day; long hours = (time % day) / hour; long minutes = (time % hour) / minute; long seconds = (time % minute) / second; return messageFormat.format(new Long[] {days, hours, minutes, seconds}); }
value = bundle.getString(key); MessageFormat messageFormat = new MessageFormat(""); messageFormat.setLocale(bundle.getLocale()); messageFormat.applyPattern(value); try { value = messageFormat.format(arguments.toArray());
MessageFormat mf = new MessageFormat(result); mf.setLocale(locale); mf.format(objects, buffer, passedField); handleArgumentField(begin, buffer.length(), argumentNumbers[i], position, fields); handleFormat(format, arg, begin, fields);
public void setInitialContext(InitialRenderContext context) { super.setInitialContext(context); String outputTemplate = outputMessages.getString(getLocaleKey() + ".print"); formatter = new MessageFormat(""); formatter.applyPattern(outputTemplate); }
/** * Replace MessageFormat(String, Locale) constructor (not available until JDK 1.4). * @param pattern string * @param locale Locale * @return MessageFormat */ private MessageFormat createMessageFormat(final String pattern, final Locale locale) { final MessageFormat result = new MessageFormat(pattern); if (locale != null) { result.setLocale(locale); result.applyPattern(pattern); } return result; }
public final List<AbstractPatternRule> getRules(InputStream stream, Language textLanguage, Language motherTongue) throws ParserConfigurationException, SAXException, IOException { FalseFriendRuleHandler handler = new FalseFriendRuleHandler( textLanguage, motherTongue); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); saxParser.getXMLReader().setFeature( "http://apache.org/xml/features/nonvalidating/load-external-dtd", false); saxParser.parse(stream, handler); List<AbstractPatternRule> rules = handler.getRules(); // Add suggestions to each rule: ResourceBundle messages = ResourceBundle.getBundle( JLanguageTool.MESSAGE_BUNDLE, motherTongue.getLocale()); MessageFormat msgFormat = new MessageFormat(messages.getString("false_friend_suggestion")); for (AbstractPatternRule rule : rules) { List<String> suggestions = handler.getSuggestionMap().get(rule.getId()); if (suggestions != null) { String[] msg = { formatSuggestions(suggestions) }; rule.setMessage(rule.getMessage() + " " + msgFormat.format(msg)); } } return rules; }
public static String getFormattedText(Locale locale, String message, Object[] params) { MessageFormat messageFormat = null; if (params == null || message == null) { return message; } if (locale != null) { messageFormat = new MessageFormat(message, locale); } else { messageFormat = new MessageFormat(message); } return messageFormat.format(params); }
public String toString() { String message = bundle.getString(definition.key); MessageFormat format = new MessageFormat(message); format.setLocale(bundle.getLocale()); String formattedMessage = format.format(args); return formattedMessage; } }
/** * Creates a <code>MessageFormat</code> object and set the <code>Locale</code> to default and formats the message */ private void errorMsg(String key, Object[] args) { MessageFormat mf = new MessageFormat(key); mf.setLocale(Locale.getDefault()); LOGGER.log(Level.SEVERE, mf.format(args)); }
/** * Replace MessageFormat(String, Locale) constructor (not available until JDK 1.4). * @param pattern string * @param locale Locale * @return MessageFormat */ private MessageFormat createMessageFormat(final String pattern, final Locale locale) { final MessageFormat result = new MessageFormat(pattern); if (locale != null) { result.setLocale(locale); result.applyPattern(pattern); } return result; }
/** * Gets the check message 'as is' from appropriate 'messages.properties' * file. * * @param messageBundle the bundle name. * @param messageKey the key of message in 'messages.properties' file. * @param arguments the arguments of message in 'messages.properties' file. * @return The message of the check with the arguments applied. */ private static String internalGetCheckMessage( String messageBundle, String messageKey, Object... arguments) { final ResourceBundle resourceBundle = ResourceBundle.getBundle( messageBundle, Locale.getDefault(), Thread.currentThread().getContextClassLoader(), new LocalizedMessage.Utf8Control()); final String pattern = resourceBundle.getString(messageKey); final MessageFormat formatter = new MessageFormat(pattern, Locale.ROOT); return formatter.format(arguments); }
protected String formatMessage(final String msgPattern, final Object... args) { try { final MessageFormat temp = new MessageFormat(msgPattern, locale); return temp.format(args); } catch (final IllegalFormatException ife) { LOGGER.error("Unable to format msg: " + msgPattern, ife); return msgPattern; } }
public static String message( Locale locale, String bundleName, Class<?> type, String key, Object... args ) { ResourceBundle bundle = ResourceBundle.getBundle( bundleName, locale ); String template = bundle.getString( type.getName() + '.' + key ); MessageFormat format = new MessageFormat( template ); format.setLocale( locale ); return format.format( args ); } }