2011-03-28 16 views
47

tengo algo como:¿Cómo formatear el mensaje con nombres de argumento en lugar de números?

String text = "The user {0} has email address {1}." 
// params = { "Robert", "[email protected]" } 
String msg = MessageFormat.format(text, params); 

Esto no es bueno para mí, porque a veces mis traductores no están seguros de lo que pasa en el campo {0} y {1}, también sería bueno poder reformular los mensajes sin preocuparse por el orden de los argumentos.

Me gustaría reemplazar los argumentos con nombres legibles en lugar de números. Algo como esto:

String text = "The user {USERNAME} has email address {EMAILADDRESS}." 
// Map map = new HashMap(... [USERNAME="Robert", EMAILADDRESS="[email protected]"] 
String msg = MessageFormat.format(text, map); 

¿Hay una manera fácil de hacer esto?

Gracias! robo

+2

Commons Lang tiene StrSubstitutor – Ramon

+0

Puedo estar equivocado, pero parece que [JTPL] (http://jtpl.sourceforge.net/) puede ayudarte. – Nishant

+0

Relacionados: http://stackoverflow.com/q/2286648/435605 –

Respuesta

26

Usted puede utilizar MapFormat para esto. Descubre los detalles aquí:

http://www.java2s.com/Code/Java/I18N/AtextformatsimilartoMessageFormatbutusingstringratherthannumerickeys.htm

String text = "The user {name} has email address {email}."; 
      Object[] params = { "nameRobert", "[email protected]" }; 
      Map map = new HashMap(); 
      map.put("name", "Robert"); 
      map.put("email", "[email protected]"); 

System.out.println("1st : " + MapFormat.format(text, map)); 

SALIDA: 1 °: El usuario tiene Robert dirección de correo electrónico [email protected]

+3

¿Con qué seguridad podemos usar esta clase para formatear? – Ketan

9

Fácil de hacer uno usted mismo. Esto es lo que yo uso (la función main() es sólo para código de prueba):

import java.util.HashMap; 
import java.util.Map; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class StringTemplate { 
    final private String template; 
    final private Matcher m; 
    static final private Pattern keyPattern = 
     Pattern.compile("\\$\\{([a-zA-Z][a-zA-Z0-9_]*(\\.[a-zA-Z][a-zA-Z0-9_]*)*)\\}"); 
    private boolean blanknull=false; 

    public StringTemplate(String template) { 
     this.template=template; 
     this.m = keyPattern.matcher(template); 
    } 

    /** 
    * @param map substitution map 
    * @return substituted string 
    */ 
    public String substitute(Map<String, ? extends Object> map) 
    { 
     this.m.reset(); 
     StringBuffer sb = new StringBuffer(); 
     while (this.m.find()) 
     { 
      String k0 = this.m.group(); 
      String k = this.m.group(1); 
      Object vobj = map.get(k); 
      String v = (vobj == null) 
       ? (this.blanknull ? "" : k0) 
       : vobj.toString(); 
      this.m.appendReplacement(sb, Matcher.quoteReplacement(v)); 
     } 
     this.m.appendTail(sb); 
     return sb.toString();  
    } 

    public StringTemplate setBlankNull() 
    { 
     this.blanknull=true; 
     return this; 
    } 

    static public void main(String[] args) 
    { 
     StringTemplate t1 = new StringTemplate("${this} is a ${test} of the ${foo} bar=${bar} ${emergency.broadcasting.system}"); 
     t1.setBlankNull(); 
     Map<String, String> m = new HashMap<String, String>(); 
     m.put("this", "*This*"); 
     m.put("test", "*TEST*"); 
     m.put("foo", "$$$aaa\\\\111"); 
     m.put("emergency.broadcasting.system", "EBS"); 
     System.out.println(t1.substitute(m)); 
    } 
} 
1
static final Pattern REPLACE_PATTERN = Pattern.compile("\\x24\\x7B([a-zA-Z][\\w\\x2E].*?)\\x7D"); 

/** 
* Check for unresolved environment 
* 
* @param str 
* @return origin if all substitutions resolved 
*/ 
public static String checkReplacement(String str) { 
    Matcher matcher = REPLACE_PATTERN.matcher(str); 
    if (matcher.find()) { 
     throw LOG.getIllegalArgumentException("Environment variable '" + matcher.group(1) + "' is not defined"); 
    } 
    return str; 
} 

// replace in str ${key} to value 
public static String resolveReplacement(String str, Map<String, String> replacements) { 
    Matcher matcher = REPLACE_PATTERN.matcher(str); 
    while (matcher.find()) { 
     String value = replacements.get(matcher.group(1)); 
     if (value != null) { 
      str = matcher.replaceFirst(replaceWindowsSlash(value)); 
     } 
    } 
    return str; 
} 

Pero usted suelta todas las opciones de formato (como ## #.)

16

Ver StrSubstitutor de org.apache.commons.lang3:

Map valuesMap = HashMap(); 
valuesMap.put("animal", "quick brown fox"); 
valuesMap.put("target", "lazy dog"); 
String templateString = "The ${animal} jumped over the ${target}."; 
StrSubstitutor sub = new StrSubstitutor(valuesMap); 
String resolvedString = sub.replace(templateString); 

// resolvedString: "The quick brown fox jumped over the lazy dog." 
+0

¿Alguna idea de cómo resolver esto si, por ejemplo, el objetivo es opcional y luego desea tener una cadena vacía en lugar de $ {objetivo}, para establecer una cadena vacía en el mapa de la única manera? – poyger

+1

@poyger No probé esto, pero probablemente podría anular 'Map' para devolver' "" 'en lugar de' null' para las claves que no están mapeadas. –

+1

a partir de hoy, 2018-02, esta clase se ha movido a commons-text: https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/StrSubstitutor .html – Chris

1

sé que mi respuesta llega un poco tarde, pero si usted todavía necesita esta funcionalidad, sin la necesidad de descargar un motor de plantillas completo puede consultar aleph-formatter (soy uno de los autores):

Student student = new Student("Andrei", 30, "Male"); 

String studStr = template("#{id}\tName: #{st.getName}, Age: #{st.getAge}, Gender: #{st.getGender}") 
        .arg("id", 10) 
        .arg("st", student) 
        .format(); 
System.out.println(studStr); 

O se pueden encadenar los argumentos:

String result = template("#{x} + #{y} = #{z}") 
        .args("x", 5, "y", 10, "z", 15) 
        .format(); 
System.out.println(result); 

// Output: "5 + 10 = 15" 

Internamente funciona usando un StringBuilder crear el resultado por "analizar" la expresión, sin concatenación de cadenas, expresiones regulares/sustitución se lleva a cabo.

Cuestiones relacionadas