我正在尝试国际化(i18n)基于基本 JSP / Servlets 的Java Web应用程序。
我正在将 JSTL fmt taglib
与Resource Bundle
结合使用来实现我的应用程序的 i18n 。我也在关注this回答/指南。
当我在某个页面选择所需语言例如 index.jsp
时,此页面会根据所选语言进行翻译,但当我按某个链接进行时< strong>例如 signup.jsp
然后语言更改为默认语言,因此i18n可以单独处理每个页面。
问题是如何在所有网页上分享语言价值的?
首先,我有一个过滤器,它指定了页面的默认/初始语言。
DefaultLocaleFilter.java
@WebFilter(filterName = "DefaultLocaleFilter", urlPatterns = {"/app/*"})
public class DefaultLocaleFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletResponse resp = (HttpServletResponse) response;
HttpServletRequest req = (HttpServletRequest) request;
req.getSession().setAttribute("language", "ru");
chain.doFilter(request, response);
}
@Override
public void destroy() {}
}
的index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="language" value="${not empty param.language ? param.language : not empty language ? language : pageContext.request.locale}" scope="session" />
<fmt:setLocale value="${language}"/>
<fmt:setBundle basename="i18n.lang"/>
<!DOCTYPE html>
<html lang="${language}">
<head>
<title><fmt:message key="index.title"/></title>
</head>
<body>
<h3 class="hero"><fmt:message key="index.text.welcome"/></h3>
<p><fmt:message key="index.text.overview"/></p><br>
<a class="button" href="signup"><fmt:message key="index.button.signup"/></a>
<a class="button" href="login"><fmt:message key="index.button.login"/></a>
<span class="lang">
<form>
<select name="language" onchange="submit()">
<option value="en" ${language == 'en' ? 'selected' : ''}>EN</option>
<option value="ru" ${language == 'ru' ? 'selected' : ''}>RU</option>
</select>
</form>
</span>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="language" value="${not empty param.language ? param.language : not empty language ? language : pageContext.request.locale}" scope="session" />
<fmt:setLocale value="${language}"/>
<fmt:setBundle basename="i18n.lang"/>
<!DOCTYPE html>
<html lang="${language}">
<head>
<title><fmt:message key="signup.title"/></title>
</head>
<body>
<h3 class="hero"><fmt:message key="signup.text.welcome"/></h3>
<%-- Here is the form to Sign Up --%>
<span class="lang">
<form>
<select name="language" onchange="submit()">
<option value="en" ${language == 'en' ? 'selected' : ''}>EN</option>
<option value="ru" ${language == 'ru' ? 'selected' : ''}>RU</option>
</select>
</form>
</span>
</body>
</html>
答案 0 :(得分:1)
您的过滤器会在每次请求时覆盖所选语言。
只有在用户未选择任何语言时,才应设置默认语言。
class CustomAdmin(admin.ModelAdmin):
model = YourModel
list_display = ['your_large_text_field__custom_rendering']
def your_large_text_field__custom_rendering(self, obj):
return "<pre>%s</pre>" % (obj.your_large_text_field,)
your_large_text_field__custom_rendering.allow_tags = True
admin.site.register(YourModel, CustomAdmin)
但是,最好只删除servlet过滤器并将if (req.getSession().getAttribute("language") == null) {
req.getSession().setAttribute("language", "ru");
}
中的pageContext.request.locale
替换为<c:set var="language">
。
'ru'