Forum: Rails I18n Inverse Translation

Posted by Veet (Guest)
on 2010-03-05 11:00
(Received via mailing list)
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.
Posted by Sven Fuchs (Guest)
on 2010-03-05 11:12
(Received via mailing list)
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")
Posted by Krzysztof Knapik (Guest)
on 2010-03-05 11:26
(Received via mailing list)
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 Fuchs <svenfuchs@artweb-design.de>:
Posted by Hans Marmolin (marmolin)
on 2010-03-05 17:14
(Received via mailing list)
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 Marmolin
St: Larsgatan 50, 58224 Linköping, Sweden
Phone: +46708371202







5 mar 2010 kl. 11.25 skrev Krzysztof Knapik:
Posted by Krzysztof Knapik (Guest)
on 2010-03-05 18:06
(Received via mailing list)
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:
http://github.com/svenfuchs/i18n

Regards,
KK

2010/3/5 Hans Marmolin <hans.marmolin@klockholm.se>:
Posted by Veet (Guest)
on 2010-03-05 23:43
(Received via mailing list)
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
Posted by Hans Marmolin (marmolin)
on 2010-03-06 00:32
(Received via mailing list)
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 Marmolin
St: Larsgatan 50, 58224 Linköping, Sweden
Phone: +46708371202







5 mar 2010 kl. 11.11 skrev Sven Fuchs:
Posted by Sven Fuchs (Guest)
on 2010-03-06 00:46
(Received via mailing list)
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?
Posted by Hans Marmolin (marmolin)
on 2010-03-06 01:09
(Received via mailing list)
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 Marmolin
St: Larsgatan 50, 58224 Linköping, Sweden
Phone: +46708371202







6 mar 2010 kl. 00.45 skrev Sven Fuchs:
Posted by Veet (Guest)
on 2010-03-09 18:56
(Received via mailing list)
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.