• R/O
  • SSH
  • HTTPS

amateras: Commit


Commit MetaInfo

Revisión576 (tree)
Tiempo2011-09-27 22:34:14
Autortakezoe

Log Message

i18nタグを追加。

Cambiar Resumen

Diferencia incremental

--- functions/trunk/functions/src/test/java/jp/sf/amateras/functions/utils/TextUtilsTest.java (nonexistent)
+++ functions/trunk/functions/src/test/java/jp/sf/amateras/functions/utils/TextUtilsTest.java (revision 576)
@@ -0,0 +1,34 @@
1+package jp.sf.amateras.functions.utils;
2+
3+import java.util.Locale;
4+import java.util.MissingResourceException;
5+
6+import junit.framework.TestCase;
7+
8+public class TextUtilsTest extends TestCase {
9+
10+ public void testGetText_1() {
11+ assertEquals("message1", TextUtils.getText("key1", new Locale("en", "US")));
12+ assertEquals("メッセージ1", TextUtils.getText("key1", new Locale("ja", "JP")));
13+ }
14+
15+ public void testGetText_2() {
16+ assertEquals("message2", TextUtils.getText("key2", new Locale("en", "US")));
17+ assertEquals("message2", TextUtils.getText("key2", new Locale("ja", "JP")));
18+ }
19+
20+ public void testGetText_3() {
21+ assertEquals("key3", TextUtils.getText("key3", new Locale("en", "US")));
22+ assertEquals("key3", TextUtils.getText("key3", new Locale("ja", "JP")));
23+ }
24+
25+ public void testGetText_4() {
26+ TextUtils.setBundleName("hoge");
27+ try {
28+ TextUtils.getText("key1", new Locale("en", "US"));
29+ fail();
30+ } catch(MissingResourceException ex){
31+ assertEquals("Can't find bundle for base name hoge, locale en_US", ex.getMessage());
32+ }
33+ }
34+}
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- functions/trunk/functions/src/main/java/jp/sf/amateras/functions/I18N.java (nonexistent)
+++ functions/trunk/functions/src/main/java/jp/sf/amateras/functions/I18N.java (revision 576)
@@ -0,0 +1,20 @@
1+package jp.sf.amateras.functions;
2+
3+import javax.servlet.http.HttpServletRequest;
4+
5+import jp.sf.amateras.functions.utils.RequestUtils;
6+import jp.sf.amateras.functions.utils.TextUtils;
7+
8+/**
9+ * Webアプリケーションの国際化のための関数を提供します。
10+ *
11+ * @author Naoki Takezoe
12+ */
13+public class I18N {
14+
15+ public static String text(String key){
16+ HttpServletRequest request = RequestUtils.getRequest();
17+ return TextUtils.getText(key, request.getLocale());
18+ }
19+
20+}
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- functions/trunk/functions/src/main/java/jp/sf/amateras/functions/utils/TextUtils.java (nonexistent)
+++ functions/trunk/functions/src/main/java/jp/sf/amateras/functions/utils/TextUtils.java (revision 576)
@@ -0,0 +1,74 @@
1+package jp.sf.amateras.functions.utils;
2+
3+import java.util.Locale;
4+import java.util.Map;
5+import java.util.MissingResourceException;
6+import java.util.ResourceBundle;
7+import java.util.concurrent.ConcurrentHashMap;
8+
9+/**
10+ * 国際化されたメッセージを取得するためのユーティリティクラスです。
11+ *
12+ * @author Naoki Takezoe
13+ */
14+public class TextUtils {
15+
16+ private static String bundleName = "messages";
17+
18+ private static ResourceBundle defaultBundle = null;
19+
20+ private static Map<Locale, ResourceBundle> bundleMap = new ConcurrentHashMap<Locale, ResourceBundle>();
21+
22+ /**
23+ * 使用するResourceBundleのバンドル名を設定します。
24+ * デフォルトではmessagesというバンドル名が使用されます。
25+ *
26+ * @param bundleName バンドル名
27+ */
28+ public static void setBundleName(String bundleName){
29+ TextUtils.bundleName = bundleName;
30+ }
31+
32+ /**
33+ * 国際化されたメッセージを取得します。
34+ * <p>
35+ * 引数で指定したロケールに対応するResourceBundleが存在しない場合や、
36+ * 対応するResourceBundleに指定したキーを持つメッセージが存在しない場合は
37+ * また、デフォルトのResourceBundleで定義されたメッセージが返却されます。
38+ * キーに対応するエントリが存在しない場合はキーの文字列をそのまま返却します。
39+ * </p>
40+ * <p>
41+ * デフォルトのResourceBundleが存在しない場合はMissingResourceExceptionがスローされます。
42+ * </p>
43+ *
44+ * @param key メッセージのキー
45+ * @param locale ロケール
46+ * @return キーに対応するメッセージ
47+ * @throw MissingResourceException デフォルトのResourceBundleが存在しない場合
48+ */
49+ public static String getText(String key, Locale locale) throws MissingResourceException {
50+ if(defaultBundle == null){
51+ defaultBundle = ResourceBundle.getBundle(bundleName, new Locale(""));
52+ }
53+
54+ ResourceBundle bundle = bundleMap.get(locale);
55+ if(bundle == null){
56+ bundle = ResourceBundle.getBundle(bundleName, locale);
57+ if(bundle.getLocale().getCountry().equals(locale.getCountry()) ||
58+ bundle.getLocale().getLanguage().equals(locale.getLanguage())){
59+ bundleMap.put(locale, bundle);
60+ } else {
61+ bundle = defaultBundle;
62+ }
63+ }
64+
65+ String text = "";
66+ try {
67+ text = bundle.getString(key);
68+ } catch(MissingResourceException ex){
69+ text = key;
70+ }
71+ return text;
72+ }
73+
74+}
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- functions/trunk/functions/src/main/java/jp/sf/amateras/functions/filter/FunctionsFilter.java (revision 575)
+++ functions/trunk/functions/src/main/java/jp/sf/amateras/functions/filter/FunctionsFilter.java (revision 576)
@@ -17,6 +17,7 @@
1717 import jp.sf.amateras.functions.utils.RequestUtils;
1818 import jp.sf.amateras.functions.utils.ResponseUtils;
1919 import jp.sf.amateras.functions.utils.StringUtils;
20+import jp.sf.amateras.functions.utils.TextUtils;
2021
2122 /**
2223 * Java Standard EL Functionsを使用する場合はこのフィルタを<tt>web.xml</tt>に登録する必要があります。
@@ -32,7 +33,7 @@
3233 * &lt;url-pattern&gt;*&lt;/url-pattern&gt;
3334 * &lt;dispatcher&gt;REQUEST&lt;/dispatcher&gt;
3435 * &lt;/filter-mapping&gt; </pre>
35- *
36+ *
3637 * このフィルタは以下の処理を行います。
3738 * <ul>
3839 * <li>
@@ -46,7 +47,7 @@
4647 * リクエストごとにJava Standard EL Functionsが提供する暗黙変数を<code>HttpServletRequest</code>の属性にセットします。
4748 * </li>
4849 * </ul>
49- *
50+ *
5051 * このフィルタによって設定される暗黙変数は以下の通りです。
5152 * <table border="1">
5253 * <tr><th>変数名</th><th>説明</th></tr>
@@ -55,22 +56,24 @@
5556 * <td>アプリケーションのコンテキストパス(pageContext.request.contextPathと等価です)</td>
5657 * </tr>
5758 * </table>
58- *
59+ *
5960 * @author Naoki Takezoe
6061 */
6162 public class FunctionsFilter implements Filter {
62-
63+
6364 private static final String DATE_PATTERN = "datePattern";
64-
65+
6566 private static final String DATETIME_PATTERN = "datetimePattern";
66-
67+
6768 private static final String TIME_PATTERN = "timePattern";
68-
69+
6970 private static final String DEFAULT_ENCODING = "defaultEncoding";
70-
71+
72+ private static final String BUNDLE_NAME = "bundleName";
73+
7174 public void destroy() {
7275 }
73-
76+
7477 /**
7578 * <tt>functions.properties</tt>を読み込み、設定内容を反映します。
7679 */
@@ -82,18 +85,19 @@
8285 DateUtils.setDatetimePattern(properties.getProperty(DATETIME_PATTERN));
8386 DateUtils.setTimePattern(properties.getProperty(TIME_PATTERN));
8487 StringUtils.setDefaultEncoding(properties.getProperty(DEFAULT_ENCODING));
88+ TextUtils.setBundleName(properties.getProperty(BUNDLE_NAME));
8589 }
8690 }
8791
8892 public void doFilter(ServletRequest request, ServletResponse response,
8993 FilterChain chain) throws IOException, ServletException {
90-
94+
9195 RequestUtils.setRequest((HttpServletRequest) request);
9296 ResponseUtils.setResponse((HttpServletResponse) response);
93-
97+
9498 // コンテキストパスを$contextという変数名でセット
9599 request.setAttribute("context", ((HttpServletRequest) request).getContextPath());
96-
100+
97101 try {
98102 chain.doFilter(request, response);
99103 } finally {
Show on old repository browser