如何从gem添加I18n语言环境词典到Rails应用程序

时间:2022-05-17 07:32:23

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

我有一个gem,它使用位于我的gems文件夹中的lib / locales / *。yml中的I18n语言环境字典。

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

初始化gem时,我想将这些词典加载到我的rails应用程序中,但我无法弄清楚如何:

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".

不幸的是,当我的Rails应用程序中加载gem时,这不起作用。当我打电话给I18n.t(“foo”)时,我得到“翻译缺失: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.

我可能必须在调用I18n.load_path时提供语言环境的完整路径,但我无法弄清楚如何。

Any hints?

2 个解决方案

#1


6  

use __FILE__

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

#2


4  

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

对于记录,如果将区域设置放在 / config / locales中,它们将自动被选中。

#1


6  

use __FILE__

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

#2


4  

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

对于记录,如果将区域设置放在 / config / locales中,它们将自动被选中。