Jsp-jstl-format-setlocale-tag

提供:Dev Guides
移動先:案内検索

JSTL-コア<fmt:setLocale>タグ

*<fmt:setLocale>* タグは、指定されたロケールをロケール構成変数に格納するために使用されます。

属性

*<fmt:setLocale>* タグには次の属性があります-
Attribute Description Required Default
Value Specifies a two-part code that represents the ISO-639 language code and an ISO-3166 country code. Yes en_US
variant Browser-specific variant No None
scope Scope of the locale configuration variable No Page

リソースバンドルには、ロケール固有のオブジェクトが含まれています。 リソースバンドルにはキー/値のペアが含まれます。 プログラムにロケール固有のリソースが必要な場合、すべてのロケールに共通するすべてのキーを保持しますが、ロケール固有の翻訳された値を持つことができます。 リソースバンドルは、ロケール固有のコンテンツを提供するのに役立ちます。

Javaリソースバンドルファイルには、一連の*キーから文字列へのマッピング*が含まれています。 私たちが焦点を当てる方法は、 java.util.ListResourceBundle クラスを拡張するコンパイル済みJavaクラスの作成を伴います。 これらのクラスファイルをコンパイルし、Webアプリケーションのクラスパスで使用できるようにする必要があります。

次のようにデフォルトのリソースバンドルを定義しましょう-

package com.finddevguides;

import java.util.ListResourceBundle;

public class Example_En extends ListResourceBundle {
   public Object[][] getContents() {
      return contents;
   }
   static final Object[][] contents = {
      {"count.one", "One"},
      {"count.two", "Two"},
      {"count.three", "Three"},
   };
}

スペイン語ロケールに使用するリソースバンドルをもう1つ定義します。

package com.finddevguides;

import java.util.ListResourceBundle;

public class Example_es_ES extends ListResourceBundle {
   public Object[][] getContents() {
      return contents;
   }
   static final Object[][] contents = {
      {"count.one", "Uno"},
      {"count.two", "Dos"},
      {"count.three", "Tres"},
   };
}

上記のクラス Example.class および Example_es_ES.class をコンパイルして、WebアプリケーションのCLASSPATHで使用できるようにします。 これで、次のJSTLタグを使用して、次のように3つの数字を表示できます-

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/fmt" prefix = "fmt" %>

<html>
   <head>
      <title>JSTL fmt:setLocale Tag</title>
   </head>

   <body>
      <fmt:bundle basename = "com.finddevguides.Example">
         <fmt:message key = "count.one"/><br/>
         <fmt:message key = "count.two"/><br/>
         <fmt:message key = "count.three"/><br/>
      </fmt:bundle>

      <!-- Change the Locale -->
      <fmt:setLocale value = "es_ES"/>
      <fmt:bundle basename = "com.finddevguides.Example">
         <fmt:message key = "count.one"/><br/>
         <fmt:message key = "count.two"/><br/>
         <fmt:message key = "count.three"/><br/>
      </fmt:bundle>

   </body>
</html>

上記のコードは、次の結果を生成します-

One
Two
Three
Uno
Dos
Tres

link:/jsp/jstl_format_bundle_tag [<fmt:bundle>]およびlink:/jsp/jstl_format_setbundle_tag [<setBundle>]タグを確認して、完全な概念を理解してください。