Skip to content

Instantly share code, notes, and snippets.

@timusus
Created August 10, 2016 06:18
Show Gist options
  • Save timusus/f3a24714aa03025baf2aa1c651e357f0 to your computer and use it in GitHub Desktop.
Save timusus/f3a24714aa03025baf2aa1c651e357f0 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/ruby
## Created by Tim Malseed 16/07/2016
## This script depends on `onesky-ruby`
## Run gem install onesky-ruby
require 'onesky'
SOURCE_FILE_NAME = 'strings.xml'
API_PUBLIC_KEY = 'your_public_key'
API_SECRET_KEY = 'your_secret_key'
EXPORT_DIR = 'path/to/export/dir'
available_language_codes = []
downloaded_languages = []
skipped_languages = []
def getDir(language)
"#{EXPORT_DIR}/#{language.getFolderName}"
end
def getPathToFile(language)
"#{getDir(language)}/#{SOURCE_FILE_NAME}"
end
class Language
def initialize(json)
@english_name = json['english_name']
@progress = json['translation_progress'].delete('%').to_f / 100.0
@code = json['code']
@locale = json['locale']
@region = json['region']
end
def progress
@progress
end
def code
@code
end
def locale
@locale
end
def region
@region
end
def englishName
@english_name
end
def isEnglish
@locale.eql? 'en'
end
def hasRegion
@region.to_s.strip.length != 0
end
# Don't download if progress is less than 0.85 (The translation must be 85% translated or better)
def canDownload
(isEnglish && hasRegion) || (!isEnglish && progress > 0.85)
end
def getFormattedName
"#{@locale}#{@region.to_s.strip.length == 0 ? '' : "-r#{@region}"}"
end
def getFolderName
"values-#{getFormattedName}/"
end
def to_s
"Code:#{@code}\nLocale:#{@locale}\nRegion:#{@region}\nProgress:#{@progress}\nHas Region: #{hasRegion}"
end
end
# Create client
client = Onesky::Client.new(API_PUBLIC_KEY, API_SECRET_KEY)
# Get project info
project_groups = JSON.parse(client.list_project_group)
project_group_id = project_groups['data'][0]['id']
projects = JSON.parse(client.list_project project_group_id)
project_id = projects['data'][0]['id']
project = client.project(project_id)
# Delete existing translations
# Get languages
languages = JSON.parse(project.list_language)
# Do work
languages['data'].each do |child|
language = Language.new(child)
#Delete all existing strings first.
dir = getDir(language)
if File.exist?(dir)
print "\nDeleting: #{dir}"
FileUtils.rm_r dir
end
end
print "\n"
# Download new translations
# Get languages
languages = JSON.parse(project.list_language)
languages['data'].each do |child|
language = Language.new(child)
if language.canDownload
print "\nDownloading..\n#{language.englishName}\n"
resp = project.export_translation(source_file_name: SOURCE_FILE_NAME, locale: language.code)
dir = getDir(language)
Dir.mkdir(dir) unless File.exists?(dir)
File.open(getPathToFile(language), 'w') { |file| file.write(resp) }
available_language_codes << "\"#{language.getFormattedName}\""
downloaded_languages << '%-35s %s' % ["#{language.englishName}", '(%0.1f%% translated)' % [language.progress*100]]
next
else
skipped_languages << '%-35s %s' % ["#{language.englishName}", '(%0.1f%% translated)' % [language.progress*100]]
end
end
print "\n\nDownloaded:\n#{downloaded_languages.join(",\n")}"
print "\n\nSkipped:\n#{skipped_languages.join(",\n")}"
print "\n\nAvailable language codes: \n\n\"en\", #{available_language_codes.join(', ')}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment