11

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') ?

5 Answers5

7

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

Sammy Larbi
  • 3,062
  • 3
  • 26
  • 21
Tilo
  • 33,354
  • 5
  • 79
  • 106
  • Works for me. Using RubyMine, you can open the gem and insert those puts in the code, and run the server within RM. clean. Still, I'd prefer a `config.i18n.lookup_debug = true` – oma Oct 30 '12 at 16:05
  • Sorry, it was a wish. It's not there AFAIK. Do you have a reference to Sven Fuchs statement? – oma Oct 31 '12 at 13:52
  • I had a Twitter conversation with him - search "@svenfuchs config.i18n.lookup_debug" and click on "expand" – Tilo Nov 01 '12 at 18:03
  • unfortunately `config.i18n.lookup_debug` does not exist. – Tilo Nov 06 '12 at 18:56
  • 1
    Link seems to be 404 now. – pjmorse Sep 30 '14 at 19:47
  • Can somebody share the information that was in the link? – Yo Ludke Mar 11 '15 at 09:54
  • @PereJoanMartorell you have to check for the source code of the latest L10N code and update the monkey patch accordingly... – Tilo Mar 18 '21 at 20:46
3

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

sled
  • 14,525
  • 3
  • 42
  • 70
1

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
iloveitaly
  • 2,053
  • 22
  • 21
  • For `I18n.load_path.join("\n")`, when multiple files define the same key, based on the order of the load_paths which file definition will `I18n` end up using? – Kelsey Hannan Apr 19 '21 at 22:21
0

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>"
Yo Ludke
  • 2,149
  • 2
  • 23
  • 38
0

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

Yann
  • 43
  • 1
  • 5