Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

For example :

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

Then if I do :

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

This will not parse in Ruby's yml. Is there some way to make it work though? My keys are based on URL's which have dashes ( - ) in them.

like image 438
Trip Avatar asked May 12 '15 13:05

Trip


3 Answers

Which version of ruby are you using? Can you show us your code and the error?

It 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?

like image 70
Javier Vidal Avatar answered Nov 15 '22 23:11

Javier Vidal


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

t('foobar-does-not-work')
like image 43
infused Avatar answered Nov 15 '22 23:11

infused


Clearly there is an underlying problem that needs to be ferreted out. There is very good tool which analyzes your i18n YAML as the Rails app, which I have found extremely helpful in debug.

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

To create a comprehensive report of the your i18n components:

$ i18n-tasks health

From their spec:

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 the gem was intended to be used as an i18n debug tool, but I have found it to be useful for debugging hard to find problems in i18n.

like image 30
Elvn Avatar answered Nov 15 '22 22:11

Elvn