Is it possible to have hyphens in a jumbled key for use in my I18n en.yml file?

For instance:

en: foobar-does-not-work: 'This is my value' 

Then if I do:

 t(foobar-does-not-work) # => returns nil 

This will not parse Ruby yml. Is there any way to make it work? My keys are based on a url that has a dash ( - ) in it.

+6
source share
3 answers

What version of ruby ​​are you using? Can you show us your code and error?

This works for me:

 > require 'yaml' > YAML.load_file('foo.yml') {"en"=>{"foobar-does-not-work"=>"This is my value"}} 

And it works when I add it to my en.yml:

 > I18n.t('foobar-does-not-work') => "This is my value" 

Have you checked the value of I18n.locale?

+5
source

I think you are using the wrong key when calling method t . Remove the 'en' from the key. It should be:

 t('foobar-does-not-work') 
+1
source

Obviously, there is a basic problem that needs to be fixed. There is a very good tool that parses your i18n YAML as a Rails application, which I found extremely useful when debugging.

Install and run this gem of i18n tasks: https://github.com/glebm/i18n-tasks .

To create a comprehensive report of your i18n components:

 $ i18n-tasks health 

From their specification:

 This gem analyses code statically for key usages, such as I18n.t('some.key'), in order to: Report keys that are missing or unused. Pre-fill missing keys, optionally from Google Translate. Remove unused keys. Thus addressing the two main problems of i18n gem design: Missing keys only blow up at runtime. Keys no longer in use may accumulate and introduce overhead, without you knowing it. 

I'm not sure if the gem was intended to be used as a debugging tool for i18n, but I found it useful for debugging to find problems in i18n.

+1
source

Source: https://habr.com/ru/post/987055/


All Articles