将 spring 语言环境变量传递给 application.properties [英] Pass spring locale variable to application.properties

查看:49
本文介绍了将 spring 语言环境变量传递给 application.properties的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 application.properties 文件中放置了一些 URL.现在这些 URL 需要有语言,以便页面以不同的语言加载.例如,在我的 application.properties 文件中,我有一个联系我们"链接的属性,如下所示

There are URLs I have put in my application.properties file. Now these URLs need to have the language so that the page loads in different languages. For example in my application.properties file I have a property for the Contact Us link as below

contact_us_link=https://my-domain.com/{locale}/contact-us

contact_us_link=https://my-domain.com/{locale}/contact-us

在上面的链接中我需要使用当前的应用程序语言环境,这样如果语言环境是en",上面的属性就会变成

In the above link I need to use the current application locale so that if the locale is "en", the above property will become

contact_us_link=https://my-domain.com/en/联系我们

contact_us_link=https://my-domain.com/en/contact-us

如何在属性文件中使用我的语言环境变量?

How can I use my locale variable in the property file?

PS:上面的属性会在 Thymeleaf 中访问,如下图

PS: The above property will be accessed in Thymeleaf as shown below

<li>
<a th:href="@{${@environment.getProperty('contact_us_link')}}" th:text="#{footer.contactUs.text}">Contact Us</a>
</li>

推荐答案

对于其他 stackoverflow 用户,我从 Thymeleaf 文档中找到了上述问题的简单解决方案.

For the fellow stackoverflow users, I found a simple solution for the above question from the Thymeleaf documentation.

在我的 application.properties 文件中,我使用了如下属性

In my application.properties file I used the property as below

contact_us_link=https://my-domain.com/{locale}/contact-us

contact_us_link=https://my-domain.com/{locale}/contact-us

在我的 html ${@environment.getProperty('contact_us_link')} 中从应用程序属性文件中获取 url,另外我传递与 {locale}<对应的值/strong> 在如下所示的 url 中

In my html ${@environment.getProperty('contact_us_link')} to get the url from the application properties file and additionally I pass the value corresponding to the {locale} in the url as shown below

<li>
<a th:href="@{${@environment.getProperty('contact_us_link')}(locale=${locale.toLowerCase()})}" th:text="#{footer.contactUs.text}">Contact Us</a>
</li>

我可以使用 Thymeleaf 模板中的 ${locale} 变量访问当前语言环境.

I can access the current locale using ${locale} variable in Thymeleaf templates.

以下是官方文档的链接

https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#link-urls

这篇关于将 spring 语言环境变量传递给 application.properties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