mirror of
https://github.com/EDCD/EDMarketConnector.git
synced 2025-04-18 18:07:37 +03:00
Refactored return based logic
A good chunk of the logic in here was returning from branches but still using else/elif/except branches to do an alternate behaviour.
This commit is contained in:
parent
553082c1fe
commit
4ede555c13
44
l10n.py
44
l10n.py
@ -10,10 +10,11 @@ import re
|
||||
import sys
|
||||
from codecs import StreamReaderWriter
|
||||
from collections import OrderedDict
|
||||
from contextlib import suppress
|
||||
from os.path import basename, dirname, exists, isdir, isfile, join, normpath
|
||||
from sys import platform
|
||||
from traceback import print_exc
|
||||
from typing import TYPE_CHECKING, Any, Dict, Iterable, Optional, Set, Union, cast
|
||||
from typing import TYPE_CHECKING, Dict, Iterable, Optional, Set, Union, cast
|
||||
|
||||
if TYPE_CHECKING:
|
||||
def _(x: str) -> str: ...
|
||||
@ -106,8 +107,8 @@ class _Translations:
|
||||
|
||||
if lang not in self.available():
|
||||
self.install_dummy()
|
||||
return
|
||||
|
||||
else:
|
||||
self.translations = {None: self.contents(cast(str, lang))}
|
||||
for plugin in os.listdir(config.plugin_dir):
|
||||
plugin_path = join(config.plugin_dir, plugin, LOCALISATION_DIR)
|
||||
@ -131,7 +132,6 @@ class _Translations:
|
||||
if not h:
|
||||
return {}
|
||||
|
||||
else:
|
||||
for line in h:
|
||||
if line.strip():
|
||||
match = _Translations.TRANS_RE.match(line)
|
||||
@ -163,7 +163,6 @@ class _Translations:
|
||||
|
||||
return self.translations.get(context, {}).get(x) or self.translate(x)
|
||||
|
||||
else:
|
||||
if __debug__:
|
||||
if self.translations[None] and x not in self.translations[None]:
|
||||
print(f'Missing translation: {x!r}')
|
||||
@ -186,11 +185,11 @@ class _Translations:
|
||||
|
||||
def available_names(self) -> Dict[Optional[str], str]:
|
||||
"""Available language names by code."""
|
||||
names = OrderedDict([
|
||||
names: Dict[Optional[str], str] = OrderedDict([
|
||||
(None, _('Default')), # Appearance theme and language setting
|
||||
])
|
||||
names.update(sorted(
|
||||
[(lang, self.contents(lang).get(LANGUAGE_ID, lang)) for lang in self.available()] + # type: ignore
|
||||
[(lang, self.contents(lang).get(LANGUAGE_ID, lang)) for lang in self.available()] +
|
||||
[(_Translations.FALLBACK, _Translations.FALLBACK_NAME)],
|
||||
key=lambda x: x[1]
|
||||
)) # Sort by name
|
||||
@ -203,13 +202,11 @@ class _Translations:
|
||||
if platform == 'darwin':
|
||||
return normpath(join(dirname(sys.executable), os.pardir, 'Resources'))
|
||||
|
||||
else:
|
||||
return join(dirname(sys.executable), LOCALISATION_DIR)
|
||||
|
||||
elif __file__:
|
||||
return join(dirname(__file__), LOCALISATION_DIR)
|
||||
|
||||
else:
|
||||
return LOCALISATION_DIR
|
||||
|
||||
def file(self, lang: str, plugin_path: Optional[str] = None) -> Optional[StreamReaderWriter]:
|
||||
@ -235,7 +232,6 @@ class _Translations:
|
||||
elif getattr(sys, 'frozen', False) and platform == 'darwin':
|
||||
return codecs.open(join(self.respath(), f'{lang}.lproj', 'Localizable.strings'), 'r', 'utf-16')
|
||||
|
||||
else:
|
||||
return codecs.open(join(self.respath(), f'{lang}.strings'), 'r', 'utf-8')
|
||||
|
||||
|
||||
@ -270,12 +266,10 @@ class _Locale:
|
||||
if not decimals and isinstance(number, numbers.Integral):
|
||||
return self.int_formatter.stringFromNumber_(number)
|
||||
|
||||
else:
|
||||
self.float_formatter.setMinimumFractionDigits_(decimals)
|
||||
self.float_formatter.setMaximumFractionDigits_(decimals)
|
||||
return self.float_formatter.stringFromNumber_(number)
|
||||
|
||||
else:
|
||||
if not decimals and isinstance(number, numbers.Integral):
|
||||
return locale.format('%d', number, True)
|
||||
|
||||
@ -293,15 +287,12 @@ class _Locale:
|
||||
if platform == 'darwin':
|
||||
return self.float_formatter.numberFromString_(string)
|
||||
|
||||
else:
|
||||
try: # TODO: This is awful.
|
||||
with suppress(ValueError):
|
||||
return locale.atoi(string)
|
||||
|
||||
except Exception:
|
||||
try:
|
||||
with suppress(ValueError):
|
||||
return locale.atof(string)
|
||||
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def preferredLanguages(self) -> Iterable[str]:
|
||||
@ -318,7 +309,10 @@ class _Locale:
|
||||
if platform == 'darwin':
|
||||
return NSLocale.preferredLanguages()
|
||||
|
||||
elif platform == 'win32':
|
||||
elif platform != 'win32':
|
||||
# POSIX
|
||||
lang = locale.getlocale()[0]
|
||||
return lang and [lang.replace('_', '-')] or []
|
||||
|
||||
def wszarray_to_list(array):
|
||||
offset = 0
|
||||
@ -333,24 +327,14 @@ class _Locale:
|
||||
|
||||
num = ctypes.c_ulong()
|
||||
size = ctypes.c_ulong(0)
|
||||
if (
|
||||
GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, ctypes.byref(num), None, ctypes.byref(size))
|
||||
and size.value
|
||||
):
|
||||
if GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, ctypes.byref(num), None, ctypes.byref(size)) and size.value:
|
||||
buf = ctypes.create_unicode_buffer(size.value)
|
||||
if (
|
||||
GetUserPreferredUILanguages(
|
||||
MUI_LANGUAGE_NAME, ctypes.byref(num), ctypes.byref(buf), ctypes.byref(size)
|
||||
)
|
||||
):
|
||||
|
||||
if GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, ctypes.byref(num), ctypes.byref(buf), ctypes.byref(size)):
|
||||
return wszarray_to_list(buf)
|
||||
|
||||
return []
|
||||
|
||||
else: # POSIX
|
||||
lang = locale.getlocale()[0]
|
||||
return lang and [lang.replace('_', '-')] or []
|
||||
|
||||
|
||||
# singletons
|
||||
Locale = _Locale()
|
||||
|
Loading…
x
Reference in New Issue
Block a user