# download csv of data sources
require 'open-uri'
require 'csv'
require 'json'

# https://docs.google.com/spreadsheets/d/1ARJHGA0f5B_hgj8-CcU-eFPQgcBFFq-cG8rYlz-e2pc/edit#gid=0

# url =  "https://docs.google.com/spreadsheets/d/1ARJHGA0f5B_hgj8-CcU-eFPQgcBFFq-cG8rYlz-e2pc/export?format=csv"

# download = open(url)
# IO.copy_stream(download, 'localisations.csv')

# for each row

lines = CSV.open('localisations.csv').readlines

keys = lines.delete lines.first

translations = Hash.new

languages = keys[3..-1]
languages.each_with_index do |language, index|
    print(language)
    translations[language] = Hash.new
    lines.each do |line|
        key = line[0] || line[1]
        unless key.nil?
            translations[language][key] = line.at(3+index)
        end
    end
end


# export to json with file name
example_id = 1
translations.each do |iso_code, strings|
    File.open(iso_code+'.json', 'w') do |f|
        file_structure = { "iso_code" => iso_code, "translations" => strings, "id" => example_id }
        f.puts JSON.pretty_generate(file_structure)
    end
    example_id += 1
end


