背景
项目中使用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 ?
原文地址:http://www.cnblogs.com/eaglelihh/p/16916945.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性