Java MessageFormat中的嵌套选择子句?
我正在尝试使用java.text.MessageFormat执行一个简单的逻辑:Java MessageFormat中的嵌套选择子句?,java,messageformat,Java,Messageformat,我正在尝试使用java.text.MessageFormat执行一个简单的逻辑: MessageFormat cf = new MessageFormat( "{0,choice, 1<hello|5<{1,choice,1<more than one|4<more than four}}"); Object[] array = {3, 1}; System.out.println(cf.format(array)); MessageFormat cf=新的Mes
MessageFormat cf = new MessageFormat(
"{0,choice, 1<hello|5<{1,choice,1<more than one|4<more than four}}");
Object[] array = {3, 1};
System.out.println(cf.format(array));
MessageFormat cf=新的MessageFormat(
{0,choice,1如果您这样编写模式,选项格式
无法解析格式,因为它无法知道是否控制字符像格式分隔符(|
)用于内部格式或外部格式。但是如果您引用嵌套的格式,则可以告诉解析器引用的文本不包含它应该分析的任何控制字符。ChoiceFormat
将只返回包含另一个ChoiceFormat
模式的文本
如果MessageFormat
类应用了ChoiceFormat
,它会将结果再次解析为MessageFormat
,以处理额外的参数处理,然后处理内部ChoiceFormat
因此,如果您像这样编写模式,代码将起作用:
MessageFormat cf = new MessageFormat(
"{0,choice, 1<hello|5<'{1,choice,1<more than one|4<more than four}'}");
Object[] array = {3, 1};
System.out.println(cf.format(array));
MessageFormat cf=新的MessageFormat(
“{0,choice,1正如@Reboot所提到的,这些类的混淆部分在于ChoiceFormat
被MessageFormat.subformat()
这里特别处理:
子格式=格式[i];
if(ChoiceFormat的子表单实例){
arg=格式[i]。格式(obj);
if(arg.indexOf('{')>=0){
子格式=新消息格式(arg,locale);
obj=参数;
arg=null;
}
}
这种黑客行为允许MessageFormat
包含ChoiceFormat
,该格式本身包含MessageFormat
:
new ChoiceFormat(“0#无| 1#一| 1您可以放置整个堆栈跟踪吗?”?
MessageFormat cf = new MessageFormat(
"{0,choice, 1<hello|5<'{1,choice,1<more than one|4<more than four}'}");
Object[] array = {3, 1};
System.out.println(cf.format(array));