Translating enum_strings and switching languages in production mode

65 views
Skip to first unread message

Ignacio Huerta

unread,
Feb 22, 2012, 2:13:05 PM2/22/12
to hobousers
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I'm a bit puzzled with a translation problem: switching languages
doesn't work in production only for enum_string fields.

We are working on a Rails 3.0.10 + Hobo 1.3.0 app. It's a Car Rental
site, and we need it to work in English and Spanish. Right now
translations are working except for a enum_string field, where we have
found a weird behaviour when switching languages:

- - Language switches correctly in development mode, including the
enum_string field
- - Language switches correctly in production mode, *except* for the
enum_string field, which stays in the first language loaded (sometimes
spanish, sometimes english, depending on the param provided in the
first petition).

I have reproduced this behaviour in a clean one table Hobo app, and we
are switching languages using the official Rails guide method, so I
think that's not the problem. I believe that as the problem is in the
fix commented last year:
http://groups.google.com/group/hobousers/browse_thread/thread/484dc97b1e7c80e6?pli=1
:

Region = HoboFields::Types::EnumString.for(:ab, :bc, :name =>
"Region")

Somehow the enum_string is being cached and not changing languages.
Has anybody else seen this behaviour? Any tip on where to start digging?

Regards,
Ignacio

- --
Ignacio Huerta Arteche
http://www.ihuerta.net
Tel�fono: 0034 645 70 77 35
Email realizado con software libre
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk9FPj4ACgkQBPlUJ6RHaORIDgCdGIk7h/zxyVBJ6MGEVxt357a1
CxcAn1DYP0n2VRctcuAryxXD8ws7etTM
=+tMc
-----END PGP SIGNATURE-----

Tomoaki Hayasaka

unread,
Feb 23, 2012, 4:07:55 AM2/23/12
to hobo...@googlegroups.com, ign...@ihuerta.net
Hi,

> - - Language switches correctly in development mode, including the
> enum_string field
> - - Language switches correctly in production mode, *except* for the
> enum_string field, which stays in the first language loaded (sometimes
> spanish, sometimes english, depending on the param provided in the
> first petition).

Current enum_string implementation doesn't consider locale switching.

As a workaround, you can re-initialize the enum_string on every
request in your application_controller:

prepend_before_filter :force_random_locale

def force_random_locale
I18n.locale = ("aa".."zz").to_a[rand(676)]
Region.with_values(*Region.values)
end

But it may produce strange results in threaded environment.

-----
Tomoaki Hayasaka <haya...@pfsl.mech.tohoku.ac.jp>

Bryan Larsen

unread,
Feb 23, 2012, 4:25:16 AM2/23/12
to hobo...@googlegroups.com
> Current enum_string implementation doesn't consider locale switching.

This is not a coincidence. Hobo's i18n mechanism was written by
people who actually use it: Spiralis and Domizio, primarily. The
only exception is the enum_string localization, which I wrote because
we needed to finish 1.3 and get it out the door, instead of writing it
because I needed it for one of my own websites.

So if somebody who actually creates multi-lingual websites wants to
take a crack at fixing this problem properly, that would be
appreciated.

Bryan

Ignacio Huerta

unread,
Feb 23, 2012, 10:40:50 AM2/23/12
to hobo...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thank you very much to both of you.

I'm afraid the workaround didn't work for me. I got this error:

uninitialized constant OrdersController::Region

I'm going to try to take a look at the Hobo code and see what I can
do. By the way, I created an ticket about this in lighthouse: we have
reached ticket number 1000!

https://hobo.lighthouseapp.com/projects/8324-hobo/tickets/1000-i18n-support-switching-locales-with-enum_string

Regards,
Ignacio

El 23/02/12 10:25, Bryan Larsen escribi�:

- --

Ignacio Huerta Arteche
http://www.ihuerta.net
Tel�fono: 0034 645 70 77 35
Email realizado con software libre
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk9GXgAACgkQBPlUJ6RHaOT9+ACggrtEJuZS6IMs52jj67XfrE0w
2t4An38tx05jVD4L3otJH6G+pm0lCKnR
=djc5
-----END PGP SIGNATURE-----

Ignacio Huerta

unread,
Feb 23, 2012, 2:41:17 PM2/23/12
to hobo...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I've been digging into this and I have found that right now the
enum_string translations is done within hobo_fields, and that makes
translations only work on first load in production mode.

Maybe we can move translations to the tag library? I made a small test
with the input tag and it worked nicely in development and production
mode:

<def tag="input" for="HoboFields::Types::EnumString" attrs="labels,
titleize, first-option, first-value"><%
labels ||= {}
labels = HashWithIndifferentAccess.new(labels)
titleize = true if titleize.nil?

