我在我的一个Web应用程序中实现了多种语言。 用户选择的语言保存在cookie中。 我已经能够从这个cookie中提取内容,如下所示:
${cookie.userdata.value}我遇到的问题是语言不是我存储在这个cookie中的唯一东西。 当用户上次访问通知时也有一些时间戳。 Cookie的完整值采用JSON格式:
userdata={ "notificationsViewed":1509696860036, "documentNotificationsViewed":1509696859896, "language":"en" }我需要的仅仅是"en" 。
在其他情况下,我使用javascript和JSONParser来获取语言,但我有一种情况,即能够使用JSTL / Expression Language获取语言会非常棒,但我一直无法找到。 到目前为止,我的尝试导致:
<c:set var="language">${cookie.userdata.value.language}</c:set> javax.el.PropertyNotFoundException: The class 'java.lang.String' does not have the property 'language'.我需要以某种方式使用JSON方法,但我甚至不知道它是否可能。 有人知道吗?
I am implementing multi languages in one of my web applications. The language selected by the user, is saved in a cookie. I have been able to extract the contents from this cookie, this way:
${cookie.userdata.value}The problem I am having, is that language is not the only thing I store in this cookie. There are also some timestamps for when the user last has accessed notifications. The full value of the cookie is in JSON format:
userdata={ "notificationsViewed":1509696860036, "documentNotificationsViewed":1509696859896, "language":"en" }What I need, is simply the "en".
In other cases, I use javascript, and JSONParser to get the language, but I have a situation where it would be brilliant to be able to fetch the language using JSTL / Expression Language, but I have been unable to find one. My attempts so far results in:
<c:set var="language">${cookie.userdata.value.language}</c:set> javax.el.PropertyNotFoundException: The class 'java.lang.String' does not have the property 'language'.I need to somehow use a JSON approach, but I don't even know if it is possible. Does anyone know?
最满意答案
在我工作的一个项目中,我做了以下工作:
在Java方面,将语言设置为一个meber变量,可以使用getter在JSP中访问该变量。
您可以使用您设置的Cookie或任何其他方式(用户输入,请求标题...)来执行此操作。
在JSP中,获取该值并将其用于渲染页面之前完成的任何本地化。
然后,作为呈现页面的一部分,包括本地化javascript数组:
... <script> var l10n = { "language" : "${language}" }; </script> </body>通过这种方式,在页面被渲染后,您可以使用语言l10n.language (在JavaScript中)
这或多或少是在另一种情况下,Wordpress处理JavaScript脚本的本地化。
In a project I worked on I did the following:
On the java side set the language to a meber variable that can be accessed in the JSP with a getter.
You can do this using the cookie you set or by any other means (user input, request header, ...)
In the JSP get hold that value and use it for any localization done before rendering the page.
Then as part of the rendered page include a localization javascript array:
... <script> var l10n = { "language" : "${language}" }; </script> </body>In this way, after the page hs been rendered you can access the language as l10n.language (in javascript)
This is more or less how, in another context, Wordpress handles localization of javascript scripts.
更多推荐
language,cookie,语言,The,电脑培训,计算机培训,IT培训"/> <meta name="descri
发布评论