forked from asfadmin/Discovery-MkDocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phrase-translate-to-md.py
87 lines (68 loc) · 3.06 KB
/
phrase-translate-to-md.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# python script.py path/to/directory_or_file key value
# https://pypi.org/project/mkdocs-i18n/
# https://github.com/phrase/phrase-python
import os
import re
import sys
import phrase_strings
from pprint import pprint
from pathlib import Path
def replace_key_in_file(file_path):
"""Open file_path and search for keys wrapped in double curly braces.
If a key is found, replace it with a value from the
dict returned by the load_locale function. If no value is found, replace the key with an empty string.
Then output a new file with the modified content. The filename should be the same as the original file, but with the
.locale as appended to the stem of the filepath.
"""
p = Path(file_path)
# Skip files that are not .key files
if p.suffix.lower() != '.key':
return
try:
with open(file_path, 'r') as file_in:
content = file_in.read()
list_locales = phrase_strings.list_locales()
for locale in list_locales:
data_locale = phrase_strings.load_locale(locale.name)
content_out = content
# Find all keys wrapped in double curly braces
# Example: {{ key }}
keys = re.findall(r"\{\{.*?}}", content)
# Loop through keys and replace them with values from the loaded locale
for key in keys:
# Remove curly braces from the key
key_without_curly_braces = key[2:-2].strip()
# Replace key with value from loaded locale
if key_without_curly_braces in data_locale:
content_out = content.replace(key, data_locale[key_without_curly_braces]['message'])
else:
content_out = content.replace(key, "")
# Output a new file with the modified content
new_file_name = f"{p.stem}.{locale.name}.md"
new_file_path = os.path.dirname(file_path) + '/' + new_file_name
with open(new_file_path, 'w') as file_out:
file_out.write(content_out)
print(f"File '{new_file_path}' processed successfully.")
except Exception as e:
print(f"An error occurred while processing '{file_path}': {e}")
def process_directories(directory_path):
for root, dirs, files in os.walk(directory_path):
# Ignore hidden files and directories
files = [f for f in files if not f[0] == '.']
dirs[:] = [d for d in dirs if not d[0] == '.']
for file in files:
file_path = os.path.join(root, file)
replace_key_in_file(file_path)
if __name__ == "__main__":
print('phase-translate-to-md.py starting')
if len(sys.argv) < 1:
print("Usage: python phrase-translate-to-md.py <directory_or_filename>")
sys.exit(1)
target_path = sys.argv[1]
if os.path.isfile(target_path):
replace_key_in_file(target_path)
elif os.path.isdir(target_path):
process_directories(target_path)
else:
pprint("target path:" + target_path)
print("Invalid input. Please provide a valid directory or filename.")