开发者

Making appliication internationnalize using jstl tag

I am trying to Internationalize my application using JSTL tag in JSP. My problem is, the content of properties file are not visible in UI.

JSP CODE

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
This Example demonstrates the basic JSTL formating tags:
<br/><br/>
Locale from client:

<b><c:out value="${pageContext.request.locale}"/></b><br/>

<fmt:setBundle basename="ApplicationResources" var="mybundle"/>
<fmt:message key="welcome.message" bundle="${mybundle}">
   <fmt:param value="${param.uname}"></fmt:param>
</fmt:message>
<b>Now testing &lt;fmt:setLocale&gt;tag:</b><br>
Creating a ResourceBundle with client locale and setting it to <i>mybundle1</i>
variable.<br>
<fmt:setBundle basename="ApplicationResources" var="mybundle1"/>
Setting the locale to&开发者_如何学Golt;i>it</i>(italian).<br/>
<fmt:setLocale value="it"/>
Creating a ResourceBundle with <i>it</i>(italian) locale and setting it to <i>mybundle2</i> variable.<br><br>
<fmt:setBundle basename="ApplicationResources" var="mybundle2"/>
<b>Message using <i>mybundle1</i>:</b><br>
<pre>
 <fmt:message bundle="${mybundle1}" key="welcome.message">

 </fmt:message>
</pre>
<br>
<b>Message using <i>mybundle2</i>:</b><br>
<pre>
<fmt:message bundle="${mybundle2}" key="welcome.message">

</fmt:message>
</pre>
</body>
</html>

The rendered output is:

This Example demonstrates the basic JSTL formating tags:

Locale from client: en_US
???welcome.message??? Now testing <fmt:setLocale>tag:
Creating a ResourceBundle with client locale and setting it to mybundle1 variable.
Setting the locale toit(italian).
Creating a ResourceBundle with it(italian) locale and setting it to mybundle2 variable.

Message using mybundle1:

 ???welcome.message???


Message using mybundle2:

???welcome.message???


I believe this is what we need to start with. Create two files named:

  1. ApplicationResources.properties
  2. ApplicationResources_it.properties

Both should contain (at least) similar entry:

welcome.message=Hello,{0}

Remove these unnecessary declarations:

<fmt:setBundle basename="ApplicationResources" var="mybundle1"/>
<fmt:setBundle basename="ApplicationResources" var="mybundle2"/>

Move <fmt:setLocale value="it"/> to the top of your page. It will set the locale for the entire page either way, I don't think there is a way to mix languages (thank God).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