1

java.util.resourcebundle を使用して JSTL メッセージをフォーマットしていますが、これは正常に機能します。

ここで見ることができるクラス MessageFormat を使用します。今、これをメソッドにカプセル化したいのですが、getParametrizedMessage(String key, String[]parameters)それを行う方法がわかりません。ここで、パラメーターを使用して 1 つまたは 2 つのメッセージを表示するだけでも、非常に多くの作業が必要になります。

UserMessage um = null;   
ResourceBundle messages = ResourceBundle.getBundle("messages");
String str = messages.getString("PF1");
Object[] messageArguments = new String[]{nyreg.getNummer()};
MessageFormat formatter = new MessageFormat("");
formatter.applyPattern(messages.getString("PI14"));
String outputPI14 = formatter.format(messageArguments);
formatter.applyPattern(messages.getString("PI15"));
String outputPI15 = formatter.format(messageArguments)
if(ipeaSisFlag) 
if(checkIfPCTExistInDB && nyreg.isExistInDB()) {            
//um = new ExtendedUserMessage(MessageHandler.getParameterizedMessage("PI15", new String[]{nyreg.getNummer()}) , UserMessage.TYPE_INFORMATION, "Info");
um = new ExtendedUserMessage(outputPI15 , UserMessage.TYPE_INFORMATION, "Info");

…等々。このロジックを静的クラス MessageHandler.getParameterizedMessage に移動できますが、これは現在機能しておらず、次のようになっています。

private final static String dictionaryFileName="messages.properties";

public static String getParameterizedMessage(String key, String [] params){
        if (dictionary==null){
            loadDictionary();
        }
        return getParameterizedMessage(dictionary,key,params);
    }

    private static void loadDictionary(){       
        String fileName = dictionaryFileName;   
                try {
            dictionary=new Properties();
            InputStream fileInput = MessageHandler.class.getClassLoader().getResourceAsStream(fileName);
            dictionary.load(fileInput);
            fileInput.close();
        }
        catch(Exception e) {
            System.err.println("Exception reading propertiesfile in init "+e);
            e.printStackTrace();
            dictionary=null;
        }
    }

キーとパラメータでメソッドを呼び出すのと同じくらい簡単に、パラメータ化されたメッセージを使用するにはどうすればよいですか?

助けてくれてありがとう

アップデート

ロジックは、 this が拡張する抽象クラスにある継承されたメソッドから取得されます。メソッドは次のようになります。

    protected static String getParameterizedMessage(Properties dictionary,String key,String []params){
        if (dictionary==null){
            return "ERROR";
        }
        String msg = dictionary.getProperty(key);
        if (msg==null){
            return "?!Meddelande " +key + " saknas!?";
        }
        if (params==null){
            return msg;
        }
        StringBuffer buff = new StringBuffer(msg);
        for (int i=0;i<params.length;i++){
            String placeHolder = "<<"+(i+1)+">>";
            if (buff.indexOf(placeHolder)!=-1){
                replace(buff,placeHolder,params[i]);
            }
            else {
                remove(buff,placeHolder);
            }
        }
        return buff.toString();
    }

上記のメソッドを単なる辞書ではなくリソースバンドルのように機能させるには、上記のメソッドを書き直す必要があると思います。

更新 2

動きそうなコードはこちら

public static String getParameterizedMessage(String key, Object [] params){

    ResourceBundle messages = ResourceBundle.getBundle("messages");
    MessageFormat formatter = new MessageFormat("");
    formatter.applyPattern(messages.getString(key));
    return formatter.format(params);
}
4

1 に答える 1

1

あなたが何を達成しようとしているのかよくわかりません。これが私が過去にしたことです:

public static final String localize(final Locale locale, final String key, final Object... param) {
    final String name = "message";
    final ResourceBundle rb;

    /* Resource bundles are cached internally,
       never saw a need to implement another caching level
     */
    try {
        rb = ResourceBundle.getBundle(name, locale, Thread.currentThread()
                .getContextClassLoader());
    } catch (MissingResourceException e) {
        throw new RuntimeException("Bundle not found:" + name);
    }

    String keyValue = null;

    try {
        keyValue = rb.getString(key);
    } catch (MissingResourceException e) {
        // LOG.severe("Key not found: " + key);
        keyValue = "???" + key + "???";
    }

    /* Message formating is expensive, try to avoid it */
    if (param != null && param.length > 0) {
        return MessageFormat.format(keyValue, param);
    } else {
        return keyValue;
    }
}
于 2012-05-09T09:23:43.067 に答える