Ruby-on-rails 如何在 YAML 中声明带有单引号和双引号的字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14992328/
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
How do I declare a string with both single and double quotes in YAML?
提问by Peter Brown
I'm internationalizing an application and cannot figure out how to declare a translation string that contains both single and double quotes. Here's an example of the en.yml string that I am trying to
我正在对应用程序进行国际化,但无法弄清楚如何声明包含单引号和双引号的翻译字符串。这是我尝试使用的 en.yml 字符串的示例
en:
my_string: When you're using double quotes, they look like "this"
With the above string, I get the following error:
使用上述字符串,我收到以下错误:
can not load translations from /vagrant/config/locales/en.yml,
expected it to return a hash, but does not
If there were just double quotes, I would wrap it in single quotes and vise-versa. How do I handle both double and single quotes though?
如果只有双引号,我会将它用单引号括起来,反之亦然。我如何处理双引号和单引号?
回答by jvnill
escaping should be done like this
逃跑应该像这样完成
"When you're using double quotes, they look like \"this\""
回答by Aleksei Matiushkin
Actually I can't figure out why do you need obsolete typewriter quotes in translation strings. There is 2013 around and we are not stuck to ASCII-7 anymore. The typography rules dictate their demands to use unicode quotation marks.
实际上我不明白为什么你需要在翻译字符串中使用过时的打字机引号。大约是 2013 年,我们不再拘泥于 ASCII-7。排版规则规定了他们使用unicode 引号的要求。
That's the best practice ever: map those within 3rd keyboard level (or, eventually, sedyour yml):
这是有史以来的最佳实践:将那些映射到第 3 键盘级别(或最终是sed您的yml):
"When you're using double quotes, they look like “this”"
With such an approach you'll never run into troubles with escaping and your clients will definitely say “oh, neat.”
使用这种方法,您永远不会遇到逃逸的麻烦,您的客户肯定会说“哦,整洁”。
Sorry, if this seems a little bit off-topic, but since the question was about translation strings, I still consider it to be the best solution.
对不起,如果这看起来有点离题,但由于问题是关于翻译字符串,我仍然认为它是最好的解决方案。
回答by coolersport
See if this works for you, it works perfectly for me in my spring boot applications where I needed to pass JSON values in:
看看这是否适合你,它在我需要传递 JSON 值的 Spring Boot 应用程序中非常适合我:
Using YAML pipe style:
使用 YAML 管道样式:
app.json:
values: |
{"key": "value"}
Your case would be:
你的情况是:
en:
my_string: |
When you're using double quotes, they look like "this"
Folded style might work too but I haven't tried.
折叠样式也可以,但我没试过。
See more here: http://symfony.com/doc/current/components/yaml/yaml_format.html#strings
在此处查看更多信息:http: //symfony.com/doc/current/components/yaml/yaml_format.html#strings

