当resource bundle 的多语言文件里包含引号'时

x33g5p2x  于9个月前 转载在 其他  
字(2.8k)|赞(0)|评价(0)|浏览(357)

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

背景

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

例子

resources下包含三个语言文件

  1. 分别是:
  2. bundle_zh_CN.properties
  3. hello=你好吗?{0}
  4. bundle_zh_TW.properties
  5. hello=你好嗎?{0}
  6. bundle_en.properties
  7. hello=how are you ? {0}
  1. 测试类:
  2. public class Main {
  3. public static void main(String[] args) throws UnsupportedEncodingException {
  4. System.out.println(getMessage("hello", new Object[] {"辉"}, Locale.CHINA));
  5. System.out.println(getMessage("hello", new Object[] {"輝"}, Locale.TRADITIONAL_CHINESE));
  6. System.out.println(getMessage("hello", new Object[] {"hui"}, Locale.ENGLISH));
  7. }
  8. public static String getMessage(String code, Object[] args, Locale locale) {
  9. ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
  10. messageSource.setDefaultEncoding(StandardCharsets.UTF_8.name());
  11. messageSource.setBasename("bundle");
  12. messageSource.setCacheSeconds(1800);
  13. return messageSource.getMessage(code, args, locale);
  14. }
  15. }
  16. 输出:
  17. 你好吗?辉
  18. 你好嗎?輝
  19. how are you ? hui
  20. 可以看出没什么问题

如果含有引号 '

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

原因

  1. MessageFormat messageFormat = resolveCode(code, locale);
  2. if (messageFormat != null) {
  3. synchronized (messageFormat) {
  4. return messageFormat.format(argsToUse);
  5. }
  6. }
  7. 追踪源码,底层是由Java底层的MessageFormat来实现的
  8. API中解释如下:
  9. Within a String, a pair of single quotes can be used to quote any arbitrary characters except single quotes.
  10. For example, pattern string "'{0}'" represents string "{0}", not a FormatElement.
  11. A single quote itself must be represented by doubled single quotes '' throughout a String.
  12. 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 "{}".
  13. Any unmatched quote is treated as closed at the end of the given pattern. For example, pattern string "'{0}" is treated as pattern "'{0}'".
  14. 大概意思就是:
  15. - 两个单引号里面的值保持不变,不会被格式化
  16. - 如果想输出单引号,使用两个单引号'' 来输出单引号'
  17. - 如果只有一个单引号,那么单引号后面的值就原封不动的输出来,即不会被格式化

修改

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

相关文章