背景
项目中使用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 ?