-
Notifications
You must be signed in to change notification settings - Fork 12
/
translation.py
53 lines (38 loc) · 1.78 KB
/
translation.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
41
42
43
44
45
46
47
48
49
50
51
52
53
import argparse
from pathlib import Path
import json
def convertToJson():
contentPath = Path(__file__).parent / 'content'
for contentFile in contentPath.glob('**/*'):
if contentFile.suffix != '.lr':
continue
with open(contentFile, 'r', encoding="utf-8") as content_file:
contents = content_file.read().split('\n---\n')
results = {}
for content in contents:
contentTyp, text = content.split(':', maxsplit=1)
results[contentTyp.strip()] = text
with open(contentFile.with_suffix('.json'), 'w', encoding="utf-8") as tr_file:
json.dump(results, tr_file, indent=2, ensure_ascii=False)
def convertToLr():
contentPath = Path(__file__).parent / 'content'
for contentFile in contentPath.glob('**/*'):
if contentFile.suffix != '.json':
continue
with open(contentFile, 'r', encoding="utf-8") as tr_file:
translations = json.load(tr_file)
translationList = []
for contentTyp, text in translations.items():
translationList.append(':'.join([contentTyp, text]))
contents = '\n---\n'.join(translationList).rstrip() + '\n'
with open(contentFile.with_suffix('.lr'), 'w', encoding="utf-8") as content_file:
content_file.write(contents)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Convert between json translation files and lektor content files")
parser.add_argument('--json', action='store_true', help='convert lektor content files to json files')
parser.add_argument('--lr', action='store_true', help='convert translation files back to lektor content files')
args = parser.parse_args()
if args.json:
convertToJson()
if args.lr:
convertToLr()