Pass spring locale variable to application.properties

牧云@^-^@ 提交于 2021-02-10 12:06:18

问题


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

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

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

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>

回答1:


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

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

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

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>

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

Below is the link to the official documentation

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




回答2:


Rather than using application.properties for this, you should probably use internationalization (i18n) using message bundles (messages.properties).

If you have a limited amount of languages, you can create a separate properties file for each language, for example:

  • messages.properties: Default/fallback
  • messages_en.properties: English translated properties
  • messages_de.properties: German translated properties

If each of these files contains a contact_us_link property, Spring will automatically resolve them. In your Thymeleaf template you should use the following:

<small th:text="#{contact_us_link}"></small>

Alternatively, if you need a more dynamic approach, you can remove the property from all your localized message_*.properties files and use a placeholder within the messages.properties file, for example:

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

Now you can pass the locale as a substitution in your template:

<a th:href="#{contact_us_link(locale)}">Contact us</a>

If you really need to use application.properties, you'll have to do the substitution by yourself. Luckily, you can use everything SpEL has to offer within your template, so you could create your own bean:

@Component
public class Formatter {
    public String format(String message, Object... variables) {
        return MessageFormat.format(message, variables);
    }
}

And now you could substitute it using this bean:

<a th:href="${@formatter.format(@environment.getProperty('contact_us_link'), #locale)}">Contact us</a>



回答3:


I don't think that spring will provide/translate locale while fetching property in thymeleaf. Rather you can make different keys for each locale and fetch them accordingly.

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

Use ${#locale} while fetching property in thymeleaf.



来源:https://stackoverflow.com/questions/50720604/pass-spring-locale-variable-to-application-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!