@@ -68,11 +68,11 @@ def __init__(self, storage_path, locale,
68
68
# Define the local storage filenames
69
69
self .storage_file = os .path .join (
70
70
storage_path , locale ,
71
- 'cache_{0 }_{1 }' .format (locale , repository_name ))
71
+ 'cache_{}_{}' .format (locale , repository_name ))
72
72
73
73
self .reference_storage_file = os .path .join (
74
74
storage_path , reference_locale ,
75
- 'cache_{0 }_{1 }' .format (reference_locale , repository_name ))
75
+ 'cache_{}_{}' .format (reference_locale , repository_name ))
76
76
77
77
def setRepositoryPath (self , path ):
78
78
'''Set path to repository.'''
@@ -107,8 +107,8 @@ def getRelativePath(self, file_name):
107
107
relative_path = file_name [len (self .repository_path ) + 1 :]
108
108
# Prepend storage_prefix if defined
109
109
if self .storage_prefix != '' :
110
- relative_path = '{0 }/{1 }' .format (self .storage_prefix ,
111
- relative_path )
110
+ relative_path = '{}/{}' .format (self .storage_prefix ,
111
+ relative_path )
112
112
113
113
return relative_path
114
114
@@ -138,14 +138,14 @@ def extractStrings(self):
138
138
# Ignore Junk
139
139
if isinstance (entity , parser .Junk ):
140
140
continue
141
- string_id = u'{0 }:{1 }' .format (
141
+ string_id = u'{}:{}' .format (
142
142
self .getRelativePath (file_name ), six .text_type (entity ))
143
143
if file_extension == '.ftl' :
144
144
if entity .raw_val is not None :
145
145
self .translations [string_id ] = entity .raw_val
146
146
# Store attributes
147
147
for attribute in entity .attributes :
148
- attr_string_id = u'{0 }:{1 }.{2 }' .format (
148
+ attr_string_id = u'{}:{}.{}' .format (
149
149
self .getRelativePath (file_name ),
150
150
six .text_type (entity ),
151
151
six .text_type (attribute ))
@@ -154,7 +154,7 @@ def extractStrings(self):
154
154
else :
155
155
self .translations [string_id ] = entity .raw_val
156
156
except Exception as e :
157
- print ('Error parsing file: {0 }' .format (file_name ))
157
+ print ('Error parsing file: {}' .format (file_name ))
158
158
print (e )
159
159
160
160
# Remove extra strings from locale
@@ -179,24 +179,28 @@ def storeTranslations(self, output_format):
179
179
180
180
if output_format != 'php' :
181
181
# Store translations in JSON format
182
+ json_output = json .dumps (self .translations , sort_keys = True )
182
183
with open ('{}.json' .format (self .storage_file ), 'w' ) as f :
183
- f .write (json . dumps ( self . translations , sort_keys = True ) )
184
+ f .write (json_output )
184
185
185
186
if output_format != 'json' :
186
187
# Store translations in PHP format (array)
187
188
string_ids = list (self .translations .keys ())
188
189
string_ids .sort ()
189
190
191
+ # Generate output before creating an handle for the file
192
+ output_php = []
193
+ output_php .append ('<?php\n $tmx = [\n ' )
194
+ for string_id in string_ids :
195
+ translation = self .escape (self .translations [string_id ])
196
+ string_id = self .escape (string_id )
197
+ output_php .append (
198
+ u"'{}' => '{}',\n " .format (string_id , translation ))
199
+ output_php .append ('];\n ' )
200
+
190
201
file_name = '{}.php' .format (self .storage_file )
191
202
with codecs .open (file_name , 'w' , encoding = 'utf-8' ) as f :
192
- f .write ('<?php\n $tmx = [\n ' )
193
- for string_id in string_ids :
194
- translation = self .escape (
195
- self .translations [string_id ])
196
- string_id = self .escape (string_id )
197
- line = u"'{0}' => '{1}',\n " .format (string_id , translation )
198
- f .write (line )
199
- f .write ('];\n ' )
203
+ f .writelines (output_php )
200
204
201
205
def escape (self , translation ):
202
206
'''
0 commit comments