Skip to content

Commit 07f0706

Browse files
authored
Fix bugs and add non-tab seperation with spaces
1 parent fa6b9be commit 07f0706

1 file changed

Lines changed: 66 additions & 13 deletions

File tree

MineOS Lang Translate.py

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
# English to other lang file converter.
2+
# Localization Translator Helper.
33
# -*- coding: utf-8 -*-
44

55
# Semi-compadable with Google Translate.
@@ -14,11 +14,11 @@
1414

1515
# Programmed by CoolCat467
1616

17-
__title__ = 'English to Lolcat lang file converter'
17+
__title__ = 'Localization Translator Helper'
1818
__author__ = 'CoolCat467'
19-
__version__ = '0.1.0'
20-
__ver_major__ = 0
21-
__ver_minor__ = 1
19+
__version__ = '1.0.0'
20+
__ver_major__ = 1
21+
__ver_minor__ = 0
2222
__ver_patch__ = 0
2323

2424
READ = 'English.lang'
@@ -37,37 +37,90 @@ def writeData(filename, data):
3737
writefile.write(data)
3838
writefile.close()
3939

40-
def readLang(filename):
40+
def readLang(filename, maxsplen=8, _recursion=False):
4141
"""Read database from filename and convert to dictionary."""
4242
# Read data from file
4343
data = readData(filename)
4444
# Seperate data into individual lines
4545
lines = data.split('\n\t')
46+
# If it's a broken file (not split by tabs)
47+
if len(lines) == 1:
48+
# Figure out where the spaces start
49+
s = data.index(' ')
50+
# Find the end of the spaces
51+
e = data.index(data[s:s+maxsplen].replace(' ', '')[0])
52+
# Use start and end to find the seperation and get line data back properly
53+
lines = data.split('\n'+' '*(e-s))
54+
# If somehow it's still broken, try to fix it but don't explode.
55+
if len(lines) == 1:
56+
if not _recursion:
57+
return readLang(filename, maxsplen*3, True)
58+
else:
59+
raise RuntimeError(f'Invalid File "{filename}"')
4660
# Remove first empty value ('')
4761
info = lines[1:]
4862
# Create dictionary to save to
4963
vsmap = {}
64+
# Define search function
65+
def seccond(string, target, v=0.5):
66+
# Try to find seccond instance of target
67+
res = string.find(target, round(-len(string)*v))
68+
# If index is invalid, return None
69+
if res == -1:
70+
return None
71+
# Catch invalid selections (not seccond)
72+
if string[res-2:res] == '= ':
73+
# Delete first one we found from search and try again
74+
lst = list(string)
75+
del lst[res]
76+
# with an offset of one because we deleted a character.
77+
return seccond(''.join(lst), target, v) + 1
78+
return res
5079
# Go through all lines from read file
80+
an = False
5181
for entry in info:
52-
# For each line, take off end and seperate variable to n and data to v
53-
n, v = entry[:-1].split(' = ')
54-
# Remove (") from start and end of value
55-
v = v[1:-1]
82+
# Get start of removeing index
83+
rm = seccond(entry, '"', 0.6)
84+
# Set entry to up to the seccond double quote
85+
# and then get everything after that point
86+
entry, rm = entry[:rm], entry[rm+1:]
87+
# Seperate our entry into the key and the value that's in double quotes
88+
n, v = entry.split(' = ')
89+
# Remove double quotes from the start and end of our value.
90+
if v.startswith('"'):
91+
v = v[1:]
92+
if v.endswith('"'):
93+
v = v[:-1]
94+
# If a linebreak character was in our removed characters,
95+
if rm.startswith('\n'):
96+
# add a fake linebreak back into the data
97+
v += '_n_'
5698
# Record value in dictionary saved as key to value
5799
vsmap[n] = v
58-
# Fix last one with wierdness
59-
vsmap[n] = vsmap[n][:-3]
100+
# Fix last one with wierdness with the closing curly bracket
101+
if vsmap[n].endswith('_n_'):
102+
vsmap[n] = vsmap[n][:-3]
60103
# Return created dictionary
61104
return vsmap
62105

63106
def writeLang(filename, data):
64107
"""Convert dictionary to database file string and save."""
65108
# Start with the open curly bracket
66109
info = ['{']
110+
# No linebreak char to add
111+
an = False
67112
# For the keys and values of the dictionary input,
68113
for k, v in ((k, data[k]) for k in data):
114+
# If there is a linebreak char, remove it and add it in in the proper place.
115+
if v.endswith('\n'):
116+
v = v[:-1]
117+
an = True
69118
# Add '<key> = "<value>",' to the line data list
70119
info.append('%s = "%s",' % (k, v))
120+
# Add linebreak char back
121+
if an:
122+
info[-1] += '\n'
123+
an = False
71124
# Merge line data together with linebreak and tab characters
72125
# and take off the final tab and and add linebreak and
73126
# the end curly bracket to filedata string
@@ -106,7 +159,7 @@ def run():
106159
# Add '#<key number>*<value>' to toPaste data
107160
toPaste += ('#%i*' % num) + data[key].replace('\n', '_n_')
108161
# Print for user and get translated back
109-
print('Somehow get the following translated:')
162+
print('Copy-paste the following into some sort of translator (text within single quotes):')
110163
print("'"+toPaste+"'")
111164
# Get translated back from user
112165
copied = input('Enter translated result: ')

0 commit comments

Comments
 (0)