# I18N: First we need to find the column name searching in the
content_columns
for column in model.content_columns.*.name
if model.attr_type(column) == this_type
options = this_type.values.map {|v|
[

I18n.t("activerecord.attributes.#{model.to_s.downcase}/#{column}s.#{v}",
:default => titleize ? v.titleize : v),
v]}
end
end
%>
<select name="#{param_name_for_this}" merge-attrs>
<option value="#{first_value}"
unless="&first_option.nil?"><first-option/></option>
<%= options_for_select(options, this) %>
</select>
</def>


Now you can add translations to your app.**.yml like this:

activerecord:
attributes:
order/pickup_points:
hotel: "Hotel"
office: "Oficina"
airport: "Aeropuerto"

Do you think this is a good solution? If you want I can test it a bit
more and make a pull request :).

Regards,
Ignacio

El 23/02/12 16:40, Ignacio Huerta escribi�:

iEYEARECAAYFAk9Gll0ACgkQBPlUJ6RHaOTPsgCdFKaWwla4YyTeq4xNjG48tMf2
hd0AnRWzO9nD09lo6hvRgCoq0q9JJFhQ
=gIP+
-----END PGP SIGNATURE-----

Bryan Larsen

unread,
Feb 23, 2012, 3:43:28 PM2/23/12
to hobo...@googlegroups.com

That seems like a reasonable solutiony to me.

El 23/02/12 16:40, Ignacio Huerta escribió:

> Thank you very much to both of you.
>
> I'm afraid the workaround didn't work for me. I got this error:
>
> uninitialized constant OrdersController::Region
>
> I'm going to try to take a look at the Hobo code and see what I
> can do. By the way, I created an ticket about this in lighthouse:
> we have reached ticket number 1000!
>
> https://hobo.lighthouseapp.com/projects/8324-hobo/tickets/1000-i18n-support-switching-locales-with-enum_string
>
>  Regards, Ignacio
>
> El 23/02/12 10:25, Bryan Larsen escribió:

>>> Current enum_string implementation doesn't consider locale
>>> switching.
>
>> This is not a coincidence.  Hobo's i18n mechanism was written by
>>  people who actually use it: Spiralis and Domizio, primarily.
>> The only exception is the enum_string localization, which I
>> wrote because we needed to finish 1.3 and get it out the door,
>> instead of writing it because I needed it for one of my own
>> websites.
>
>> So if somebody who actually creates multi-lingual websites wants
>> to take a crack at fixing this problem properly, that would be
>> appreciated.
>
>> Bryan
>
>

- --
Ignacio Huerta Arteche
http://www.ihuerta.net
Teléfono: 0034 645 70 77 35

Email realizado con software libre
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk9Gll0ACgkQBPlUJ6RHaOTPsgCdFKaWwla4YyTeq4xNjG48tMf2
hd0AnRWzO9nD09lo6hvRgCoq0q9JJFhQ
=gIP+
-----END PGP SIGNATURE-----

--
You received this message because you are subscribed to the Google Groups "Hobo Users" group.
To post to this group, send email to hobo...@googlegroups.com.
To unsubscribe from this group, send email to hobousers+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/hobousers?hl=en.

erbecke

unread,
Jul 11, 2012, 3:35:35 PM7/11/12
to hobo...@googlegroups.com
Ignacio, what version did you use with that fix?

I'm stucked with that bug.

best regards,
eddie.

El 23/02/12 16:40, Ignacio Huerta escribi�:


> Thank you very much to both of you.
>
> I'm afraid the workaround didn't work for me. I got this error:
>
> uninitialized constant OrdersController::Region
>
> I'm going to try to take a look at the Hobo code and see what I
> can do. By the way, I created an ticket about this in lighthouse:
> we have reached ticket number 1000!
>
> https://hobo.lighthouseapp.com/projects/8324-hobo/tickets/1000-i18n-support-switching-locales-with-enum_string
>
>  Regards, Ignacio
>

> El 23/02/12 10:25, Bryan Larsen escribi�:


>>> Current enum_string implementation doesn't consider locale
>>> switching.
>
>> This is not a coincidence.  Hobo's i18n mechanism was written by
>>  people who actually use it: Spiralis and Domizio, primarily.
>> The only exception is the enum_string localization, which I
>> wrote because we needed to finish 1.3 and get it out the door,
>> instead of writing it because I needed it for one of my own
>> websites.
>
>> So if somebody who actually creates multi-lingual websites wants
>> to take a crack at fixing this problem properly, that would be
>> appreciated.
>
>> Bryan
>
>

