-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLangFileCreator.py
204 lines (166 loc) · 7.34 KB
/
LangFileCreator.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
.lang file helper for Tanks: The Crusades
Author: Ton
Don't message me if you have any questions regarding this script,
because I don't know what I'm doing either.
Python version: 3.11.3 (lower than 3.10.x is also fine, but idk for sure)
"""
"""
Only keeps everything after the separator, removing everything else
and creates a new file as output.
Args:
inputFile (str): the name of the input text file + file type
outputFile (str): the name of the output text file + file type
separator (str): the character(s) after which everything needs to be removed
Output:
A file called {outputFile} containing the output.
"""
def stripLanguage(inputFile, outputFile, separator):
language = []
try:
# Read and store the input
with open(inputFile, "r", errors="replace") as sourceDocument:
# Source language name, append this separately to prevent writing an unnecessary newline
language.append(sourceDocument.readline().strip("\n"))
for line in sourceDocument:
# Only keep the lines with actual letters in it
if line != "\n" and line != " ":
language.append(line.split(separator, 1)[0])
# Write the stored input
with open(outputFile, "w") as outputDocument:
for line in language:
outputDocument.write(line + "\n")
print(f"{outputFile} has been successfully created!")
except Exception as e:
print(e)
"""
Merges the stripped English language file with the file containing your translation
Args:
englishWordsFile (str): the name of the input text file + file type
translatedWordsFile (str): the name of the output text file + file type
separator (str): the character(s) which separate the English
and translated words/phrases
Output:
The .lang file, suitable for Tanks: The Crusades
"""
def createLanguage(englishWordsFile, translatedWordsFile, separator):
englishLines = []
try:
# Read and store the input. This is the left side of the .lang file
with open(englishWordsFile, "r", errors="replace") as englishDocument:
# Skip the language name
next(englishDocument)
for line in englishDocument:
# Only keep the lines with actual letters in it
if line != "\n" and line != " ":
englishLines.append(line.rstrip("\n"))
except Exception as e:
print(e)
translatedLines = []
try:
# Read and store the input again. This is the right side of the .lang file
with open(translatedWordsFile, "r", errors="replace") as translatedDocument:
# Use the first line of the file as the name of the language
langName = translatedDocument.readline().strip("\n")
for line in translatedDocument:
# Only keep the lines with actual letters in it
if line != "\n" and line != " ":
translatedLines.append(line.rstrip("\n"))
except Exception as e:
print(e)
# Prevents index errors
if len(englishLines) == len(translatedLines):
try:
# Merge the 2 stored inputs with an "=" and write it to an output file
with open(f"{langName}.lang", "w") as outputLanguage:
outputLanguage.write(f"{langName}\n")
for i in range(len(englishLines)):
outputLanguage.write(
f"{englishLines[i]}{separator}{translatedLines[i]}\n"
)
print(f"{langName}.lang has been successfully created!")
except Exception as e:
print(e)
else:
print(
f"Error: {englishWordsFile} does not have an equal amount of lines compared to {translatedWordsFile}"
)
"""
For a list of English words separated with a newline, adds a character
(DEFAULT: '=') after all of them, and creates a file as its output.
Args:
nonSeparatedFile (str): the name of the input text file + file type
separatedFile (str): the name of the output text file + file type
separator (str): the character(s) which are added
Output:
A file called {separatedFile} containing the output.
"""
def addSeparatorToEnglish(nonSeparatedFile, separatedFile, separator):
englishLines = []
try:
# Read and store the input
with open(nonSeparatedFile, "r", errors="replace") as sourceDocument:
langName = sourceDocument.readline().strip("\n")
for line in sourceDocument:
if line != "\n" and line != " ":
englishLines.append(line.rstrip("\n"))
# Write the stored input
with open(separatedFile, "w") as outputDocument:
outputDocument.write(f"{langName}\n")
for line in englishLines:
outputDocument.write(f"{line}{separator}\n")
print(f"{separatedFile} has been successfully created!")
except Exception as e:
print(e)
if __name__ == "__main__":
# A whole bunch of input / output, this isn't really important
print("Quick(?) .lang file creator for Tanks: The Crusades")
choice = ""
while choice != "q":
print("---------------")
print("(1) Strip language/txt file: remove everything after and including the \"=\"-sign")
print("(2) Create a language: output of 1 + a translation required")
print("(3) Add an \"=\"-sign after every word")
print("(q) Quit program")
print("---------------")
choice = input(">")
if choice == "1":
print("---------------")
print("File to strip (INCLUDE file type):")
inputFile = input(">")
outputFile = f"{inputFile}_stripped.txt"
print("Separator (press ENTER if you want to keep it as \"=\"): ")
sepChoice = input(">")
if sepChoice == "":
separator = "="
else:
separator = sepChoice
stripLanguage(inputFile, outputFile, separator)
if choice == "2":
print("---------------")
print("Add the stripped file (INCLUDE file type):")
inputStrippedFile = input(">")
print("Add the translation file (INCLUDE file type):")
inputTranslatedFile = input(">")
print("Separator (press ENTER if you want to keep it as \"=\"): ")
sepChoice = input(">")
if sepChoice == "":
separator = "="
else:
separator = sepChoice
createLanguage(inputStrippedFile, inputTranslatedFile, separator)
if choice == "3":
print("---------------")
print("Add the stripped file (INCLUDE file type):")
inputFile = input(">")
outputFile = f"{inputFile}_separated.txt"
print("Separator (press ENTER if you want to keep it as \"=\"): ")
sepChoice = input(">")
if sepChoice == "":
separator = "="
else:
separator = sepChoice
addSeparatorToEnglish(inputFile, outputFile, separator)
print("Program has been terminated!")