1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17package org.apache.commons.math.exception.util; 18 19import java.text.MessageFormat; 20import java.util.Locale; 21 22/** 23 * Class for constructing localized messages. 24 * 25 * @since 2.2 26 * @version $Revision$ $Date$ 27 */ 28public class MessageFactory { 29 /** 30 * Class contains only static methods. 31 */ 32 private MessageFactory() {} 33 34 /** 35 * Builds a message string by from a pattern and its arguments. 36 * 37 * @param locale Locale in which the message should be translated. 38 * @param pattern Format specifier. 39 * @param arguments Format arguments. 40 * @return a localized message string. 41 */ 42 public static String buildMessage(Locale locale, 43 Localizable pattern, 44 Object ... arguments) { 45 return buildMessage(locale, null, pattern, arguments); 46 } 47 48 /** 49 * Builds a message string by from two patterns (specific and general) and 50 * an argument list. 51 * 52 * @param locale Locale in which the message should be translated. 53 * @param specific Format specifier (may be null). 54 * @param general Format specifier (may be null). 55 * @param arguments Format arguments. They will be substituted in 56 * <em>both</em> the {@code general} and {@code specific} format specifiers. 57 * @return a localized message string. 58 */ 59 public static String buildMessage(Locale locale, 60 Localizable specific, 61 Localizable general, 62 Object ... arguments) { 63 final StringBuilder sb = new StringBuilder(); 64 if (general != null) { 65 final MessageFormat fmt = new MessageFormat(general.getLocalizedString(locale), locale); 66 sb.append(fmt.format(arguments)); 67 } 68 if (specific != null) { 69 if (general != null) { 70 sb.append(": "); 71 } 72 final MessageFormat fmt = new MessageFormat(specific.getLocalizedString(locale), locale); 73 sb.append(fmt.format(arguments)); 74 } 75 76 return sb.toString(); 77 } 78} 79