forked from EFForg/privacybadger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix_placeholders.py
executable file
·40 lines (28 loc) · 1.16 KB
/
fix_placeholders.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3
import json
from collections import OrderedDict
from glob import glob
SOURCE_LOCALE = 'src/_locales/en_US/messages.json'
def fix_locale(locale, placeholder_keys):
# read in locale, preserving existing ordering
with open(locale, 'r', encoding="utf-8") as f:
data = json.load(f, object_pairs_hook=OrderedDict)
# restore missing placeholders
for key in placeholder_keys:
if key in data and "placeholders" not in data[key]:
data[key]["placeholders"] = source_data[key]["placeholders"]
with open(locale, 'w', encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=4)
if __name__ == '__main__':
with open(SOURCE_LOCALE, 'r', encoding="utf-8") as f:
source_data = json.load(f, object_pairs_hook=OrderedDict)
# get keys of locale messages with placeholders
placeholder_keys = []
for key in source_data:
if "placeholders" in source_data[key]:
placeholder_keys.append(key)
# fix all locales
for locale in glob('src/_locales/*/*.json'):
if locale == SOURCE_LOCALE:
continue
fix_locale(locale, placeholder_keys)