Spotter-VM/sahana/sahana-lang-csv.py

35 lines
1.1 KiB
Python
Executable File

#!/usr/bin/env python3
from pprint import pprint
import argparse
import csv
import os
import xlrd
def main(args):
translations = {}
basename, extension = os.path.splitext(args.inputfile.lower())
if extension == '.csv':
with open(args.inputfile, 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='\'')
for line in reader:
translations[line[1]] = line[2]
elif extension == '.xls':
sheet = xlrd.open_workbook(args.inputfile).sheet_by_index(0)
for i in range(1, sheet.nrows):
row = sheet.row(i)
translations[row[1].value] = row[2].value
else:
print('Unknown input file extension')
return
with open('{}.py'.format(basename), 'w') as langfile:
print('# -*- coding: utf-8 -*-', file=langfile)
pprint(translations, langfile, 0, 8192)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Spotter Cluster Sahana Eden translation converter')
parser.add_argument('inputfile', help='CSV or XLS translation file.')
main(parser.parse_args())