user985823
user985823

Reputation:

How to debug Rails I18N lookup?

In the Rails docs there seem to be different default locations for I18N strings, depending if the I18N-lookup was initiated from a view, model / validation, controller, helper, ..., if it's a label, etc...

How can I see where Rails is trying to lookup things by default, e.g. when I just use t('.something') ?

Upvotes: 11

Views: 5127

Answers (5)

Yann
Yann

Reputation: 45

If the translation you're trying to debug is being set in the controller, there is a simple solution:

Set a variable in the controller to a tag that's doesn't exist and read the path return by the translation missing warning.

For instance in the controller

@test =  t('.choco_pizza')

In your view:

<%= @test %>

Will return:

translation missing: fr.unexpected_namespace.controller_name.action_name.choco_pizza

Upvotes: 0

Tilo
Tilo

Reputation: 33732

You can monkey patch the I18N backend in development mode to print out the I18n keys that are looked up in the backend.

Check here:

http://www.unixgods.org/Rails/where_is_Rails_trying_to_lookup_L10N_strings.html

Upvotes: 7

Yo Ludke
Yo Ludke

Reputation: 2259

In rails 3.2 (maybe also lower versions), a span is produced by the t - helper in views that shows you which key was searched for the translation. This isn't a solution for all cases (from controller and so on), but I think it can be the answer for a lot of people who search for this question, where the full monkey patch from above would be over the top (the monkey patch also works for me in i18n 0.7.0 and gives more detail)

title="translation missing: de.<path to key>"

Upvotes: 0

iloveitaly
iloveitaly

Reputation: 2155

The solution above does not help find what file a key is being looked up in. I did not find any elegant solution to this, below is the best method I came up with. The instructions would have to be adapted for a production box.

  1. Open up a rails console bundle exec rails c
  2. Run I18n.load_path.join("\n") and copy this to your clipboard. If you use pry with some clipboard helpers, just run copy in the console
  3. Open up a new terminal window and run pbpaste | ack 'en.yml$' | xargs ack 'key:' This will print out a list of files containing the key I18n is trying to access

Upvotes: 1

sled
sled

Reputation: 14625

the standalone I18n.t does not prefix your translation key in any way, here are the helper methods/modules that are responsible for the rails' magic:

(click on the "source" link below the methods' description to see what's happening inside)

ActionView:

http://api.rubyonrails.org/classes/ActionView/Helpers/TranslationHelper.html#method-i-t

scope_key_by_partial

ActiveModel:

http://api.rubyonrails.org/classes/ActiveModel/Translation.html#method-i-i18n_scope

AbstractController

http://api.rubyonrails.org/classes/AbstractController/Translation.html

Upvotes: 3

Related Questions