質問

私は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}が番号に何もしないと予想される場合、およびそれを正しくローカライズすることを期待していたら、代わりに、どちらもローカライズされますが、明示的な形式が常にen_usローカライズを使用しているようです。

リンクされたドキュメントに従って、Explicitフォームが{0,number,integer}を使用している間、{0}NumberFormat.getInstance(getLocale())を実行します。まだ私が直接(出力の最後の列)を呼び出すと、両方とも同一であり、両方とも正しくローカライズされます。

ここで何がありませんか?

役に立ちましたか?

解決

あなたは正しいです。"MessageFormat(" {0、number} ")"を使用すると、フォーマッタは初期化時にデフォルトのロケール(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);
.

後でロケールを設定しているので、数字に割り当てられているフォーマットパターンに影響はありません。数字のフォーマットで欲望のロケールを使いたい場合は、初期化時にロケール引数を使用してください。以下:

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

他のヒント

私の意見では、これはJavaのバグ(インターフェースが間違っている)またはドキュメントの問題です。Oracleで新しい問題を開く必要があります。

ヨーゲンドラSinghとして、MessageFormatコンストラクタのときにフォーマッタ(DecimalFormat)のインスタンスが作成されます。

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帰属
所属していません StackOverflow
scroll top