关于java:Apostrophe放在资源束中时无法正确翻译

Apostrophe doesn't get translated properly when placed in a resource bundle

将Apostrophe放在资源包中后,翻译不正确。

1
2
3
key = {0}'s brush is {1} centimeters tall

(e.g. Sam'
s brush is 4 centimeters tall)

如果我从java.util.ResourceBundle格式化上述密钥,则撇号将丢失。这可能是什么问题?


您应该将单引号转义为

1
key = {0}''s brush is {1} centimeters tall


我坚信问题不是资源束,而是用于打印消息的MessageFormater:

从MessageFormater Java文档:

Within a String, '' (two single quotes ) represents a
single quote. A QuotedString can
contain arbitrary characters except
single quotes; the surrounding single
quotes are removed. An UnquotedString
can contain arbitrary characters
except single quotes and left curly
brackets. Thus, a string that should
result in the formatted message
'{0}' can be written as '''{'0}''
or '''{0}'''.

所以你需要写:

1
{0}''s brush is {1} centimeters tall

在这里看javadoc

Within a String,"''" represents a
single quote. A QuotedString can
contain arbitrary characters except
single quotes; the surrounding single
quotes are removed. An UnquotedString
can contain arbitrary characters
except single quotes and left curly
brackets. Thus, a string that should
result in the formatted message
"'{0}'" can be written as"'''{'0}''"
or"'''{0}'''".


如果您像我一样完全卡住了(上述方法均无效),则可以将单引号替换为其Unicode:\ u0027。请记住,始终允许您在属性文件中使用UTF符号。


添加到@Ralph的答案:
当您有类似这样的文本时,您将意识到这是MessageFormat事情

1
text1=It's too late

1
text2={0}''s too late

text1可能不会通过MessageFormater运行(例如,如果传递或不传递参数,spring的代码路径都不同),而text2则不会。
因此,如果您在text1中使用了两个单引号,则它们可能会/会这样显示。因此,您需要检查是否有任何参数格式化了,并相应地使用了一个或两个单引号。


您需要将单引号加倍,即{0}的画笔高{1}厘米


考虑使用Properties Editor插件(对于Eclipse)

http://propedit.sourceforge.jp/index_en.html


对于在string.xml中遇到Android问题的每个人,请使用\'\'而不是单引号。