- --
Ignacio Huerta Arteche
http://www.ihuerta.net

Tel�fono: 0034 645 70 77 35


Email realizado con software libre
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

erbecke

unread,
Jul 11, 2012, 4:52:35 PM7/11/12
to hobo...@googlegroups.com
sorry, I had a typo with the "attributes" chain, and I found that you pluralize with an "s" all the columns name ;)

thank you for your workaround!

e.

Tel�fono: 0034 645 70 77 35


Email realizado con software libre
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk9FPj4ACgkQBPlUJ6RHaORIDgCdGIk7h/zxyVBJ6MGEVxt357a1
CxcAn1DYP0n2VRctcuAryxXD8ws7etTM
=+tMc
-----END PGP SIGNATURE-----

Tel�fono: 0034 645 70 77 35


Email realizado con software libre
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk9FPj4ACgkQBPlUJ6RHaORIDgCdGIk7h/zxyVBJ6MGEVxt357a1
CxcAn1DYP0n2VRctcuAryxXD8ws7etTM
=+tMc
-----END PGP SIGNATURE-----

Tel�fono: 0034 645 70 77 35


Email realizado con software libre
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk9FPj4ACgkQBPlUJ6RHaORIDgCdGIk7h/zxyVBJ6MGEVxt357a1
CxcAn1DYP0n2VRctcuAryxXD8ws7etTM
=+tMc
-----END PGP SIGNATURE-----

Tel�fono: 0034 645 70 77 35


Email realizado con software libre
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk9FPj4ACgkQBPlUJ6RHaORIDgCdGIk7h/zxyVBJ6MGEVxt357a1
CxcAn1DYP0n2VRctcuAryxXD8ws7etTM
=+tMc
-----END PGP SIGNATURE-----

Tel�fono: 0034 645 70 77 35


Email realizado con software libre
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

Stefan Haslinger

unread,
Dec 7, 2013, 7:22:43 AM12/7/13
to hobo...@googlegroups.com
Ignacio,

thanks, that helped me a lot.
But your solution fails, if the Enum lives in these subforms which are generated by available => true
in a strange way: undefined method `map' for nil:NilClass
in the line of <%= options_for_select(options, this) %> (the third line from the bottom)

Capturing with a debugger gives me 
(rdb:1) self
"    \n    "
(rdb:1) @this
nil
which puzzles me even more.

Is there any more info that could be helpful? 

Muchos saludos,
Stefan

Ignacio Huerta

unread,
Dec 9, 2013, 3:53:51 PM12/9/13
to hobo...@googlegroups.com
Hi Stefan,

I just tested it in a subform and it seemed to work. I tested the
default Hobo 2.0 (the code from the previous message in this thread was
merged into master before releasing Hobo 2.0).

Maybe you are using a previous Hobo version? Did you customise the enum
string input or are you using the default one that comes with Hobo?
Could you copy some of the failing code? Thanks.

Regards,
Ignacio

El 07/12/13 13:22, Stefan Haslinger escribi�:
> --
> You received this message because you are subscribed to the Google
> Groups "Hobo Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to hobousers+...@googlegroups.com.
> To post to this group, send email to hobo...@googlegroups.com.
> Visit this group at http://groups.google.com/group/hobousers.
> For more options, visit https://groups.google.com/groups/opt_out.

Stefan Haslinger

unread,
Dec 11, 2013, 8:40:50 AM12/11/13
to hobo...@googlegroups.com
HI Ignacio, 

thanks for your support, you're awesome! 

I commented my overridden input tag, and now the subform was translated 
... but only the subform 
... because I translated the wrong keys (the EnumString instance name not the attribute name). 
Silly me.

So instead of coping failing code, for everyone who is as puzzled as I was, a Gist with the code that works for me.

Thanks again,
Stefan

Ignacio Huerta

unread,
Dec 12, 2013, 4:50:06 AM12/12/13
to hobo...@googlegroups.com
You are welcome, I'm gonna make a small comment on the gist :)

El 11/12/13 14:40, Stefan Haslinger escribi�:
> HI Ignacio,
>
> thanks for your support, you're awesome!
>
> I commented my overridden input tag, and now the subform was translated
> ... but only the subform
> ... because I translated the wrong keys (the EnumString instance name
> not the attribute name).
> Silly me.
>
> So instead of coping failing code, for everyone who is as puzzled as I
> was, a Gist <https://gist.github.com/haslinger/7909337> with the code
> that works for me.
>
> Thanks again,
> Stefan
>
Reply all
Reply to author
Forward
0 new messages