Skip to content

Instantly share code, notes, and snippets.

@nhancv
Last active July 10, 2021 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhancv/7201651d4c8d0582925a1e9f288e77f4 to your computer and use it in GitHub Desktop.
Save nhancv/7201651d4c8d0582925a1e9f288e77f4 to your computer and use it in GitHub Desktop.
Export flutter intl arb file to csv
# python3 intl_arb2csv.py
import csv
import json
import os
def qrepr(s):
"repr with double-quotes"
# make one symbol special that isn't treated special in a string, in
# this case '@'.
# now modify the string in such a manner that quoting-characters
# become non-quoting characters, get the repr and reverse the whole
# operation.
# replace all '@'-signs with '@@'
s = s.replace('@', '@@')
# replace all '"' with '@+@'
s = s.replace('"', '+ at +')
# replace all "'" with '@:@')
s = s.replace("'", '- at -')
# get the repr
s = repr(s)[1:-1]
# and reverse the changes
s = s.replace('- at -', "'")
s = s.replace('+ at +', r'\"')
s = s.replace('@@', '@')
return '%s' % s
cmd_path = os.path.dirname(__file__)
# Init file
intl_name = 'intl_en'
arb_file_path = cmd_path + '/' + 'lib/l10n/' + intl_name + '.arb'
csv_file_path = cmd_path + '/' + intl_name + '.csv'
# Read arb
with open(arb_file_path, 'r', encoding='utf8') as f:
intl_dict = json.load(f)
for key in list(intl_dict):
if key.startswith('@') and key != '@@locale':
del intl_dict[key]
# Write to csv
with open(csv_file_path, mode='w', encoding='utf8') as csv_file:
fieldnames = ['key', 'value']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames, delimiter=';', lineterminator='\r\n', quotechar = "'", quoting=csv.QUOTE_ALL)
for key in list(intl_dict):
line = qrepr(intl_dict[key])
writer.writerow({'key': key, 'value': line})
# python3 intl_csv2arb.py
import csv
import json
import os
from datetime import datetime
cmd_path = os.path.dirname(__file__)
# Init file
intl_name = 'intl_ru'
csv_file_path = cmd_path + '/' + intl_name + '.csv'
new_arb_file_path = cmd_path + '/' + 'lib/l10n/' + intl_name + '.arb'
original_arb_file_path = cmd_path + '/' + 'lib/l10n/intl_en.arb'
# Read csv
with open(csv_file_path, mode='r', encoding='utf8') as csv_file:
fieldnames = ['key', 'value']
csv_reader = csv.DictReader(csv_file, fieldnames=fieldnames, delimiter=';', lineterminator='\r\n', quotechar = "'", quoting=csv.QUOTE_ALL)
new_dict = {}
for row in csv_reader:
new_dict[row['key']] = row
# Read origin arb
new_intl = {}
with open(original_arb_file_path, 'r', encoding='utf8') as f:
intl_dict = json.load(f)
for key in list(intl_dict):
if key in new_dict:
new_intl[key] = new_dict[key]['value'].replace('\\"', '\"').replace('\\n', '\n')
elif key == '@@last_modified':
today = datetime.utcnow().today().isoformat()
new_intl[key] = today
else:
new_intl[key] = intl_dict[key]
# Write to arb
with open(new_arb_file_path, 'w', encoding='utf8') as jsonFile:
json.dump(new_intl, jsonFile, ensure_ascii=False, indent=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment