【问题标题】:Spring MessageSource properties file first line not detected未检测到 Spring MessageSource 属性文件第一行
【发布时间】:2019-08-06 17:57:09
【问题描述】:

我正在使用 Spring 4 并使用 MessageSource 进行本地化。

我的 xml 文件有:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
   <property name="basename" value="classpath:i18n/myfile"/> 
   <property name="defaultEncoding" value="UTF-8"/>
</bean>

其中一个本地化文件 myfile_nl.properties 具有:

prop1=abc
other1=o1
other2=o2
other3=o3

获取本地化字符串的Java代码是:

@Autowired
private MessageSource messageSource;

String localizedMsg = messageSource.getMessage("prop1", null, locale);

现在,我得到了 prop1 未找到的异常。 因此,我编辑了属性文件,以便稍后在文件中复制 prop1。

other1=o1
other2=o2
prop1=abc
other3=o3

现在检测到 prop1。

为什么 prop1 在第一行时没有被检测到?

【问题讨论】:

    标签: java spring


    【解决方案1】:

    您的 UTF-8 属性文件可能以 BOM (Byte Order Mark) 开头。

    由于 Java 不做 BOM,3-byte BOM 被视为不可显示的 BOM 字符,并成为第一行列出的属性名称的一部分,这当然会使其不匹配。

    在文本编辑器中编辑文件,无需 BOM 即可保存文件。

    Notepad++ 就是这样一个文本编辑器的一个例子。见:Remove a BOM character in a file

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      • 1970-01-01
      • 2018-04-18
      • 1970-01-01
      • 2016-01-03
      • 2021-01-26
      • 1970-01-01
      相关资源
      最近更新 更多