Java 放置在资源包中时,撇号无法正确翻译

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4449639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:40:55  来源:igfitidea点击:

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

javaresourcebundle

提问by user339108

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

放在资源包中时,撇号无法正确翻译。

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

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

The apostrophe gets missed if I format the above key from a java.util.ResourceBundleWhat could be the problem here?

如果我从 a 格式化上面的键,撇号会被遗漏java.util.ResourceBundle这里可能是什么问题?

采纳答案by Raghuram

You should escape the single quote as

您应该将单引号转义为

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

回答by Ralph

I strongly belive that the problem is not the ressource bundle but the MessageFormateryou use to print the message:

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

From MessageFormater java doc:

从 MessageFormater java doc

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}'''.

在字符串中,''(两个单引号)代表一个单引号。QuotedString 可以包含除单引号之外的任意字符;周围的单引号被删除。UnquotedString 可以包含除单引号和左大括号之外的任意字符。因此,应生成格式化消息“{0}”的字符串可以写为“''{'0}''或'''{0}'''。

So you need to write:

所以你需要写:

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

回答by Samuel Parsonage

You need to double single quote i.e. {0}''s brush is {1} centimeters tall

你需要双单引号,即{0}的画笔是{1}厘米高

回答by Aravind Yarram

Look at the javadoc here

看看这里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}'''".

在字符串中,“''”代表单引号。QuotedString 可以包含除单引号之外的任意字符;周围的单引号被删除。UnquotedString 可以包含除单引号和左大括号之外的任意字符。因此,应生成格式化消息“'{0}'”的字符串可以写为“'''{'0}''”或“'''{0}'''”。

回答by Danubian Sailor

Consider using Properties Editor plugin (for Eclipse)

考虑使用属性编辑器插件(用于 Eclipse)

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

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

回答by Uriel Frankel

For everyone that has Android problems in the string.xml, use \'\' instead of single quote.

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

回答by sorrymissHymanson

Adding to @Ralph's answer: You will realize that this is a MessageFormatthing when you have a text like

添加到@Ralph 的回答中:MessageFormat当您有类似的文本时,您会意识到这是一件事

text1=It's too late

versus

相对

text2={0}''s too late

text1would probably not run through a MessageFormater (e.g. spring has different code paths if arguments are passed or not), whereas text2would. So if you used two single quotes in text1, they may / will display as such. So you'll need to check if any arguments get formatted in or not and use one or two single quotes accordingly.

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

回答by Nestor Milyaev

If you are completely stuck, as I was (none of the above worked), you can replace the apostrophe sign with its Unicode: \u0027. Remember you are always allowed to use UTF symbol in your properties file.

如果你完全卡住了,就像我一样(以上都没有奏效),你可以用它的 Unicode 替换撇号:\u0027。请记住,您始终可以在属性文件中使用 UTF 符号。