获取脚本上的用户区域设置

时间:2012-12-18 17:04:16

标签: google-maps jsf primefaces

我有一点问题。我在primefaces中使用gmap,我们需要加载脚本

<script src="http://maps.google.com/maps/api/js?sensor=true" type="text/javascript"/>

但是我需要根据用户区域设置的语言加载脚本。

如果没有“硬编码”字符串,我怎么能设法做到这一点?

我试过这样的事情:

    <script src="http://maps.google.com/maps/api/js?sensor=true&amp;language="#{template.userLocale} type="text/javascript"/>
// {template.userLocale} has a string o the locale

你能帮帮忙吗?

1 个答案:

答案 0 :(得分:1)

你有HTML语法错误。鉴于en语言

,您最终得到的结果将是这样的
<script src="http://maps.google.com/maps/api/js?sensor=true&amp;language="en type="text/javascript"/>

(在浏览器中右键单击页面,然后自己查看源代码)

您需要将双引号移动到属性值的末尾:

<script src="http://maps.google.com/maps/api/js?sensor=true&amp;language=#{template.userLocale}" type="text/javascript"/>

这样HTML最终会成为:

<script src="http://maps.google.com/maps/api/js?sensor=true&amp;language=en" type="text/javascript"/>

EL只能用于模板文本。您需要意识到JSF基本上生成HTML代码。 HTML <script src>属性和EL #{}不会同步运行。相反,JSF / EL会对其进行处理,您只需要确保生成的HTML在语法上有效。