如何在YAML中声明带有单引号和双引号的字符串?

Pet*_*own 32 ruby yaml ruby-on-rails

我正在国际化一个应用程序,无法弄清楚如何声明包含单引号和双引号的转换字符串.这是我正在尝试的en.yml字符串的示例

en:
  my_string: When you're using double quotes, they look like "this"
Run Code Online (Sandbox Code Playgroud)

使用上面的字符串,我收到以下错误:

can not load translations from /vagrant/config/locales/en.yml,
expected it to return a hash, but does not
Run Code Online (Sandbox Code Playgroud)

如果只有双引号,我会用单引号括起来,反之亦然.我如何处理双引号和单引号?

jvn*_*ill 42

逃避应该这样做

"When you're using double quotes, they look like \"this\""
Run Code Online (Sandbox Code Playgroud)


Ale*_*kin 12

实际上我无法弄清楚为什么你需要在翻译字符串中使用过时的打字机报价.有2013年左右,我们不再坚持ASCII-7了.排版规则规定了他们使用unicode引号的要求.

这是有史以来最好的做法:映射第三个键盘级别(或最终,sed你的yml):

"When you’re using double quotes, they look like “this”"
Run Code Online (Sandbox Code Playgroud)

通过这种方法,您将永远不会遇到逃避的麻烦,您的客户肯定会说"哦,整洁."

对不起,如果这看起来有点偏离主题,但由于问题是关于翻译字符串,我仍然认为它是最好的解决方案.

  • 我知道我已经迟到了,但使用奇怪的字符作为引号是没有意义的.它们很难插入,使解析成为一种痛苦(任何消耗数据的过程都需要知道你使用的是非标准引号),但不要添加任何内容.正如在接受的答案中那样逃避它们 - 任何拥有该方法的人都不会感到惊讶. (5认同)
  • 我喜欢这个主意!刚发现我可以用选项+左/右括号(OS X)键入它们. (2认同)
  • @Knetic你只是没有明白一点。它意义重大,很容易插入,它使解析变得很有趣(左右引号不同),并且没有人会故意自动对您的_translated_字符串进行操作。另一方面,你添加了一种美学,这比任何东西都更有价值。2015 年,只有尼安德特人还被打字机上的引言所困扰。 (2认同)

coo*_*ort 6

看看这是否适合你,它在我需要传递 JSON 值的 Spring Boot 应用程序中非常适合我:

使用 YAML 管道样式:

app.json:
  values: |
    {"key": "value"}
Run Code Online (Sandbox Code Playgroud)

你的情况是:

en:
  my_string: |
    When you're using double quotes, they look like "this"
Run Code Online (Sandbox Code Playgroud)

折叠样式也可以,但我没试过。

在此处查看更多信息:http : //symfony.com/doc/current/components/yaml/yaml_format.html#strings