-
Notifications
You must be signed in to change notification settings - Fork 58
/
crowdin.py
71 lines (54 loc) · 2.42 KB
/
crowdin.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
# Setup: Project Settings > Facets > Python
# Or Right-click on "KingdomsX" project folder in Intellij > Add Framework Support > Python
# After doing this, wait for indexing to finish.
# Run: Right-click anywhere in this file and click "Run File in Python Console" (IntelliJ)
# Used Python version: 3.11.2
# Used libs: https://pyyaml.org/wiki/PyYAMLDocumentation
import os
import yaml
# Pyyaml doesn't indent lists w h a t?: https://github.com/yaml/pyyaml/issues/234
class CrowdinDumper(yaml.Dumper):
def increase_indent(self, flow=False, indentless=False):
return super(CrowdinDumper, self).increase_indent(flow, False)
def quotePaths(dumper, data):
if "/" in data:
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='"')
else:
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='')
yaml.add_representer(str, quotePaths)
crowdinFiles = []
guiSourcePath = r"core/src/main/resources/guis"
for subdir, _, files in os.walk(guiSourcePath):
# Language file
crowdinFiles.append({
"source": f"/core/src/main/resources/en.yml",
"translation": f"/resources/languages/%two_letters_code%/%two_letters_code%.yml"
})
subdir = subdir.replace('\\', '/')
# GUIs
for file in files:
translationDir = subdir[len(guiSourcePath):]
if file.endswith(".yml"):
crowdinFiles.append({
"source": subdir + '/' + file,
"translation": f"/resources/languages/%two_letters_code%/guis{translationDir}/{file}"
})
print(f"{subdir} - {translationDir} - {file}")
with open(r'crowdin.yml', 'w') as file:
file.write("# https://developer.crowdin.com/configuration-file/\n")
file.write("# Automatically generated by crowdin.py, do not edit.\n\n")
dumper = CrowdinDumper(stream=file, default_style=None,
default_flow_style=False,
canonical=False, indent=2, allow_unicode=True,
encoding='utf-8',
explicit_start=False, explicit_end=False, sort_keys=False,
width=None, line_break=None, version=None, tags=None
)
data = dict(files=crowdinFiles)
# yaml.dump(data=data, stream=file, Dumper=dumper)
try:
dumper.open()
dumper.represent(data)
dumper.close()
finally:
dumper.dispose()