Railsで以下のようなlocaleファイルを書いたところ、translation missing
となってしまった。
ja:
yes: はい
no: いいえ
I18n.t("yes")
# => "translation missing: ja.yes"
調べてみたところこれは YAML の仕様で、yes/no は真偽値と判定され、”yes”/”no” ではなく true/false と扱われるためらしい。
In YAML, untagged nodes are given an type depending on the application. The examples in this specification generally use the “
https://yaml.org/spec/1.1/#id858600seq
”, “map
” and “str
” types from the YAML tag repository. A few examples also use the “int
” and “float
” types. The repository includes additional types such as “null
”, “bool
”, “set
” and others.
なので、”yes”ではなく”true”でアクセスできた。
I18n.t("true")
=> "はい"
YAML で yes/no を使いたい場合はクォートすれば良い。
ja:
"yes": はい
ちなみに yes/no だけでなく、on/off も真偽値と判定されるよう。
https://yaml.org/type/bool.html
YAMLは型をもっていて文字列以外も扱えるのだけど、キーにも文字列以外が使えることが抜けがちだなと思いました。
irb(main):001:0> YAML.load("0xff: yes")
=> {255=>true}