背景

项目中使用Spring的ReloadableResourceBundleMessageSource这个类来实现多语言,有一次字符串里包含引号'时,解析时出了问题,一起来看一下吧

例子

resources下包含三个语言文件

分别是:
bundle_zh_CN.properties
hello=你好吗?{0} bundle_zh_TW.properties
hello=你好嗎?{0} bundle_en.properties
hello=how are you ? {0}
测试类:
public class Main {
public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println(getMessage("hello", new Object[] {"辉"}, Locale.CHINA));
System.out.println(getMessage("hello", new Object[] {"輝"}, Locale.TRADITIONAL_CHINESE));
System.out.println(getMessage("hello", new Object[] {"hui"}, Locale.ENGLISH));
} public static String getMessage(String code, Object[] args, Locale locale) {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setDefaultEncoding(StandardCharsets.UTF_8.name());
messageSource.setBasename("bundle");
messageSource.setCacheSeconds(1800);
return messageSource.getMessage(code, args, locale);
}
} 输出:
你好吗?辉
你好嗎?輝
how are you ? hui 可以看出没什么问题

如果含有引号'

改成:
bundle_zh_CN.properties
hello=你好'吗?{0} bundle_zh_TW.properties
hello=你好'嗎?{0} bundle_en.properties
hello=how are' you ? {0}
输出结果:
你好吗?{0}
你好嗎?{0}
how are you ? {0} 可以看出如果含有引号',参数不起作用,而且引号'也没显示出来

原因

MessageFormat messageFormat = resolveCode(code, locale);
if (messageFormat != null) {
synchronized (messageFormat) {
return messageFormat.format(argsToUse);
}
} 追踪源码,底层是由Java底层的MessageFormat来实现的 API中解释如下:
Within a String, a pair of single quotes can be used to quote any arbitrary characters except single quotes.
For example, pattern string "'{0}'" represents string "{0}", not a FormatElement.
A single quote itself must be represented by doubled single quotes '' throughout a String.
For example, pattern string "'{''}'" is interpreted as a sequence of '{ (start of quoting and a left curly brace), '' (a single quote), and }' (a right curly brace and end of quoting), not '{' and '}' (quoted left and right curly braces): representing string "{'}", not "{}". Any unmatched quote is treated as closed at the end of the given pattern. For example, pattern string "'{0}" is treated as pattern "'{0}'". 大概意思就是:
- 两个单引号里面的值保持不变,不会被格式化
- 如果想输出单引号,使用两个单引号'' 来输出单引号'
- 如果只有一个单引号,那么单引号后面的值就原封不动的输出来,即不会被格式化

修改

改成:
bundle_zh_CN.properties
hello=你好''吗?{0} bundle_zh_TW.properties
hello=你好''嗎?{0} bundle_en.properties
hello=how are'' you ? {0} 输出结果:
你好'吗?辉
你好'嗎?輝
how are' you ? hui
还有一点需要注意的就是:在没有参数的情况下,如果想输出引号,那就用一个引号即可,如下:
bundle_zh_CN.properties
hello=你好'吗? bundle_zh_TW.properties
hello=你好'嗎? bundle_en.properties
hello=how are' you ?
public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println(getMessage("hello", null, Locale.CHINA));
System.out.println(getMessage("hello", null, Locale.TRADITIONAL_CHINESE));
System.out.println(getMessage("hello", null, Locale.ENGLISH));
} 输出:
你好'吗?
你好'嗎?
how are' you ?

最新文章

  1. 详解Maple中的基础工具栏
  2. 如何写出优雅的Python
  3. noip模拟赛 好元素 哈希表的第一题
  4. Linux 文件常见类型
  5. HBase命令(二) -- 表操作
  6. 控制反转(IOC: Inverse Of Control) & 依赖注入(DI: Independence Inject)
  7. iOS-网络请求-AFN升级至AFN3.0
  8. ubuntu修改grub界面默认开机顺序
  9. 关键字 static
  10. Android开发之一些资源索引
  11. Git之路--1
  12. 编写带参数decorator
  13. 51nod 1016 水仙花数
  14. fastdfs-nginx-module-master的一些奇怪的特点
  15. win10 常用设置 桌面出来计算机图标,固定桌面摆好的图标设置方法,电脑设备ID方法
  16. python-selenium,关于页面滑动的操作
  17. 记录Python类与继承的一个错误
  18. 装好Linux后没有声音的看过来
  19. JAVA基础部分面试
  20. Oracle学习笔记(八)

热门文章

  1. .NET 反向代理-YARP
  2. filebeat客户端传输cisco日志到elasticsearch
  3. Ansible_playbook
  4. (数据科学学习手札143)为geopandas添加gdb文件写出功能
  5. Form表单数据
  6. redhat替换yum源时redhat.repo无法删除或禁用的问题
  7. 工作7年收集到的git命令
  8. sql中更换函数REPLACE
  9. PHP全栈开发(八):CSS Ⅲ background
  10. POJ3417 Network暗的连锁 (树上差分)