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