在使用MessageFormat进行国际化时发现pattern如果是例如:
Don't support get file which path:{0}
这样包含单引号时,后面的{0}无法正常显示
百度后无果。。。
最后通过阅读MessageFormat的源代码和注释找到了解决方案
applyPattern方法会将读到的第一个英文单引号当做字符串的开始,也就是当成了转义字符,然后如果后面没有单引号出现就会把后面都当做字符串处理。在注释中有给出解决方案:
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 "{}".
当必须使用单引号时需要使用两个单引号来使用,最终还是会显示一个单引号
百度不是万能,多看源码