java 如何在百里香叶中包含 message.properties

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30106659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:29:33  来源:igfitidea点击:

How to include message.properties with thymeleaf

javaspringspring-bootthymeleaf

提问by osagie

I am using spring boot with thymeleaf. This is my project structure: enter image description here

我正在使用带有百里香叶的弹簧靴。这是我的项目结构: 在此处输入图片说明

And this is my App start class:

这是我的应用程序启动类:

    @EnableAutoConfiguration
@Configuration
@ComponentScan
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class);
    }
}

I have this on my home.leaf.html: <p th:text = "#{username}"></p>

我的 home.leaf.html 上有这个: <p th:text = "#{username}"></p>

But when I run this application this is what I get: ??username_en_US??

但是当我运行这个应用程序时,我得到的是:??username_en_US??

I have tried various things on how to resolve this configuration issue. Please, can anyone help?

我已经尝试了各种有关如何解决此配置问题的方法。拜托,有人可以帮忙吗?

回答by Faraj Farook

Refer the official documentation for spring boot

spring boot参考官方文档

http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-convert-an-existing-application-to-spring-boot

http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-convert-an-existing-application-to-spring-boot

It says

它说

Static resources can be moved to /public(or /staticor /resourcesor /META-INF/resources) in the classpath root. Same for messages.properties(Spring Boot detects this automatically in the root of the classpath).

静态资源可以移动到/public(或/static/resources/META-INF/resources)类路径根目录中。相同 messages.properties(Spring Boot 在类路径的根目录中自动检测到这一点)。

So you should create ur internationalization file as messages.propertiesand place in the root classpath.

所以你应该创建你的国际化文件messages.properties并将其放在根类路径中。

Or you can also edit the default location to a more proper location by adding this entry in the application.propertiesfile

或者您也可以通过在application.properties文件中添加此条目来将默认位置编辑为更合适的位置

#messages
spring.messages.basename=locale/messages

so you can store your files in the locale folder inside resources folder, with the name messages.propertiesor in any specific language.

因此您可以使用名称messages.properties或任何特定语言将文件存储在资源文件夹内的语言环境文件夹中。