8

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.

Trip
  • 25,831
  • 41
  • 146
  • 260
  • I think using dashes in a key is allowed and should not give problems. Are you sure it's the dashes? – zwippie May 12 '15 at 13:24
  • @zwippie I'm positive. Ruby doesn't allow it. – Trip May 12 '15 at 13:37
  • Well I tried it and my Ruby (version) does allow it. What Ruby version are you on? – zwippie May 12 '15 at 13:46
  • @zwippie My apologies! It does seem to work in raw ruby. I guess my case is that it doesn't work when parsing through my en.yml ( Il8n file ). Updated my question – Trip May 12 '15 at 13:48
  • 2
    You don’t have quotes around the string. Is this just a typo (since you say it returns nil rather than a syntax error)? – matt May 14 '15 at 14:55

3 Answers3

5

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?

Javier Vidal
  • 1,251
  • 11
  • 8
  • Hmm interesting. Yes it does seem to work in console like your example. But for some reason when I do it through my en.yml ( Il8n file ), it won't load. Updated my question – Trip May 12 '15 at 13:47
1

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')
infused
  • 22,433
  • 13
  • 63
  • 74
  • Super good point. I just checked my code, and I realized I only included that in the example. It still doesn't seem to work even with the en removed – Trip May 14 '15 at 14:18
1

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.

Elvn
  • 2,781
  • 1
  • 12
  • 27