-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyncLocalization.py
114 lines (71 loc) · 2.79 KB
/
SyncLocalization.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
#
# (c) Morthimer McMare a.k.a. JSO_x, 2023
#
# Base file:
# https://github.com/DRRP-Team/DRRP/blob/master/tools/updateOldLocalization.py
#
import csv
import sys
# Constants:
const_DIR = "./"
#const_LocalizationFileTypes = [ ".core", ".tips" ]
const_LocalizationFileTypes = [ "" ]
const_InputFile = "LANGUAGE.gzdoom400"
const_InputFilesPostfix = [ i + ".csv" for i in const_LocalizationFileTypes ]
const_OutputFile = "LANGUAGE.gzdoom330"
const_OutputFilesLanguages = [ ".enu", ".rus" ] # Other languages converting not supported yet.
const_OutputFilesPostfix = [ i for i in const_LocalizationFileTypes ]
# Files and functions:
loczFiles = []
def handleSpecialSequences( text: str ) -> str:
return text.replace( '"', '\\"' ).replace( '\n', '\\\n' ).replace( "ё", "е" ).replace( "—", "-" )
def writeout( fmt: str, loczText = [], toall = False ):
global loczFiles
if len( loczText ) == 0:
for outfile in loczFiles:
outfile.write( fmt )
elif toall:
locztext = loczText[ 0 ]
for outfile in loczFiles:
outfile.write( fmt % locztext )
else:
for i in range( len( loczText ) ):
if ( loczText[ i ] is not None ):
loczFiles[ i ].write( fmt % loczText[ i ] )
# Main cycle:
for localizationType in range( len( const_LocalizationFileTypes ) ):
# Opening output files:
loczPrefix = const_DIR + const_OutputFile
loczPostfix = const_OutputFilesPostfix[ localizationType ]
loczFiles = [
open( loczPrefix + const_OutputFilesLanguages[ 0 ] + loczPostfix, 'w' ),
open( loczPrefix + const_OutputFilesLanguages[ 1 ] + loczPostfix, 'w', encoding='cp1251', errors='replace' )
]
# Write headers:
writeout( "// This file is automatically generated by SyncLocalization.py.\n// Update the localization in the \"LANGUAGE.gzdoom400.csv\" instead.\n\n" )
writeout( "%s\n\n", [ "[enu default]", "[rus]" ] )
with open( const_DIR + const_InputFile + const_InputFilesPostfix[ localizationType ], encoding='utf-8' ) as _in:
rows = csv.reader( _in )
rows.__next__()
for currow in rows:
_, cellRemark, cellID, cellEn, cellRu = currow
if ( not cellRemark and not cellID ):
writeout( "\n" )
elif ( cellRemark and not cellID ):
writeout( "// %s\n", [ ( cellRemark ) ], True )
else:
isRusText = True if ( len( cellRu ) != 0 ) else False
rusTextCortage = ( cellID, handleSpecialSequences( cellRu ) ) if isRusText else None
writeout( "%-37s = \"%s\";", [
( cellID, handleSpecialSequences( cellEn ) ),
rusTextCortage
] )
if ( cellRemark ):
writeout( " // %s", [ cellRemark, cellRemark if isRusText else None ] )
writeout( "%s", [ "\n", "\n" if isRusText else None ] )
for outfile in loczFiles:
writeout( "\n" )
print( "File " + outfile.name + " updated successfully." )
for outfile in loczFiles:
outfile.close()
print( "Updated successfully." )