onomojo / i18n-timezones

Rails I18n timezone translations
MIT License
68 stars 47 forks source link

Lookup of time zones values in German #10

Closed stiebitzhofer closed 11 years ago

stiebitzhofer commented 11 years ago

My application is localized in English and German, using ActiveSupport and i18n-timezones. When using English everything works fine with the following code.

%br
%select{:name => "timezone"}
  %option{:value => ''} #{t('myaccount.please_select')}
  - ActiveSupport::TimeZone.all.each do |t|
    %option{:value => t, :selected => @user.etsyshop.timezone == t.to_s} #{t}       

However, if I switch to German and select e.g. "(GMT+1:00) Brüssel" the value get's stored properly but when I look up the value somewhere else in the code it crashes because this value is unknown - basically it works only for values where the German translations equals the English original.

def timezone_s
  if timezone
    return timezone.sub(/^\(\S*\)\ /,'')
  end
end

I am a bit lost to be honest - can you give my any advise on how this works and where to adapt stuff get it working properly.

onomojo commented 11 years ago

Aloha, sorry for my insanely delayed response. Have you found a solution yet? I don't really understand what your problem is completely either. Can you pleas explain more or give some additional code or context so I can see what you're trying to do? Thanks

stiebitzhofer commented 11 years ago

No, haven't found a solution yet - was quite busy and on vacation:-)

Let me try another way:

  1. If a user selects e.g. "(GMT+1:00) Bruxelles" in the English localization of the site I store "(GMT+1:00) Bruxelles" in the database.
  2. If a user selects e.g. "(GMT+1:00) Brüssel" in the German localization of the site I store "(GMT+1:00) Brüssel" in the database.

In a class I load the value from the database and retrieve the time zone string for setting the time zone with the function shown above, resulting in either "Bruxelles" or "Brüssel". When I set the time zone with

Time.zone = zone

this crashes.

I set time_zone and locales in the application controller:

    before_filter :set_timezone
    before_filter :set_locale

    def set_timezone
      if current_user && current_user.etsyshop && current_user.etsyshop.timezone && current_user.etsyshop.timezone != ""
        Time.zone = current_user.etsyshop.timezone_s
      end
    end

    def set_locale
      unless params[:locale]
          params[:locale] = extract_locale_from_accept_language_header
      end
      available = ['en', 'de']
      if available.include? params[:locale]
          I18n.locale = params[:locale]
      end
    end

Basically I am a bit glue less where to attack the problem. Any hints and help appreciated.

onomojo commented 11 years ago

Ok, the problem is you're saving the translated version and trying to set the Rails timezone to the translated version. The problem is that Rails itself doesn't understand the translated names. It only knows the original zone names. You ought to just save the original version of the timezone not the translated versions and when you display them just display them based on the current locale so the user will see the translated version.

stiebitzhofer commented 11 years ago

Okay, got it. Will do so. Should I write up how to do so when done?

onomojo commented 11 years ago

If you think it would be useful for someone else then by all means please do. I know the docs are a little terse right now so some additional usage examples would probably be beneficial. Thanks

stiebitzhofer commented 11 years ago

For rookies like me that would definitely shorten the process. Will do so and send you a pull request for your review. Thanks for your help, have a nice weekend.

stiebitzhofer commented 11 years ago

I fixed it. It is so simple. Arrrgh. Anyway I past code here just in case somebody googles the stuff.

%br
  %select{:name => "timezone"}
    %option{:value => ''} #{t('myaccount.please_select')}
    - ActiveSupport::TimeZone.all.each do |t|
      %option{:value => t.name, :name => t.to_s, :selected => @user.etsyshop.timezone == t.name} #{t} 

Thanks again for pointing me to the solution!