背景

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

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

  1. 【转】python删除文件里包含关键词的行

    import shutil with open('/path/to/file', 'r') as f: with open('/path/to/file.new', 'w') as g: for li ...

  2. apk去广告工具(利用apktool去除apk文件里的广告)

    基本知识 apk安装包的文件结构 以知名桌面软件“LauncherPro”为例,apk安装包文件目录: 文件目录如下: - META-INF - res - anim - color - drawab ...

  3. [转]Windows中的命令行提示符里的Start命令执行路径包含空格时的问题

    转自:http://www.x2009.net/articles/windows-command-line-prompt-start-path-space.html 当使用Windows 中的命令行提 ...

  4. C++头文件的包含顺序研究

    一.<Google C++ 编程风格指南>里的观点 公司在推行编码规范,领导提议基本上使用<Google C++ 编程风格指南>.其中<Google C++ 编程风格指南 ...

  5. 转:Windows中的命令行提示符里的Start命令执行路径包含空格时的问题

    转自:http://www.x2009.net/articles/windows-command-line-prompt-start-path-space.html 当使用Windows 中的命令行提 ...

  6. Spring resource bundle多语言,单引号format异常

    Spring resource bundle多语言,单引号format异常 前言 十一假期被通知出现大bug,然后发现是多语言翻译问题.法语中有很多单引号,单引号在format的时候出现无法匹配问题. ...

  7. Windows Store App下代码加载page resource和resw文件里的string

    加载page resource 在page的code behind里: this.Resources["textBoxStyle"] 加载resw文件里的string: Resou ...

  8. C语言学习_C如何在一个文件里调用另一个源文件中的函数

    问题 C如何在一个文件里调用另一个源文件中的函数,如题. 解决办法 当程序大了代码多了之后,想模块化开发,不同文件中存一点,是很好的解决办法,那我们如何做才能让各个文件中的代码协同工作呢?我们知道,m ...

  9. 教你如何在 Javascript 文件里使用 .Net MVC Razor 语法

    摘录 文章主要是介绍了通过一个第三方类库RazorJS,实现Javascript 文件里使用 .Net MVC Razor 语法,很巧妙,推荐给大家 相信大家都试过在一个 View 里嵌套使用 jav ...

  10. c语言文件读写操作总结

    C语言文件读写操作总结 C语言文件操作 一.标准文件的读写 1.文件的打开 fopen() 文件的打开操作表示将给用户指定的文件在内存分配一个FILE结构区,并将该结构的指针返回给用户程序,以后用户程 ...

随机推荐

  1. KingbaseES R3 集群修改system用户密码方案

    方案说明: 对于kingbaseES R3集群修改system密码相比单机环境有一定的复杂性,需要修改的位置如下: 1)数据库中system用户密码,可以用alter user命令修改 2)在reco ...

  2. 超详细的格式化输出(format的基本玩法)

    一.format的基本玩法 一.什么是format format是字符串内嵌(字符串内嵌:字符串中再嵌套字符串,加入双引号或单引号)的一个方法,用于格式化字符串.以大括号{}来标明被替换的字符串 fo ...

  3. Java 函数式编程

    由 JS 转 Java,写惯了 React,习惯了函数式,因此转 Java 时也是先学函数式. 语法糖「Syntactic Sugar」 起初,Java 的函数式看起来是匿名类的一个语法糖. Stre ...

  4. Traefik 控制面板 SaaS 服务 Pilot

    文章转载自:https://mp.weixin.qq.com/s?__biz=MzU4MjQ0MTU4Ng==&mid=2247485572&idx=1&sn=8ffa2bc7 ...

  5. Kubernetes(k8s)为容器设置启动时要执行的命令和参数

    创建 Pod 时设置命令及参数 创建 Pod 时,可以为其下的容器设置启动时要执行的命令及其参数.如果要设置命令,就填写在配置文件的 command 字段下,如果要设置命令的参数,就填写在配置文件的 ...

  6. Ubuntu20.04本地安装Redash中文版

    一.安装基础环境: # 1.更换APT国内源 sudo sed -i s@/cn.archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources ...

  7. PAT (Basic Level) Practice 1026 程序运行时间 分数 15

    要获得一个 C 语言程序的运行时间,常用的方法是调用头文件 time.h,其中提供了 clock() 函数,可以捕捉从程序开始运行到 clock() 被调用时所耗费的时间.这个时间单位是 clock ...

  8. Linux命令系列之top——里面藏着很多鲜为人知的宝藏知识

    Linux命令系列之top--里面藏着很多鲜为人知的宝藏知识 简介 top命令是我们经常用来查看系统信息的一个指令,它提供了一个动态的而且是实时的借口帮助我们去查看系统执行时的进程.线程和系统参数的信 ...

  9. P1886 滑动窗口 /【模板】单调队列 方法记录

    原题链接 滑动窗口 /[模板]单调队列 题目描述 有一个长为 \(n\) 的序列 \(a\),以及一个大小为 \(k\) 的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最 ...

  10. FluentValidation 验证(二):WebApi 中使用 注入服务

    比如你要验证用户的时候判断一下这个用户名称在数据库是否已经存在了,这时候FluentValidation 就需要注入查询数据库 只需要注入一下就可以了 public class Login3Reque ...