Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Messages |
|
| 2.0;2 |
1 | /* | |
2 | * Copyright 2004-2010 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 | ||
17 | package org.seasar.cubby.util; | |
18 | ||
19 | import static org.seasar.cubby.CubbyConstants.ATTR_MESSAGES_RESOURCE_BUNDLE; | |
20 | ||
21 | import java.text.MessageFormat; | |
22 | import java.util.MissingResourceException; | |
23 | import java.util.ResourceBundle; | |
24 | ||
25 | import javax.servlet.ServletRequest; | |
26 | ||
27 | import org.seasar.cubby.internal.controller.ThreadContext; | |
28 | ||
29 | /** | |
30 | * メッセージリソースを取得するユーティリティクラスです。 | |
31 | * | |
32 | * @author agata | |
33 | */ | |
34 | 0 | public class Messages { |
35 | ||
36 | /** | |
37 | * 指定されたメッセージリソースから指定されたキーの値を取得し、置換処理を行ったメッセージを取得します。 | |
38 | * | |
39 | * @param resource | |
40 | * メッセージリソース | |
41 | * @param key | |
42 | * メッセージキー | |
43 | * @param args | |
44 | * 置換文字列 | |
45 | * @return 置換処理後のメッセージ | |
46 | */ | |
47 | public static String getText(final ResourceBundle resource, | |
48 | final String key, final Object... args) { | |
49 | try { | |
50 | 80 | final String text = resource.getString(key); |
51 | 39 | return MessageFormat.format(text, args); |
52 | 41 | } catch (final MissingResourceException e) { |
53 | 41 | return key; |
54 | } | |
55 | } | |
56 | ||
57 | /** | |
58 | * メッセージリソース(messages.properties)から指定されたキーの値を取得し、置換処理を行ったメッセージを取得します。 | |
59 | * | |
60 | * <pre> | |
61 | * msg.sample2=メッセージ中に置換文字列を使用できます(引数1={0}, 引数2={1})。 | |
62 | * | |
63 | * // 「メッセージ中に置換文字列を使用できます(引数1=foo, 引数2=bar)。」 | |
64 | * String message = Messages.getText("msg.sample2", "foo", "bar"); | |
65 | * </pre> | |
66 | * | |
67 | * @see Messages#getText(ResourceBundle, String, Object...) | |
68 | * @param key | |
69 | * メッセージキー | |
70 | * @param args | |
71 | * 置換文字列 | |
72 | * @return 置換処理後のメッセージ | |
73 | */ | |
74 | public static String getText(final String key, final Object... args) { | |
75 | 80 | final ThreadContext currentContext = ThreadContext.getCurrentContext(); |
76 | 80 | final ServletRequest request = currentContext.getRequest(); |
77 | 80 | final ResourceBundle bundle = (ResourceBundle) request |
78 | .getAttribute(ATTR_MESSAGES_RESOURCE_BUNDLE); | |
79 | 80 | return getText(bundle, key, args); |
80 | } | |
81 | ||
82 | } |