Given a message and parameters, resolve all message's parameter placeholders with the parameter value. : Message Format « I18N « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » I18N » Message Format 




Given a message and parameters, resolve all message's parameter placeholders with the parameter value.
        



import java.text.Format;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * Internationalisation (I18N) helper
 @author eliott bartley
 */
public class I18N
{

    private I18N() { }


    /**
     * Given a message and parameters, resolve all message's parameter
     *  placeholders with the parameter value. The firstParam can change which
     *  parameter relates to {0} placeholder in the message, and all increment
     *  from this index. If any of the parameters also have placeholders, this
     *  recursively calls itself to fill their placeholders, setting the
     *  firstParam to the index following all parameters that are used by the
     *  current message so params must be in the order p0..pN, p00..p0N..pMN,
     *  p000..p00N..p0MN..pLMN... where each additional index is for nested
     *  placeholders (ones in params) and assumes every message/param contains
     *  N M L placeholders; any that don't contain placeholders can have their
     *  pXXX.. taken out, so long as the order of remaining params don't change
     @param message Message to format
     @param firstParam Index of parameter that relates to {0} placeholder,
     *  all parameters following this one relate to incrementing placeholders
     @param params The parameters used to fill the placeholders
     @return Message with all placeholders filled with relative parameters
     */
    private static String formatMessage
    (   String message,
        int firstParam,
        Object[] params
    ){

        // Only need to do any formatting if there are parameters to do the
        //  formatting with. If there are none, the message input is returned
        //  unmodified
        if(params != null && firstParam < params.length)
        {   MessageFormat parser;
            Locale locale = Locale.getDefault();
            Format[] formats;

            // Set up
            parser = new MessageFormat("");
            parser.setLocale(locale);
            parser.applyPattern(message);
            // Used only to count how many parameters are needed by this message
            formats = parser.getFormatsByArgumentIndex();

            // Recursively format the parameters used by this message
            for(int paramIndex = 0; paramIndex < formats.length; paramIndex++)
                if(params[firstParam + paramIndexinstanceof String)
                    params[firstParam + paramIndex= formatMessage
                    (   params[firstParam + paramIndex].toString(),
                        firstParam + formats.length,
                        params
                    );

            // Format the message using the formatted parameters
            message = parser.format(getParams
            (   params,
                firstParam,
                firstParam + formats.length
            ));

        }

        return message;

    }

    /**
     * MessageFormat.format always matches the placeholder's index directly to
     *  the param array index, but because placeholders can be nested, the
     *  zeroth placeholder may not match the zeroth array element, so this
     *  method copies the array so that only the required array elements are
     *  present, and start from the zeroth element
     @param params Complete param array
     @param firstParam First element that will be used in complete param array
     @param lastParam Last element that will be used in complete param array
     @return Param array containing just the elements from
     *  firstParam..lastParam-1. If lastParam is less or equal to firstParam,
     *  null is returned rather than an empty array
     */
    private static Object[] getParams
    (   Object[]    params,
        int     firstParam,
        int      lastParam
    ){  Object[] newParams = null;
        if(firstParam < lastParam)
        {   newParams = new Object[lastParam - firstParam];
            for(int i = firstParam; i < lastParam; i++)
                newParams[i - firstParam= params[i];
        }
        return newParams;
    }

}

   
    
    
    
    
    
    
    
  














Related examples in the same category
1.Formatting Messages: Date and NumberFormatting Messages: Date and Number
2.Set MessageFormat to Locale.USSet MessageFormat to Locale.US
3.Date Number SampleDate Number Sample
4.Message Format for sentence
5.Java I18N: Format : Message Format DemoJava I18N: Format : Message Format Demo
6.Formatting Messages: Arabic DigitFormatting Messages: Arabic Digit
7.Formatting Messages: Change EraFormatting Messages: Change Era
8.Formatting Messages: Message Format ReuseFormatting Messages: Message Format Reuse
9.A text format similar to MessageFormat but using string rather than numeric keys.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.