从javascript和HTML中的ResourceBundle中获取字符串
我在我们的网站上使用不同的语言:从javascript和HTML中的ResourceBundle中获取字符串,javascript,java,html,jsp,Javascript,Java,Html,Jsp,我在我们的网站上使用不同的语言: Locale locale2 = (Locale)session.getAttribute("org.apache.struts.action.LOCALE"); ResourceBundle bundle = ResourceBundle.getBundle("content.test.Language", locale2); 我可以通过以下方式轻松访问HTML中ResourceBundle的字符串值,以将其包含在网站中: <%= bundle.get
Locale locale2 = (Locale)session.getAttribute("org.apache.struts.action.LOCALE");
ResourceBundle bundle = ResourceBundle.getBundle("content.test.Language", locale2);
我可以通过以下方式轻松访问HTML中ResourceBundle的字符串值,以将其包含在网站中:
<%= bundle.getString("line1") %>
到目前为止,这是有效的,但我不喜欢它
我相信会有更好的解决方案。在
jsp
文件中使用scriplets
通常是不好的做法
您可以使用jstl核心库中的fmt
标记从资源包中获取信息
<fmt:bundle basename="bundle">
<fmt:message var="variableName" key="bundleKey" />
</fmt:bundle>
<input type="hidden" name="hiddenLine2" id="hiddenLine2" value="${variableName}">
一些可能的解决方案
var messageOne='';
var messageTwo='';
根据我的尝试,您可以使用jstl库将翻译后的消息直接打印到JavaScript中,如:
alert("<fmt:message key='line1'/>");
警报(“”);
如果您使用struts2来处理区域设置,那么您可以轻松定义struts2区域设置(由默认堆栈上的i18nInterceptor保存)或用户请求区域设置(客户端的浏览器)
但是,如果您希望将来能够将JavaScript代码提取到外部的.js文件中,我建议您使用一些可用于JavaScript的内部化库,比如(这是我唯一使用过的库,但网上有很多)
使用外部JavaScript库进行国际化的缺点是,您必须直接在.js文件上定义转换资源,无法从基于客户端的语言(如JavaScript)访问服务器上的.properties。这里有一个不同的解决方案
getBundle()
像OP一样加载bundle<%@page import="com.tenea.intranet.conf.Conf" %>
<%@page import="java.util.ResourceBundle,
java.util.Enumeration" %>
<script type="text/javascript">
var _get = function(ID){
if (this.hasOwnProperty(ID)) return this[ID];
else {
console.warn("[Resources] Error al obtener clave <"+ ID +">");
return "[ERROR]";
}
};
var _search = function(text){
var elems = { }
Object.keys(this).map(e => {
if (typeof (this[e]) !== "function" && this[e].includes(text)) { elems[e] = this[e]; }
});
return elems;
};
var Resources = {
<%
ResourceBundle labels = ResourceBundle.getBundle("content.test.Language", locale2);
Enumeration<String> e = labels.getKeys();
while (e.hasMoreElements()) {
String param = e.nextElement();
out.print(param +":\""+ labels.getString(param) +"\"");
if (e.hasMoreElements()) out.println(",");
}
%>
};
Resources._get = _get;
Resources._search = _search;
</script>
为了增加一些额外的功能,我还在参考资料中添加了两个函数
_get()返回与作为参数传递的键相关的文本。如果所述密钥不存在,则返回文本“[ERROR]”
_search()是我为开发目的添加的函数。它搜索并返回一个自定义对象,每个键对应的文本包含作为参数传递的文本。注意:由于它使用“e=>{}”,因此在IE或Safari上不起作用,所以最好在开发阶段结束后对其进行注释
创建此JSP后,要使用它,只需使用以下命令将其导入任何JSP即可:
<%@include file="[relative_path]" %>
希望有帮助!:) Javascript在客户端运行,Java在服务器上运行。如果需要访问客户端上的捆绑内容,则必须将它们嵌入生成的HTML中,并发送到Javascript可以使用它们的客户端。
<script type="text/javascript">
var messageOne = '<%=bundle.getString("line1") %>';
var messageTwo = '<%=bundle.getString("line2") %>';
</script>
alert("<fmt:message key='line1'/>");
<!-- //Import the requierd libraries -->
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- //Take the Locale from struts if it's present and from the user request if not -->
<c:set var="locale" value="${not empty sessionScope.WW_TRANS_I18N_LOCALE
? sessionScope.WW_TRANS_I18N_LOCALE : pageContext.request.locale}"/>
<!-- //Get the bundle based on the Locale -->
<fmt:setLocale value="${locale}"/>
<fmt:setBundle basename="content.test.Language"/>
<%@page import="com.tenea.intranet.conf.Conf" %>
<%@page import="java.util.ResourceBundle,
java.util.Enumeration" %>
<script type="text/javascript">
var _get = function(ID){
if (this.hasOwnProperty(ID)) return this[ID];
else {
console.warn("[Resources] Error al obtener clave <"+ ID +">");
return "[ERROR]";
}
};
var _search = function(text){
var elems = { }
Object.keys(this).map(e => {
if (typeof (this[e]) !== "function" && this[e].includes(text)) { elems[e] = this[e]; }
});
return elems;
};
var Resources = {
<%
ResourceBundle labels = ResourceBundle.getBundle("content.test.Language", locale2);
Enumeration<String> e = labels.getKeys();
while (e.hasMoreElements()) {
String param = e.nextElement();
out.print(param +":\""+ labels.getString(param) +"\"");
if (e.hasMoreElements()) out.println(",");
}
%>
};
Resources._get = _get;
Resources._search = _search;
</script>
Resources {
abrilAbrText: "Apr"
abrilText: "April"
...
}
<%@include file="[relative_path]" %>