Inverse Translation

Is there a commonly accepted method for translating user input using
I18n ? I would like users to be able to enter the names of books in an
entry form in their own language. The list of books is large but
finite (definitely no more than 500 book titles).

I have .yml files with all the book names as english keys: For
example, in my ‘es.yml’ I have

Lord of the Rings: “El Señor de los Anillos”

If someone enters ‘El Señor de los Anillos’ how do I translate this
back to English? I’ve tried:

I18n.backend.send(:translations).index(“El Señor de los Anillos”)

but that returns nil.

Hi Veet,

no, there’s no official API for this.

In your case the translations will internally be stored in a Hash like
this (in the Simple backend, different story with the AR backend):

{ :es => { :txt => { :book => { :“Lord of the Rings” => “El Señor de los
Anillos” } } } }

I.e., this should be what you get from I18n.backend.send(:translations)

So, in your case this might work:

I18n.backend.send(:translations)[locale][:txt][:book].index(:“Lord of
the Rings”)

Or you can include FastBackend and use flattened_translations, so in
your case:

I18n.backend.flattened_translations[:es].index(“El Señor de los
Anillos”) => :“ of the Rings”


2010/3/5 Sven F. [email protected]:

i have the same problem and would like to apply your solution, but
how to install FastBackend in Rails 2.3.3
I tried by just adding I18n.backend = in a
initializer file 8new_rails_deafult) but that failed
Read about some problems installing it in Rails >2.5 ???

Hans M.
St: Larsgatan 50, 58224 Linköping, Sweden
Phone: +46708371202

5 mar 2010 kl. 11.25 skrev Krzysztof K.:

Yep - this works perfectly (with a minor syntax change to index into
the hash)

I18n.backend.send(:translations)[:es][:txt][:book].index(“El Señor de
los Anillos”)

=> :Lord of the Rings

First of all “Fast” is not a class, but an extension, so you need to
include it:
I18n::Backend::Simple.send :include, I18n::Backend::Fast

For installation in Rails < 2.3.5 you should read i18n README:


2010/3/5 Hans M. [email protected]:

Hi Sven
I try to do the same as Veet, that is to find the inverse translation
I followed your advices, but it fails

I my case I have in the yaml file
father: far

I am trying to find the key corresponding to ‘far’ by the following
It looks as the solution proposed by you and applied successfully by
Veet, but it returns the error

NoMethodError (You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]):

What is wrong with my attempt ??

Hans M.
St: Larsgatan 50, 58224 Linköping, Sweden
Phone: +46708371202

5 mar 2010 kl. 11.11 skrev Sven F.:

You might need to call #init_translations first so your translations
will be actually loaded.

Please, everybody keep in mind that all of this is wild hackery. :slight_smile: It
reaches deep into the backend and uses things that might change at any
point of time.

Maybe someone could come up with some plugin for Backend::Simple that
(still monkeypatches but) hides some of the complexity here?

Thanks but I tried both I18n.init_translations and
I18n.backend,init_translations but they will not work for me
The former is an undefined method and the latter a protected method
So you may be right, tthis may not be a very reliable way to solve the

Hans M.
St: Larsgatan 50, 58224 Linköping, Sweden
Phone: +46708371202

6 mar 2010 kl. 00.45 skrev Sven F.:

Thanks for the insight, Sven. It’s always good to know when you’re
teetering on the edge. It seems as though rudimentary reverse
translation to enable users to input data in their own language is
important so hopefully this will be a part of I18n that will be
fleshed out in future.