views:

61

answers:

1

I'm about to translate all time zones to Russian and I've done such things:

model:

# lib/i18n_time_zone.rb
class I18nTimeZone < ActiveSupport::TimeZone
  def self.all
    super.map { |z| create(z.name, z.utc_offset) }
  end

  def to_s
    translated_name = I18n.t(name, :scope => :timezones, :default => name)
    "(GMT#{formatted_offset}) #{translated_name}"
  end
end

view:

<%= time_zone_select :user, :time_zone, nil, :model => I18nTimeZone %>

locale file (/config/locales/ru.yml):

ru:
  timezones:
    "Midway Island": "Мидуэй"
    "Samoa": "Самоа"
    ....

But there are cases when original string includes some dots (".") Like "St. Petersburg" And I18n.t() tells me that translation is missing.

How can I avoid it?

+2  A: 

Just remove the dot for the translation keys.

def to_s
  translated_name = I18n.t(key, :scope => :timezones, :default => name)
  "(GMT#{formatted_offset}) #{translated_name}"
end

def key
  @key ||= name.gsub(/\./, "")
end


ru:
  timezones:
    "Midway Island": "Мидуэй"
    "Samoa": "Самоа"
    "St Petersburg" : "some one translate this"
Aaron Qian
Tnanks! It really works.
kshchepelin