Closed Jerska closed 4 years ago
#! /usr/bin/env ruby require 'pry' require_relative './zendesk.rb' SOURCE_LOCALE = 'en-us' TARGET_LOCALE = 'en-ca' class ZendeskAPI::Translation < ZendeskAPI::Resource namespace 'help_center' def self.add! client, type, id, translation client.connection.post( "/api/v2/help_center/#{type}/#{id}/translations.json", {translation: translation}, ) end end class Importer def initialize(app_name, email, token) @client = ZendeskAPI::Client.new do |config| config.url = "https://#{app_name}.zendesk.com/api/v2" config.username = email config.token = token config.retry = true end end def run(type) i = 0 @client.send(type).all! do |obj| puts "#{type}: #{i += 1}" translations = obj.translations.to_a! locales = translations.map { |t| t.locale }.sort next unless locales.include?(SOURCE_LOCALE) && !locales.include?(TARGET_LOCALE) copy = JSON.parse(translations[0].to_json) copy.delete 'id' copy.delete 'url' copy.delete 'html_url' copy['locale'] = TARGET_LOCALE ZendeskAPI::Translation.add!(@client, type, obj.id, copy) puts 'Added translation' end end end Importer.new(ENV['APP_NAME'], ENV['EMAIL'], ENV['TOKEN']).run('categories') Importer.new(ENV['APP_NAME'], ENV['EMAIL'], ENV['TOKEN']).run('sections') Importer.new(ENV['APP_NAME'], ENV['EMAIL'], ENV['TOKEN']).run('articles')