How do I declare a string with both single and double quotes in YAML?
Solution 1
escaping should be done like this
"When you're using double quotes, they look like \"this\""
Solution 2
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.
That’s the best practice ever: map those within 3rd keyboard level (or, eventually, sed
your 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.
Solution 3
See if this works for you, it works perfectly for me in my spring boot applications where I needed to pass JSON values in:
Using YAML pipe style:
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
Peter Brown
I'm a web application developer located in Vermont and working at Seated.
Updated on August 18, 2020Comments
-
Peter Brown almost 4 years
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: 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?
-
Peter Brown over 11 yearsI like this idea! Just discovered I could type them with option+left/right bracket (OS X).
-
Knetic about 9 yearsI know I'm late to the game with this, but using odd character for quotes doesn't make sense. They're difficult to insert, make parsing a pain (any process which ever consumes your data will need to be aware that you're using nonstandard quotes), but don't add anything. Just escape them, as in the accepted answer - there are no surprises for anyone with that method.
-
Aleksei Matiushkin about 9 years@Knetic You just don’t get a point. It makes a huge sense, it’s easy to insert, it makes parsing a charm (left and right quotes differ) and nobody being sane would operate on your translated strings automatically on purpose. On the other hand you add an aesthetics, that has more value than everything ever. In 2015 only Neanderthals still get stuck with typewriters quotes.
-
elsurudo about 9 yearsThis... doesn't work for me. When I do something like this and then
puts
the string, it printsWhen you're using double quotes, they look like \"this\"
. Help? -
jvnill about 9 yearsare you surrounding it in double quotes?
-
elsurudo about 9 yearsYep. Tried single quotes as well – same result.
-
jvnill about 9 yearsno it should be in double quotes. if you're using single quotes, the single quote is the one you need to escape, ie add a backslash before it. paste your entire code here or ask another question.
-
t7tran almost 7 yearsI don't know in which use case I would consider this particular option but it should not be considered as a decent solution. It merely pushes the problem to others.
-
Anthon over 5 yearsAccording to the YAML specification this quoted scalar you present as an alternative is exactly the same as the plain scalar the OP was using. This is easily verified by pasting both versions in an online parser like nimyaml.org/testing.html. So this is most like an actual bug in the ruby YAML implementation.