1 /* 2 * Copyright 2004-2008 the Seasar Foundation and the Others. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 13 * either express or implied. See the License for the specific language 14 * governing permissions and limitations under the License. 15 */ 16 package org.seasar.cubby.util; 17 18 import java.text.MessageFormat; 19 import java.util.MissingResourceException; 20 import java.util.ResourceBundle; 21 22 import org.seasar.cubby.controller.ThreadContext; 23 24 /** 25 * メッセージリソースを取得するユーティリティクラスです。 26 * 27 * @author agata 28 * @since 1.0.0 29 */ 30 public class Messages { 31 32 /** 33 * 指定されたメッセージリソースから指定されたキーの値を取得し、置換処理を行ったメッセージを取得します。 34 * 35 * @param resource 36 * メッセージリソース 37 * @param key 38 * メッセージキー 39 * @param args 40 * 置換文字列 41 * @return 置換処理後のメッセージ 42 */ 43 public static String getText(final ResourceBundle resource, 44 final String key, final Object... args) { 45 try { 46 final String text = resource.getString(key); 47 return MessageFormat.format(text, args); 48 } catch (final MissingResourceException e) { 49 return key; 50 } 51 } 52 53 /** 54 * メッセージリソース(messages.properties)から指定されたキーの値を取得し、置換処理を行ったメッセージを取得します。 55 * 56 * <pre> 57 * msg.sample2=メッセージ中に置換文字列を使用できます(引数1={0}, 引数2={1})。 58 * 59 * // 「メッセージ中に置換文字列を使用できます(引数1=foo, 引数2=bar)。」 60 * String message = Messages.getText("msg.sample2", "foo", "bar"); 61 * </pre> 62 * 63 * @see Messages#getText(ResourceBundle, String, Object...) 64 * @param key 65 * メッセージキー 66 * @param args 67 * 置換文字列 68 * @return 置換処理後のメッセージ 69 */ 70 public static String getText(final String key, final Object... args) { 71 final ResourceBundle messagesResourceBundle = ThreadContext 72 .getMessagesResourceBundle(); 73 return getText(messagesResourceBundle, key, args); 74 } 75 }