?
本文檔使用 PHP中文網(wǎng)手冊 發(fā)布
<fmt:setLocale>標(biāo)簽用來將給定的區(qū)域存儲在locale配置變量中。
<fmt:setLocale>標(biāo)簽有如下屬性:
屬性 | 描述 | 是否必要 | 默認(rèn)值 |
---|---|---|---|
value | 指定ISO-639 語言碼和ISO-3166 國家碼 | 是 | en_US |
variant | 特定瀏覽器變體 | 否 | 無 |
scope | Locale配置變量的作用域 | 否 | Page |
資源束包含區(qū)域特定對象。資源束包含鍵值對。當(dāng)您的程序需要區(qū)域特定資源時,可以將所有的關(guān)鍵詞對所有的locale共享,但是也可以為locale指定轉(zhuǎn)換后的值。資源束可以幫助提供指定給locale的內(nèi)容。
一個Java資源束文件包含一系列的鍵值對。我們所關(guān)注的方法涉及到創(chuàng)建繼承自java.util.ListResourceBundle 類的已編譯Java類。您必須編譯這些類然后放在您的Web應(yīng)用程序的CLASSPATH中。
讓我們來定義一個默認(rèn)的資源束:
package?com.tutorialspoint; 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"}, ??}; }
現(xiàn)在,再定義一個資源束,用于Spanish Locale:
package?com.tutorialspoint; 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"}, ??}; }
編譯以上文件至Examble.class和Examble_es_ES.class中,然后將它們放在Web應(yīng)用程序的CLASSPATH中?,F(xiàn)在可以使用JSTL標(biāo)簽來顯示這三個數(shù)字,就像這樣:
<%@?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.tutorialspoint.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.tutorialspoint.Example"> ???<fmt:message?key="count.one"/><br/> ???<fmt:message?key="count.two"/><br/> ???<fmt:message?key="count.three"/><br/> </fmt:bundle> </body> </html>
運行結(jié)果如下:
One? Two? Three Uno Dos Tres
查看<fmt:bundle>和<setBundle>來獲取更多信息。
關(guān)于我們 聯(lián)系我們 留言板
手冊網(wǎng)