简体   繁体   中英

How to check if key doesn't exist in yml-file?

I want to create a conditional for

t(foo.bar)

that returns false if foo.bar doesn't exist and true if it does exist in the yml-file, like this.

foo:
  bar: here

You can try using I18n.t! and rescue the I18n::MissingTranslationData exception it raises if the key is missing:

def translation_exists?(key)
  begin
    I18n.t!(key)
    true # or just return the result of t! as it's truthy.
  rescue => I18n::MissingTranslationData
    false
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM