@@ -159,26 +159,20 @@ def get_mofile(languages, localedir, domain=None):
159
159
for locale in languages :
160
160
if locale == "C" :
161
161
break
162
- if domain :
163
- basename = os .path .join (localedir , locale , "LC_MESSAGES" , domain )
164
- else :
165
- basename = os .path .join (localedir , locale )
162
+ basename = os .path .join (localedir , locale , "LC_MESSAGES" , domain ) if \
163
+ domain else os .path .join (localedir , locale )
166
164
# look for message catalog files, check timestamps
167
165
mofile = basename + ".mo"
168
- if os .path .isfile (mofile ):
169
- motime = os .path .getmtime (mofile )
170
- else :
171
- motime = 0
166
+ motime = os .path .getmtime (mofile ) if os .path .isfile (mofile ) else 0
172
167
pofile = basename + ".po"
173
- if os .path .isfile (pofile ):
174
- potime = os .path .getmtime (pofile )
175
- else :
176
- potime = 0
168
+ potime = os .path .getmtime (pofile ) if os .path .isfile (pofile ) else 0
169
+
177
170
# see what we've found
178
171
if motime < potime :
179
172
# compile
180
173
mo = msgfmt .Msgfmt (pofile ).get ()
181
- open (mofile , 'wb' ).write (mo )
174
+ with open (mofile , 'wb' ) as m :
175
+ m .write (mo )
182
176
elif motime == 0 :
183
177
# no files found - proceed to the next locale name
184
178
continue
@@ -202,17 +196,17 @@ def get_translation(language=None, tracker_home=None,
202
196
"""
203
197
mofiles = []
204
198
# locale directory paths
205
- if tracker_home is None :
206
- tracker_locale = None
207
- else :
208
- tracker_locale = os .path .join (tracker_home , "locale" )
199
+ tracker_locale = os .path .join (tracker_home , "locale" ) if \
200
+ tracker_home is not None else None
201
+
209
202
# get the list of locales
210
203
locales = find_locales (language )
211
204
# add mofiles found in the tracker, then in the system locale directory
212
205
if tracker_locale :
213
206
mofiles .append (get_mofile (locales , tracker_locale ))
214
- for system_locale in LOCALE_DIRS :
215
- mofiles .append (get_mofile (locales , system_locale , DOMAIN ))
207
+ mofiles += [get_mofile (locales , system_locale , DOMAIN )
208
+ for system_locale in LOCALE_DIRS ]
209
+
216
210
# we want to fall back to english unless english is selected language
217
211
if "en" not in locales :
218
212
locales = find_locales ("en" )
@@ -226,19 +220,18 @@ def get_translation(language=None, tracker_home=None,
226
220
translator = None
227
221
for mofile in mofiles :
228
222
try :
229
- mo = open (mofile , "rb" )
230
- if translator is None :
231
- translator = translation_class (mo )
232
- # the .mo file this translator loaded from
233
- translator ._file = mofile
234
- else :
235
- # note: current implementation of gettext_module
236
- # always adds fallback to the end of the fallback chain.
237
- fallback = translation_class (mo )
238
- fallback ._file = mofile
239
- translator .add_fallback (fallback )
240
- mo .close ()
241
- except IOError :
223
+ with open (mofile , "rb" ) as mo :
224
+ if translator is None :
225
+ translator = translation_class (mo )
226
+ # the .mo file this translator loaded from
227
+ translator ._file = mofile
228
+ else :
229
+ # note: current implementation of gettext_module
230
+ # always adds fallback to the end of the fallback chain.
231
+ fallback = translation_class (mo )
232
+ fallback ._file = mofile
233
+ translator .add_fallback (fallback )
234
+ except IOError : # noqa: PERF203
242
235
# ignore unreadable .mo files
243
236
pass
244
237
if translator is None :
0 commit comments