stop crashing when trying to translate item names
This commit is contained in:
parent
dcf254a78d
commit
c5c587fab1
2 changed files with 14 additions and 12 deletions
|
@ -389,12 +389,15 @@ class Item < ActiveRecord::Base
|
|||
!species_support_ids.empty?
|
||||
end
|
||||
|
||||
def origin_registry_info=(info)
|
||||
Rails.logger.debug("info! #{info}")
|
||||
def add_origin_registry_info(info, locale)
|
||||
# bear in mind that numbers from registries are floats
|
||||
species_support_strs = info['species_support'] || []
|
||||
self.species_support_ids = species_support_strs.map(&:to_i)
|
||||
|
||||
self.name_translations = {locale => info['name']}
|
||||
|
||||
attribute_names.each do |attribute|
|
||||
next if attribute == 'name'
|
||||
value = info[attribute.to_sym]
|
||||
if value
|
||||
value = value.to_i if value.is_a? Float
|
||||
|
@ -507,7 +510,7 @@ class Item < ActiveRecord::Base
|
|||
item.id = item_id
|
||||
items[item_id] = item
|
||||
end
|
||||
item.origin_registry_info = info_registry[item.id.to_s]
|
||||
item.add_origin_registry_info info_registry[item.id.to_s], I18n.default_locale
|
||||
item.current_body_id = pet_type.body_id
|
||||
|
||||
# Build and update the SWF
|
||||
|
|
|
@ -45,9 +45,10 @@ class Pet < ActiveRecord::Base
|
|||
options[:item_scope])
|
||||
end
|
||||
|
||||
def fetch_viewer_data(timeout=4)
|
||||
def fetch_viewer_data(timeout=4, locale=nil)
|
||||
locale ||= I18n.default_locale
|
||||
begin
|
||||
neopets_language_code = I18n.compatible_neopets_language_code_for(I18n.locale)
|
||||
neopets_language_code = I18n.compatible_neopets_language_code_for(locale)
|
||||
envelope = PET_VIEWER.request([name, 0]).post(
|
||||
:timeout => timeout,
|
||||
:headers => {
|
||||
|
@ -107,7 +108,7 @@ class Pet < ActiveRecord::Base
|
|||
|
||||
# Fetch registry data in parallel
|
||||
registries = Parallel.map(candidates.keys, :in_threads => 8) do |locale|
|
||||
viewer_data = I18n.with_locale(locale) { fetch_viewer_data }
|
||||
viewer_data = fetch_viewer_data(4, locale) # TODO: ew, don't copy-paste the default timeout
|
||||
[locale, viewer_data[:object_info_registry]]
|
||||
end
|
||||
|
||||
|
@ -126,14 +127,12 @@ class Pet < ActiveRecord::Base
|
|||
# Apply translations, and figure out what items are currently being worn
|
||||
current_items = Set.new
|
||||
registries.each do |locale, registry|
|
||||
I18n.with_locale(locale) do
|
||||
registry.each do |item_id, item_info|
|
||||
item = items_by_id[item_id.to_i]
|
||||
item.origin_registry_info = item_info
|
||||
item.add_origin_registry_info(item_info, locale)
|
||||
current_items << item
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@items = current_items
|
||||
Item.transaction { @items.each { |i| i.save! if i.changed? } }
|
||||
|
|
Loading…
Reference in a new issue