Java 带有枚举选项的Messageformat
我有一个枚举,将使用类似choice的东西输出带有MessageFormat的字符串。我怎么做 例如:Java 带有枚举选项的Messageformat,java,messageformat,Java,Messageformat,我有一个枚举,将使用类似choice的东西输出带有MessageFormat的字符串。我怎么做 例如: public enum Style{ American, European} ... String s = MessageFormat.format(" {0,choice,American#am|European#eu|null#no}", Style.American); 在没有帮助和厌倦搜索解决方案后,我创建了一个可以解决我的问题的方法: String s = Messa
public enum Style{
American,
European}
...
String s = MessageFormat.format(" {0,choice,American#am|European#eu|null#no}",
Style.American);
在没有帮助和厌倦搜索解决方案后,我创建了一个可以解决我的问题的方法:
String s = MessageFormat.format(choicestring("
{0,choicestring,American#am|European#eu|null#no}",
Style.American.toString()),Style.American.toString());
....
private static String choicestring(String fmt, LinkedList<Object> s) throws NumberFormatException {
String fmtFinal = "";
int start = 0;
Pattern pattern = Pattern.compile("\\{(\\d+),choicestring,(.*)}");
Matcher matcher = pattern.matcher(fmt);
String valorEncontrado = "";
while (matcher.find()) {
String m = matcher.group();
String valorProcurar = s.get(Integer.valueOf(matcher.group(1))).toString();
Optional resultado = Arrays.stream(matcher.group(2).split("\\|")).filter(i -> i.startsWith(valorProcurar)).findFirst();
if (resultado.isPresent()) {
valorEncontrado = resultado.get().toString().substring(valorProcurar.length());
}
fmtFinal += fmt.substring(start,matcher.start())+valorEncontrado;
start = matcher.end();
}
fmtFinal = fmt.substring(start,fmt.length());
fmt = fmtFinal;
return fmt;
}
String s=MessageFormat.format(choicestring(“
{0,choicestring,美国#am |欧洲#eu | null#no}”,
Style.American.toString()),Style.American.toString();
....
私有静态字符串选项字符串(字符串fmt、LinkedList s)引发NumberFormatException{
字符串fmtFinal=“”;
int start=0;
Pattern=Pattern.compile(\{(\\d+),choicestring,(.*);
匹配器匹配器=模式匹配器(fmt);
字符串从价税=”;
while(matcher.find()){
字符串m=matcher.group();
字符串valorProcurar=s.get(Integer.valueOf(matcher.group(1))).toString();
可选的resultado=Arrays.stream(matcher.group(2).split(“\\\\\”).filter(i->i.startsWith(valorProcurar)).findFirst();
if(resultado.isPresent()){
valorEncontrado=resultado.get().toString().substring(valorProcurar.length());
}
fmtFinal+=fmt.substring(start,matcher.start())+从价交易;
start=matcher.end();
}
fmt最终=fmt.substring(开始,fmt.length());
fmt=fmtFinal;
返回fmt;
}
我需要一些动态的东西。你不回答这个问题,你可以试着帮我,或者忘记这个。