Skip to content

Instantly share code, notes, and snippets.

@marcgg
Created November 30, 2022 13:09
Show Gist options
  • Save marcgg/aa7118c700c6c8ee7c924a1b9b6f4d2c to your computer and use it in GitHub Desktop.
Save marcgg/aa7118c700c6c8ee7c924a1b9b6f4d2c to your computer and use it in GitHub Desktop.
require 'csv'
require 'open-uri'
GOOGLE_DOC_ID = "TO_COMPLETE"
PATH = "TO_COMPLETE"
puts "Downloading translations..."
url = "https://docs.google.com/spreadsheets/d/#{GOOGLE_DOC_ID}/gviz/tq?tqx=out:csv"
begin
content = URI.open(url).read
rescue OpenURI::HTTPError => e
puts "Invalid URL - #{e.message} - #{url}"
puts "Have you made sure to change the visibility?"
end
file = CSV.new(content)
data = {}
file.each_with_index do |line, i|
next if line == "" # Ignore blank lines
if i == 0 # Getting locales from header
line.each do |locale|
next if locale == "" # Ignore blank cells
data.merge!({ locale.downcase.to_sym => [] })
end
else # Loading data into memory
data.keys.each_with_index do |locale, locale_index|
data[locale] << [line[0], line[locale_index + 1]]
end
end
end
data.keys.each_with_index do |locale, i|
path = "#{PATH}/#{locale}.lproj/Localizable.strings"
puts "Setting data for #{locale} into #{path}..."
output = ""
data[locale].each do |item|
output = output + "\"#{item[0]}\" = \"#{item[1]}\";\n"
end
File.write(path, output) # File needs to already exist
end
puts "... all done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment