How do I add I18n locale dictionaries from gem to Rails Application

强颜欢笑 提交于 2019-12-22 04:26:12

问题


I have a gem that makes use of I18n locale dictionaries that reside in lib/locales/*.yml inside my gems folder.

When the gem is initialized I want to load these dictionaries into my rails application, but I cannot figure out how:

This is what I tried:

I18n.load_path += Dir.glob("lib/locales/*.{rb,yml}")

Unfortunately this does not work when the gem is loaded inside my Rails App. When I call I18n.t("foo") I get "translation missing: en, foo".

I will probably have to provide the full path to the locales when invoking I18n.load_path, but I cannot figure out how.

Any hints?


回答1:


use __FILE__

Dir.glob( File.dirname(__FILE__) + "lib/locales/*.{rb,yml}" ) 



回答2:


For the record, if you put your locales in <GEM_ROOT>/config/locales, they will be picked up automatically.



来源:https://stackoverflow.com/questions/5392425/how-do-i-add-i18n-locale-dictionaries-from-gem-to-rails-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!