Peter Brown
Peter Brown

Reputation: 51707

How do I declare a string with both single and double quotes in YAML?

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?

Upvotes: 45

Views: 48953

Answers (3)

t7tran
t7tran

Reputation: 2136

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

Upvotes: 10

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

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.

Upvotes: 13

jvnill
jvnill

Reputation: 29599

escaping should be done like this

"When you're using double quotes, they look like \"this\""

Upvotes: 52

Related Questions