2015-09-19 00:59:51 +00:00
|
|
|
#! python2
|
|
|
|
import io, os, json, re
|
|
|
|
from collections import OrderedDict
|
|
|
|
|
2017-07-12 18:52:44 +00:00
|
|
|
with io.open('/_locales/en/messages.json', 'r', encoding='utf-8') as f:
|
2017-07-12 08:27:57 +00:00
|
|
|
items = json.load(f).items()
|
|
|
|
english = [(k, v['message']) for k, v in items if 'message' in v]
|
|
|
|
english_placeholders = [(k, v['placeholders']) for k,v in items
|
2015-09-19 00:59:51 +00:00
|
|
|
if 'placeholders' in v]
|
|
|
|
|
|
|
|
for locale_name in os.listdir('_locales'):
|
|
|
|
if locale_name == 'en':
|
|
|
|
continue
|
2017-07-12 08:27:57 +00:00
|
|
|
if not re.match(r'^\w{2}(_\w{2,3})?$', locale_name):
|
|
|
|
print('Skipped %s: not a locale dir' % locale_name)
|
|
|
|
continue
|
2017-07-12 18:52:44 +00:00
|
|
|
loc_path = '/_locales/' + locale_name + '/messages.json'
|
2015-09-19 00:59:51 +00:00
|
|
|
with io.open(loc_path, 'r+', encoding='utf-8') as f:
|
|
|
|
loc = json.load(f, object_pairs_hook=OrderedDict)
|
|
|
|
|
2017-07-12 08:27:57 +00:00
|
|
|
deduplicated = 0
|
|
|
|
for msgId, message in english:
|
|
|
|
if msgId in loc and loc[msgId].get('message', '') == message:
|
|
|
|
del loc[msgId]
|
|
|
|
deduplicated += 1
|
|
|
|
|
2015-09-19 00:59:51 +00:00
|
|
|
changed = 0
|
|
|
|
for msgId, placeholder in english_placeholders:
|
2017-07-12 08:27:57 +00:00
|
|
|
if msgId in loc and cmp(placeholder, loc[msgId].get('placeholders', None)) != 0:
|
2015-09-19 00:59:51 +00:00
|
|
|
loc[msgId]['placeholders'] = placeholder
|
|
|
|
changed += 1
|
|
|
|
|
2017-07-12 08:27:57 +00:00
|
|
|
if deduplicated > 0 or changed > 0:
|
2015-09-19 00:59:51 +00:00
|
|
|
f.seek(0)
|
|
|
|
json_str = json.dumps(loc, indent=1, ensure_ascii=False,
|
|
|
|
separators=(',', ': '), encoding='utf-8')
|
|
|
|
json_tabs = re.sub(r'^\s+', lambda s: s.group(0).replace(' ', '\t'),
|
|
|
|
json_str, flags=re.MULTILINE)
|
|
|
|
f.write(json_tabs)
|
|
|
|
f.truncate()
|
2017-07-12 08:27:57 +00:00
|
|
|
print('%s: %d deduplicated%s' % (
|
|
|
|
locale_name,
|
|
|
|
deduplicated,
|
|
|
|
', %d placeholder(s) added' % changed if changed else ''
|
|
|
|
))
|