首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在百里叶中动态添加JavaScript资源

问在百里叶中动态添加JavaScript资源
EN

Stack Overflow用户
提问于 2020-10-01 21:41:49
回答 1查看 157关注 0票数 1

我有timeago.js插件,这需要一些文件的地区。但您不能一次添加所有内容。它应该是相对当前的区域设置。所以我需要一些像switch或if条件这样的东西来添加资源。我试过了,但不起作用。

代码语言:javascript
复制
<script th:if="${#locale == 'az_AZ'}" th:src="@{/resources/jquery/js/jquery.timeago.az.js}" type="text/javascript"></script>
<script th:if="${#locale == 'ru_RU'}" th:src="@{/resources/jquery/js/jquery.timeago.ru.js}" type="text/javascript"></script>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-01 22:11:32

我建议使用#locale.language只获取上下文语言环境的语言部分-特别是因为JS文件名需要匹配语言部分。

所以,就像这样:

代码语言:javascript
复制
<script th:inline="javascript" 
        th:if="${#locale.language == 'en'}"  
        th:src="@{/resources/jquery/js/jquery.timeago.en.js}" 
        type="text/javascript"></script>
<script th:inline="javascript" 
        th:if="${#locale.language == 'ru'}"  
        th:src="@{/resources/jquery/js/jquery.timeago.ru.js}" 
        type="text/javascript"></script>

但是您可以更进一步,通过在您的src #locale.language中直接使用该URL值来简化此过程:

代码语言:javascript
复制
<script th:inline="javascript" 
        th:src="@{/resources/jquery/js/jquery.timeago.{lang}.js(lang=${#locale.language})}" 
        type="text/javascript"></script>

现在,不需要使用任何th:if语句,并且只需要一个<script>标记,而不是可能有一长串标记。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64156655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档