升级到最新版本时 Spring Boot 国际化不起作用

Posted

技术标签:

【中文标题】升级到最新版本时 Spring Boot 国际化不起作用【英文标题】:Spring boot internationalization not working when upgrading to latest version 【发布时间】:2016-05-24 17:02:03 【问题描述】:

在将我的 Spring Boot 版本升级到最新版本时遇到了一个小问题。未检测到我的 message.properties 并且未加载国际化但是当我在 1.3.0.RC1 版本上运行时一切正常并且我的消息加载。我没有更改 application.properties 的内容或消息本身的位置。我在这个主题上做了很多谷歌搜索,但从未找到答案。我当前的 application.properties 如下所示:

spring.thymeleaf.cache=false
server.session.cookie.http-only=true
spring.resources.cache-period=0
spring.application.name=Cardinal
spring.messages.basename=internationalization/base
spring.mvc.locale=sv_SE
spring.messages.fallback-to-system-locale=true
spring.messages.encoding=UTF-8
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.thymeleaf.mode=html5
spring.thymeleaf.encoding=UTF-8

如您所见,它们位于一个名为 Internationalization 的文件夹中,该文件的前缀为 base。我不知道该怎么做,非常感谢您的帮助。祝各位程序员愉快,愿代码永远对您有利。

【问题讨论】:

您是否依赖消息源的自动配置?如果是这样,现在必须使用名为 messages.properties 的文件来启用自动配置。我找不到发行说明,但我从这里的其他问题中回忆起这个问题。如果您只有 messages_sv.properties 等特定于语言环境的文件,则必须创建默认文件。 在发布公告中提到:spring.io/blog/2016/01/22/spring-boot-1-3-2-released。对于您没有特定翻译的语言环境中的任何人,将默认的 messages.properties 作为后备总是一个好主意 啊哈,我明白了。我将对此进行测试并报告,非常感谢你们。 工作就像一个魅力谢谢你们俩 @WheelchairGeek 只是为了确保 - 您在“国际化”目录中创建了一个 base.properties 文件来解决这个问题,对吗? 【参考方案1】:

我设法通过在这篇博文https://spring.io/blog/2016/01/22/spring-boot-1-3-2-released 和@Andy Wilkinson 中提到的默认messages.properties 作为后备来解决这个问题我的消息配置如下所示:

messages.properties:

index.page.title=Welcome
page.brand=Cardinal
signin.header=Please sign in
signin.email.placeholder=Email
signin.password.placeholder=Password
signin.button.text=Sign in
forgot.password.link.text=Forgot password
rememberme.checkbox.text=Remember me
navbar.header.loginbutton.text=Sign in

messages_sv.properties:

index.page.title=Välkommem
page.brand=Cardinal
signin.header=Vänligen logga in
signin.email.placeholder=E-postadress
signin.password.placeholder=Lösenord
signin.button.text=Logga in
forgot.password.link.text=Glömt Lösenord?
rememberme.checkbox.text=Kom ihåg mig
navbar.header.loginbutton.text=Logga in

和application.yml

 server:
  context-path: /Cardinal
  session:
    cookie:
      http-only: true
  tomcat:
    uri-encoding: UTF-8
  port: 8082


spring:
  thymeleaf:
    cache: false
    mode: HTML5
    encoding: UTF-8
  resources:
    cache-period: 0
  application:
    name: Cardinal
  messages:
    basename: internationalization/messages
    encoding: utf-8
  mvc:
    locale: sv_SE
  http:
    encoding:
      charset: UTF-8
debug: true

希望这可以帮助遇到类似问题的人。

【讨论】:

以上是关于升级到最新版本时 Spring Boot 国际化不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 接入支付宝,实战来了!

Spring Boot 接入支付宝,实战来了!

Spring Boot 接入支付宝,实战来了!

Spring Boot 接入支付宝,实战来了!

Spring Boot 2.0干货系列:Spring Boot1.5X升级到2.0指南

spring cloud: 升级到spring boot 2.x/Finchley.RELEASE遇到的坑