繁体   English   中英

{0}和{0,数字,整数}之间有什么区别

[英]What is the difference between {0} and {0,number,integer}

我试图更多地了解Java的MessageFormat实用程序,在我们的代码库和其他地方的示例中,我看到{0}{0,number,integer}都用于数字,但我不确定哪一个,如果是,是优选的。

快速测试打印差异:

import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Locale;

public class MessageFormatTest
{
    public static void main(String[] args){
        MessageFormat simpleChoiceTest = new MessageFormat("{0}");
        MessageFormat explicitChoiceTest = new MessageFormat("{0,number,integer}");
        int[] set = new int[]{0,1,4,5,6,10,10000,24345};
        Locale[] locs = new Locale[]{Locale.US,Locale.UK,Locale.FRANCE,Locale.GERMANY};
        for(Locale loc : locs){
            simpleChoiceTest.setLocale(loc);
            explicitChoiceTest.setLocale(loc);
            for(int i : set){
                String simple = simpleChoiceTest.format(new Object[]{i});
                String explicit = explicitChoiceTest.format(new Object[]{i});
                if(!simple.equals(explicit)){
                    System.out.println(loc+" - "+i+":\t"+simple+
                        "\t"+NumberFormat.getInstance(loc).format(i));
                    System.out.println(loc+" - "+i+":\t"+explicit+
                        "\t"+NumberFormat.getIntegerInstance(loc).format(i));
                }
            }
        }
    }
}

输出:

fr_FR - 10000:  10 000  10 000
fr_FR - 10000:  10,000  10 000
fr_FR - 24345:  24 345  24 345
fr_FR - 24345:  24,345  24 345
de_DE - 10000:  10.000  10.000
de_DE - 10000:  10,000  10.000
de_DE - 24345:  24.345  24.345
de_DE - 24345:  24,345  24.345

让我感到惊讶的是,如果有什么我会期望{0}不对数字做任何事情,并且{0,number,integer}正确地进行本地化。 相反,两者都被本地化,但似乎显式形式总是使用en_US本地化。

根据链接文档, {0}通过NumberFormat.getInstance(getLocale()) ,而显式表单使用NumberFormat.getIntegerInstance(getLocale()) 然而,当我直接调用它们(输出中的最后一列)时,两者看起来都是相同的,并且都正确地进行了本地化。

我在这里错过了什么?

你是对的。 当您使用“MessageFormat(”{0,number,integer}“)”时,格式化程序在初始化时使用默认语言环境(en_US),并且数字被标记为在默认语言环境(en_US)中使用整数格式,因为下面的代码被执行在初始化时间本身。

// this method is internally called at the time of initialization
MessageFormat.makeFormat()
// line below uses default locale if locale is not
// supplied at initialization (constructor argument) 
newFormat = NumberFormat.getIntegerInstance(locale);

由于您之后设置了区域设置,因此对分配给数字的格式模式没有影响。 如果您想以数字格式使用所需的语言环境,请在初始化时使用locale参数,例如下面:

MessageFormat test = new MessageFormat("{0,number,integer}", Locale.FRANCE);

在我看来,这是一个Java错误(界面错误)或文档问题。 您应该在Oracle上打开一个新问题来纠正这个问题。

正如Yogendra Singh所说,格式化程序(DecimalFormat)的实例是在MessageFormat构造函数时创建的。

MessageFormat simpleChoiceTest = new MessageFormat("{0}");
System.out.println(simpleChoiceTest.getFormatsByArgumentIndex()[0]);
//Prints null
MessageFormat explicitChoiceTest = new MessageFormat("{0,number,currency}");
System.out.println(explicitChoiceTest.getFormatsByArgumentIndex()[0]);
//Prints java.text.DecimalFormat@67500

调用MessageFormat.setLocale时,它不会更改其内部格式化程序的语言环境。

至少应该更改文档以反映此问题。

这是我的java版本:java版“1.7.0_07”Java(TM)SE运行时环境(版本1.7.0_07-b11)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM