How to retrieve all translations from yml files in Rails I18n

前端 未结 6 1151
Happy的楠姐
Happy的楠姐 2020-12-24 11:38

I\'m guessing that rails stores all the parsed translations yml files in a sort of array/hash. Is there a way to access this?

For example, if I\'ve a file:



        
6条回答
  •  暖寄归人
    2020-12-24 12:35

    The default I18n backend is I18n::Backend::Simple, which does not expose the translations to you. (I18.backend.translations is a protected method.)

    This isn't generally a good idea, but if you really need this info and can't parse the file, you can extend the backend class.

    class I18n::Backend::Simple
      def translations_store
        translations
      end
    end
    

    You can then call I18n.backend.translations_store to get the parsed translations. You probably shouldn't rely on this as a long term strategy, but it gets you the information you need right now.

提交回复
热议问题