From 361b5df256397d27c7f4f73a35031056a0adea66 Mon Sep 17 00:00:00 2001 From: Matchu Date: Sat, 26 Jan 2013 09:54:29 -0600 Subject: [PATCH] translate zones --- app/models/zone.rb | 2 +- lib/tasks/translate.rake | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 lib/tasks/translate.rake diff --git a/app/models/zone.rb b/app/models/zone.rb index 6f4e442b..83ec6c2e 100644 --- a/app/models/zone.rb +++ b/app/models/zone.rb @@ -23,6 +23,6 @@ class Zone < ActiveRecord::Base end def self.plainify_label(label) - label.delete('\- /').downcase + label.delete('\- /').parameterize end end diff --git a/lib/tasks/translate.rake b/lib/tasks/translate.rake new file mode 100644 index 00000000..b73823cc --- /dev/null +++ b/lib/tasks/translate.rake @@ -0,0 +1,36 @@ +require 'rocketamf/remote_gateway' + +namespace :translate do + def with_given_locale + I18n.with_locale(ENV['LOCALE']) do + language_code = I18n.neopets_language_code_for(I18n.locale) + unless language_code + raise "Locale #{I18n.locale.inspect} has no neopets language code" + end + + yield(language_code) + end + end + + desc "Download the Neopets zone data for the given locale" + task :zones => :environment do + with_given_locale do |neopets_language_code| + gateway = RocketAMF::RemoteGateway.new(Pet::GATEWAY_URL) + action = gateway.service('CustomPetService').action('getApplicationData') + envelope = action.request([]).post( + :headers => { + 'Cookie' => "lang=#{neopets_language_code}" + } + ) + application_data = envelope.messages[0].data.body + + zones_by_id = Zone.all.inject({}) { |h, z| h[z.id] = z ; h} + application_data[:zones].each do |zone_data| + zone = zones_by_id[zone_data[:id].to_i] + zone.label = zone_data[:label] + zone.plain_label = Zone.plainify_label(zone.label) + zone.save! + end + end + end +end