Today I Learned

Giant Monkeys Learning

yes and no are booleans when used as keys in YAML

Whilst fixing a strange bug, I learnt today that yes and no are actually boolean key values in yaml.

E.g. We take this line of ruby:

booking.feedback_done ? I18n.t("billing.billing_public_tours.yes") : I18n.t("billing.billing_public_tours.no"),

calling #feedback_done should simply return a boolean value, so all looks okay right?

[2] pry(#<#<Class:0x0000000162290e60>>)> booking.feedback_done ? true : false
=> false

If we take a look at our YAML:

    billing_public_tours:
      ...
      yes: "Ja"
      no: "Nein"

Our translations look correctly configured. So why do we get this?

[1] pry(#<#<Class:0x0000000162290e60>>)> booking.feedback_done ? I18n.t("billing.billing_public_tours.yes") : I18n.t("billing.billing_public_tours.no")
=> "Translation missing: de.billing.billing_public_tours.no"

Because Booleans are formatted as English words in YAML

If we wrap them in quotes:

    billing_public_tours:
      ...
      "yes": "Ja"
      "no": "Nein"

Our code will now work:

[1] pry(#<#<Class:0x000000016dff30c0>>)> booking.feedback_done ? I18n.t("billing.billing_public_tours.yes") : I18n.t("billing.billing_public_tours.no")
=> "Nein"