如何将I18n语言环境词典从gem添加到Rails应用程序 [英] How do I add I18n locale dictionaries from gem to Rails Application
问题描述
我有一个gem,它使用位于gems文件夹内lib/locales/*.yml中的I18n语言环境字典.
I have a gem that makes use of I18n locale dictionaries that reside in lib/locales/*.yml inside my gems folder.
初始化gem时,我想将这些字典加载到我的rails应用程序中,但是我不知道如何:
When the gem is initialized I want to load these dictionaries into my rails application, but I cannot figure out how:
这是我尝试过的:
I18n.load_path += Dir.glob("lib/locales/*.{rb,yml}")
不幸的是,当将宝石加载到我的Rails应用程序中时,此方法不起作用.呼叫I18n.t("foo")
时得到"translation missing: en, foo"
.
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"
.
当调用I18n.load_path时,我可能必须提供到语言环境的完整路径,但是我不知道该怎么做.
I will probably have to provide the full path to the locales when invoking I18n.load_path, but I cannot figure out how.
有任何提示吗?
推荐答案
使用__FILE__
Dir.glob( File.dirname(__FILE__) + "lib/locales/*.{rb,yml}" )
这篇关于如何将I18n语言环境词典从gem添加到Rails应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!