Skip to content

Instantly share code, notes, and snippets.

@kanru
Last active August 29, 2015 13:57
Show Gist options
  • Save kanru/9837387 to your computer and use it in GitHub Desktop.
Save kanru/9837387 to your computer and use it in GitHub Desktop.
Convert Gaia l10n repository to a CSV file and convert back
#!/usr/bin/python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import sys
import csv
def info(log):
sys.stderr.write(log)
def csv_to_properties(input_csv, l10n_dir):
if not os.path.exists(l10n_dir):
os.mkdir(l10n_dir)
with open(input_csv, 'r') as csvfile:
csv_reader = csv.reader(csvfile)
firstline = True
last_path = False
untranslated = 0
value_db = {}
# Column 0: index
# Column 1: id
# Column 2: path
# Column 3: zh-CN
# Column 4: path(en)
# Column 5: en-US
# Column 6: translation
info('Populating database...\n')
for row in csv_reader:
if value_db.has_key(row[5]) and row[6] != '' and value_db[row[5]] != row[6]:
info('Warning: inconsistent translation of key "{}"\n'.format(row[5]))
info(' previously translated as "{}"\n'.format(value_db[row[5]]))
info(' new translation is "{}"\n'.format(row[6]))
elif row[6] != '':
value_db[row[5]] = row[6]
info('Converting...\n')
csvfile.seek(0)
for row in csv_reader:
if firstline:
firstline = False
continue
if row[1].startswith('#') and last_path:
comment = True
prop_path = last_path
else:
comment = False
prop_path = os.path.join(l10n_dir, row[2])
last_path = prop_path
if not os.path.exists(os.path.dirname(prop_path)):
os.makedirs(os.path.dirname(prop_path))
with open(prop_path, 'a') as propfile:
if comment:
propfile.write('{}\n'.format(row[1]))
else:
if row[6] == '' and not value_db.has_key(row[5]):
untranslated += 1
info('id #{} {} in {} untranslated\n'.format(row[0], row[1], row[2]))
propfile.write('{} = {}\n'.format(row[1], row[5]))
elif value_db.has_key(row[5]):
propfile.write('{} = {}\n'.format(row[1], value_db[row[5]]))
else:
propfile.write('{} = {}\n'.format(row[1], row[6]))
info('done\n')
if untranslated > 0:
info('find {} untranslated string-id\n'.format(untranslated))
if __name__ == "__main__":
if len(sys.argv) != 3:
print "Usage: csv2prop.py <input.csv> <l10n_dir>"
exit(0)
csv_to_properties(sys.argv[1], sys.argv[2])
#!/usr/bin/python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import sys
import csv
class CsvWriter(object):
'''Write property files to CSV format'''
def __init__(self, infile):
self.output = open(infile, "w")
self.writer = csv.writer(self.output)
self.writer.writerow(['Id', 'Path', 'String', 'Translation'])
def convert(self, path):
prop = open(path)
for line in prop:
if line.startswith('#'):
self.writer.writerow([line.strip()])
continue
if line.strip() == '':
continue
l10n_id, string = line.split('=', 1)
self.writer.writerow([l10n_id.strip(), path, string.strip()])
def close(self):
self.output.close()
def properties_to_csv(gaia, csv):
csv_writer = CsvWriter(csv)
for root, dirs, files in os.walk(gaia):
for f in [f for f in files if f.endswith('properties')]:
csv_writer.convert(os.path.join(root, f))
csv_writer.close()
if __name__ == "__main__":
if len(sys.argv) != 3:
print "Usage: prop2csv.py <gaia_dir> <output.csv>"
exit(0)
properties_to_csv(sys.argv[1], sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment