如何在en.yml中编写引用值?

Geo*_*Geo 2 ruby yaml ruby-on-rails

我正在编写一个脚本,将新的翻译添加到en.yml文件中.但是,当我将它们转储回文件时,我的字符串采用以下格式:

some_key: This is the value
Run Code Online (Sandbox Code Playgroud)

我正在尝试使输出为:

some_key: "This is the value"
Run Code Online (Sandbox Code Playgroud)

我正在写这样的翻译:

File.open(yaml_file, "w") do |f|
  f.write(translations.to_yaml)
end
Run Code Online (Sandbox Code Playgroud)

翻译是包含所有翻译的哈希.

除了手动解析/重写YAML文件之外,还有什么方法可以添加这些引号吗?

Sim*_*tti 5

计划(unquotes)标量表示是当标量类型不需要逸出的优选版本.

在你的情况下,字符串:

This is the value
Run Code Online (Sandbox Code Playgroud)

因此,如果您提供以下YAML,则不需要在引号中:

 key: "This is the value"
Run Code Online (Sandbox Code Playgroud)

处理器可能会返回:

 key: This is the value
Run Code Online (Sandbox Code Playgroud)

因为它们完全等同.但是,如果您确实要输入带引号的字符串作为值,那么您应该使用:

 key: '"This is the value"'
Run Code Online (Sandbox Code Playgroud)

或逃避双重报价:

 key: "\"This is the value\""
Run Code Online (Sandbox Code Playgroud)

我快速浏览一下Psych发射器代码,由the引用的代码,to_yaml并且似乎没有强制引用标量的选项.

我甚至没有看到标量发射器代码中实现的选项.

 def visit_Psych_Nodes_Scalar o
   @handler.scalar o.value, o.anchor, o.tag, o.plain, o.quoted, o.style
 end
Run Code Online (Sandbox Code Playgroud)

换句话说,您无法强制引用.