# Inverse Translation

**URL:** <https://www.ruby-forum.com/t/inverse-translation/184622>\
**Category:** Rails I18n\
**Created:** [March 5, 2010, 11:00am UTC](https://www.ruby-forum.com/t/inverse-translation/184622 "2010-03-05T11:00:38Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Veet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f05b48/32.png) [@Veet](https://www.ruby-forum.com/u/Veet)\
**Post date:** [March 5, 2010, 11:00am UTC](https://www.ruby-forum.com/t/inverse-translation/184622/1 "2010-03-05T11:00:38Z")

</div>

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

txt:  
book:  
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.

---

<div class="post-metadata">

**Author:** ![Veet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f05b48/32.png) [@Veet](https://www.ruby-forum.com/u/Veet)\
**Post date:** [March 5, 2010, 11:12am UTC](https://www.ruby-forum.com/t/inverse-translation/184622/2 "2010-03-05T11:12:55Z")

</div>

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”)

---

<div class="post-metadata">

**Author:** ![Veet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f05b48/32.png) [@Veet](https://www.ruby-forum.com/u/Veet)\
**Post date:** [March 5, 2010, 11:26am UTC](https://www.ruby-forum.com/t/inverse-translation/184622/3 "2010-03-05T11:26:23Z")

</div>

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”) =\> :“txt.book.Lord of the Rings”

Regards,  
KK

2010/3/5 Sven F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Veet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f05b48/32.png) [@Veet](https://www.ruby-forum.com/u/Veet)\
**Post date:** [March 5, 2010, 5:14pm UTC](https://www.ruby-forum.com/t/inverse-translation/184622/4 "2010-03-05T17:14:48Z")

</div>

HI  
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 = I18n::Backend::Fast.new 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.:

---

<div class="post-metadata">

**Author:** ![Veet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f05b48/32.png) [@Veet](https://www.ruby-forum.com/u/Veet)\
**Post date:** [March 5, 2010, 11:43pm UTC](https://www.ruby-forum.com/t/inverse-translation/184622/5 "2010-03-05T23:43:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Veet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f05b48/32.png) [@Veet](https://www.ruby-forum.com/u/Veet)\
**Post date:** [March 5, 2010, 6:06pm UTC](https://www.ruby-forum.com/t/inverse-translation/184622/6 "2010-03-05T18:06:56Z")

</div>

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:

> **[GitHub - ruby-i18n/i18n: Internationalization (i18n) library for Ruby](https://github.com/ruby-i18n/i18n)**
>
> Internationalization (i18n) library for Ruby. Contribute to ruby-i18n/i18n development by creating an account on GitHub.

Regards,  
KK

2010/3/5 Hans M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Veet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f05b48/32.png) [@Veet](https://www.ruby-forum.com/u/Veet)\
**Post date:** [March 6, 2010, 12:32am UTC](https://www.ruby-forum.com/t/inverse-translation/184622/7 "2010-03-06T00:32:39Z")

</div>

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  
sv:  
enumerations:  
parenthood:  
parent\_role:  
father: far

I am trying to find the key corresponding to ‘far’ by the following  
expression  
I18n.backend.send(:translations)[:sv][:enumerations][:parenthood]  
[:parent\_role].index(‘far’)  
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.:

---

<div class="post-metadata">

**Author:** ![Veet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f05b48/32.png) [@Veet](https://www.ruby-forum.com/u/Veet)\
**Post date:** [March 6, 2010, 12:46am UTC](https://www.ruby-forum.com/t/inverse-translation/184622/8 "2010-03-06T00:46:07Z")

</div>

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. 🙂 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?

---

<div class="post-metadata">

**Author:** ![Veet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f05b48/32.png) [@Veet](https://www.ruby-forum.com/u/Veet)\
**Post date:** [March 6, 2010, 1:09am UTC](https://www.ruby-forum.com/t/inverse-translation/184622/9 "2010-03-06T01:09:32Z")

</div>

Sven  
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  
problem

* * *

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

6 mar 2010 kl. 00.45 skrev Sven F.:

---

<div class="post-metadata">

**Author:** ![Veet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f05b48/32.png) [@Veet](https://www.ruby-forum.com/u/Veet)\
**Post date:** [March 9, 2010, 6:56pm UTC](https://www.ruby-forum.com/t/inverse-translation/184622/10 "2010-03-09T18:56:45Z")

</div>

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.
