Philipp Hörist pushed to branch master at gajim / gajim

Commits:

9 changed files:

Changes:

  • gajim/common/app.py
    ... ... @@ -49,7 +49,7 @@
    49 49
     from gajim.common import ged as ged_module
    
    50 50
     from gajim.common import types
    
    51 51
     from gajim.common.const import Display
    
    52
    -from gajim.common.i18n import LANG
    
    52
    +from gajim.common.i18n import get_default_lang
    
    53 53
     
    
    54 54
     if typing.TYPE_CHECKING:
    
    55 55
         from gajim.common.call_manager import CallManager
    
    ... ... @@ -294,7 +294,7 @@ def detect_dependencies() -> None:
    294 294
         for dep, val in _dependencies.items():
    
    295 295
             log('gajim').info('%-13s %s', dep, val)
    
    296 296
     
    
    297
    -    log('gajim').info('Used language: %s', LANG)
    
    297
    +    log('gajim').info('Used language: %s', get_default_lang())
    
    298 298
     
    
    299 299
     
    
    300 300
     def detect_desktop_env() -> str | None:
    

  • gajim/common/application.py
    ... ... @@ -58,8 +58,6 @@
    58 58
     from gajim.common.storage.events import EventStorage
    
    59 59
     from gajim.common.task_manager import TaskManager
    
    60 60
     from gajim.common.util.http import create_http_request
    
    61
    -from gajim.plugins.pluginmanager import PluginManager
    
    62
    -from gajim.plugins.repository import PluginRepository
    
    63 61
     
    
    64 62
     
    
    65 63
     class CoreApplication(ged.EventHelper):
    
    ... ... @@ -120,6 +118,9 @@ def _init_core(self) -> None:
    120 118
                 app.nicks[account] = app.settings.get_account_setting(account,
    
    121 119
                                                                       'name')
    
    122 120
     
    
    121
    +        from gajim.plugins.pluginmanager import PluginManager
    
    122
    +        from gajim.plugins.repository import PluginRepository
    
    123
    +
    
    123 124
             app.plugin_manager = PluginManager()
    
    124 125
             app.plugin_manager.init_plugins()
    
    125 126
             app.plugin_repository = PluginRepository()
    

  • gajim/common/i18n.py
    ... ... @@ -33,39 +33,23 @@
    33 33
     DOMAIN = 'gajim'
    
    34 34
     
    
    35 35
     
    
    36
    -def get_win32_default_lang() -> str:
    
    37
    -    import ctypes
    
    38
    -    windll = ctypes.windll.kernel32
    
    39
    -    return locale.windows_locale[windll.GetUserDefaultUILanguage()]
    
    40
    -
    
    41
    -
    
    42
    -def get_darwin_default_lang() -> str:
    
    43
    -    from AppKit import NSLocale
    
    44
    -
    
    45
    -    # FIXME: This returns a two letter language code (en, de, fr)
    
    46
    -    # We need a way to get en_US, de_DE etc.
    
    47
    -    return NSLocale.currentLocale().languageCode()
    
    36
    +def init() -> None:
    
    37
    +    _trans.init()
    
    48 38
     
    
    49 39
     
    
    50 40
     def get_default_lang() -> str:
    
    51
    -    if sys.platform == 'win32':
    
    52
    -        return get_win32_default_lang()
    
    53
    -
    
    54
    -    if sys.platform == 'darwin':
    
    55
    -        return get_darwin_default_lang()
    
    56
    -
    
    57
    -    return locale.getdefaultlocale()[0] or 'en'
    
    41
    +    return _trans.get_default_lang()
    
    58 42
     
    
    59 43
     
    
    60 44
     def get_rfc5646_lang(lang: str | None = None) -> str:
    
    61 45
         if lang is None:
    
    62
    -        lang = LANG
    
    46
    +        lang = _trans.get_default_lang()
    
    63 47
         return lang.replace('_', '-')
    
    64 48
     
    
    65 49
     
    
    66 50
     def get_short_lang_code(lang: str | None = None) -> str:
    
    67 51
         if lang is None:
    
    68
    -        lang = LANG
    
    52
    +        lang = _trans.get_default_lang()
    
    69 53
         return lang[:2]
    
    70 54
     
    
    71 55
     
    
    ... ... @@ -84,10 +68,6 @@ def is_rtl_text(text: str) -> bool:
    84 68
         return False
    
    85 69
     
    
    86 70
     
    
    87
    -def p_(context: str, message: str) -> str:
    
    88
    -    return _translation.pgettext(context, message)
    
    89
    -
    
    90
    -
    
    91 71
     def ngettext(s_sing: str,
    
    92 72
                  s_plural: str,
    
    93 73
                  n: int,
    
    ... ... @@ -100,7 +80,7 @@ def ngettext(s_sing: str,
    100 80
     
    
    101 81
         In other words this is a hack to ngettext() to support %s %d etc..
    
    102 82
         '''
    
    103
    -    text = _translation.ngettext(s_sing, s_plural, n)
    
    83
    +    text = _trans.translation.ngettext(s_sing, s_plural, n)
    
    104 84
         if n == 1 and replace_sing is not None:
    
    105 85
             return text % replace_sing
    
    106 86
     
    
    ... ... @@ -109,29 +89,69 @@ def ngettext(s_sing: str,
    109 89
         return text
    
    110 90
     
    
    111 91
     
    
    112
    -try:
    
    113
    -    locale.setlocale(locale.LC_ALL, '')
    
    114
    -except locale.Error as error:
    
    115
    -    print(error, file=sys.stderr)
    
    116
    -
    
    117
    -LANG = get_default_lang()
    
    118
    -if sys.platform == 'win32':
    
    119
    -    # Set the env var on Windows because gettext.find() uses it to
    
    120
    -    # find the translation
    
    121
    -    # Use LANGUAGE instead of LANG, LANG sets LC_ALL and thus
    
    122
    -    # doesn't retain other region settings like LC_TIME
    
    123
    -    os.environ['LANGUAGE'] = LANG
    
    124
    -
    
    125
    -
    
    126
    -package_dir = cast(Path, importlib.resources.files('gajim'))
    
    127
    -locale_dir = package_dir / 'data' / 'locale'
    
    128
    -
    
    129
    -try:
    
    130
    -    _translation = gettext.translation(DOMAIN, locale_dir)
    
    131
    -    _ = _translation.gettext
    
    132
    -    if hasattr(locale, 'bindtextdomain'):
    
    133
    -        locale.bindtextdomain(DOMAIN, locale_dir)
    
    134
    -except OSError:
    
    135
    -    _translation = gettext.NullTranslations()
    
    136
    -    _ = _translation.gettext
    
    137
    -    print('No translations found for', LANG, file=sys.stderr)
    92
    +class Translation:
    
    93
    +    def __init__(self) -> None:
    
    94
    +        self.translation = gettext.NullTranslations()
    
    95
    +        self._default_lang = None
    
    96
    +
    
    97
    +    def get_default_lang(self) -> str:
    
    98
    +        assert self._default_lang is not None
    
    99
    +        return self._default_lang
    
    100
    +
    
    101
    +    @staticmethod
    
    102
    +    def _get_win32_default_lang() -> str:
    
    103
    +        import ctypes
    
    104
    +        windll = ctypes.windll.kernel32
    
    105
    +        return locale.windows_locale[windll.GetUserDefaultUILanguage()]
    
    106
    +
    
    107
    +    @staticmethod
    
    108
    +    def _get_darwin_default_lang() -> str:
    
    109
    +        from AppKit import NSLocale
    
    110
    +
    
    111
    +        # FIXME: This returns a two letter language code (en, de, fr)
    
    112
    +        # We need a way to get en_US, de_DE etc.
    
    113
    +        return NSLocale.currentLocale().languageCode()
    
    114
    +
    
    115
    +    def _get_default_lang(self) -> str:
    
    116
    +        if sys.platform == 'win32':
    
    117
    +            return self._get_win32_default_lang()
    
    118
    +
    
    119
    +        if sys.platform == 'darwin':
    
    120
    +            return self._get_darwin_default_lang()
    
    121
    +
    
    122
    +        return locale.getdefaultlocale()[0] or 'en'
    
    123
    +
    
    124
    +    def init(self) -> None:
    
    125
    +        try:
    
    126
    +            locale.setlocale(locale.LC_ALL, '')
    
    127
    +        except locale.Error as error:
    
    128
    +            print(error, file=sys.stderr)
    
    129
    +
    
    130
    +        self._default_lang = self._get_default_lang()
    
    131
    +        if sys.platform == 'win32':
    
    132
    +            # Set the env var on Windows because gettext.find() uses it to
    
    133
    +            # find the translation
    
    134
    +            # Use LANGUAGE instead of LANG, LANG sets LC_ALL and thus
    
    135
    +            # doesn't retain other region settings like LC_TIME
    
    136
    +            os.environ['LANGUAGE'] = self._default_lang
    
    137
    +
    
    138
    +        package_dir = cast(Path, importlib.resources.files('gajim'))
    
    139
    +        locale_dir = package_dir / 'data' / 'locale'
    
    140
    +
    
    141
    +        try:
    
    142
    +            self.translation = gettext.translation(DOMAIN, locale_dir)
    
    143
    +            if hasattr(locale, 'bindtextdomain'):
    
    144
    +                locale.bindtextdomain(DOMAIN, locale_dir)
    
    145
    +        except OSError:
    
    146
    +            pass
    
    147
    +
    
    148
    +        self.install()
    
    149
    +
    
    150
    +    def install(self) -> None:
    
    151
    +        global _, g_, p_
    
    152
    +        _ = self.translation.gettext
    
    153
    +        g_ = self.translation.gettext
    
    154
    +        p_ = self.translation.pgettext
    
    155
    +
    
    156
    +
    
    157
    +_trans = Translation()

  • gajim/gtk/message_input.py
    ... ... @@ -34,7 +34,7 @@
    34 34
     from gajim.common.events import MessageSent
    
    35 35
     from gajim.common.ged import EventHelper
    
    36 36
     from gajim.common.i18n import _
    
    37
    -from gajim.common.i18n import LANG
    
    37
    +from gajim.common.i18n import get_default_lang
    
    38 38
     from gajim.common.styling import PlainBlock
    
    39 39
     from gajim.common.styling import process
    
    40 40
     from gajim.common.types import ChatContactT
    
    ... ... @@ -219,7 +219,7 @@ def _get_spell_checker_language(self,
    219 219
                 # use the default one
    
    220 220
                 lang = app.settings.get('speller_language')
    
    221 221
                 if not lang:
    
    222
    -                lang = LANG
    
    222
    +                lang = get_default_lang()
    
    223 223
     
    
    224 224
             assert isinstance(lang, str)
    
    225 225
             lang = Gspell.language_lookup(lang)
    

  • gajim/gajim.py → gajim/main.py
    ... ... @@ -95,6 +95,11 @@ def _check_required_deps() -> None:
    95 95
         check_version('sqlite', sqlite3.sqlite_version, _MIN_SQLITE_VER)
    
    96 96
     
    
    97 97
     
    
    98
    +def _init_translations() -> None:
    
    99
    +    import gajim.common.i18n
    
    100
    +    gajim.common.i18n.init()
    
    101
    +
    
    102
    +
    
    98 103
     def _init_gui(gui: str) -> None:
    
    99 104
         if gui == 'GTK':
    
    100 105
             _init_gtk()
    
    ... ... @@ -154,7 +159,7 @@ def _set_proc_title() -> None:
    154 159
                 libc.setproctitle('gajim')
    
    155 160
     
    
    156 161
     
    
    157
    -def main() -> None:
    
    162
    +def run() -> None:
    
    158 163
         if sys.platform != 'win32':
    
    159 164
             if os.geteuid() == 0:
    
    160 165
                 sys.exit('You must not launch gajim as root, it is insecure.')
    
    ... ... @@ -162,5 +167,6 @@ def main() -> None:
    162 167
         _check_required_deps()
    
    163 168
         _set_proc_title()
    
    164 169
         _set_env_vars()
    
    170
    +    _init_translations()
    
    165 171
         _init_gui('GTK')
    
    166 172
         _run_app()

  • gajim/gajim_remote.py → gajim/remote.py
    ... ... @@ -133,7 +133,7 @@ def create_arg_parser() -> argparse.ArgumentParser:
    133 133
         return parser
    
    134 134
     
    
    135 135
     
    
    136
    -def main() -> None:
    
    136
    +def run() -> None:
    
    137 137
         args = create_arg_parser().parse_args()
    
    138 138
         try:
    
    139 139
             result = call_method(args)
    

  • launch.py
    ... ... @@ -3,7 +3,7 @@
    3 3
     import subprocess
    
    4 4
     
    
    5 5
     import gajim
    
    6
    -from gajim.gajim import main
    
    6
    +import gajim.main
    
    7 7
     
    
    8 8
     try:
    
    9 9
         res = subprocess.check_output(
    
    ... ... @@ -12,4 +12,4 @@
    12 12
     except Exception:
    
    13 13
         pass
    
    14 14
     
    
    15
    -main()
    15
    +gajim.main.run()

  • pyproject.toml
    ... ... @@ -55,10 +55,10 @@ dev = [
    55 55
     ]
    
    56 56
     
    
    57 57
     [project.scripts]
    
    58
    -gajim-remote = "gajim.gajim_remote:main"
    
    58
    +gajim-remote = "gajim.remote:run"
    
    59 59
     
    
    60 60
     [project.gui-scripts]
    
    61
    -gajim = "gajim.gajim:main"
    
    61
    +gajim = "gajim.main:run"
    
    62 62
     
    
    63 63
     [tool.setuptools.packages.find]
    
    64 64
     include = ["gajim*"]
    
    ... ... @@ -146,8 +146,8 @@ include = [
    146 146
         "gajim/common/styling.py",
    
    147 147
         "gajim/common/task_manager.py",
    
    148 148
         "gajim/common/util/*",
    
    149
    -    "gajim/gajim.py",
    
    150
    -    "gajim/gajim_remote.py",
    
    149
    +    "gajim/main.py",
    
    150
    +    "gajim/remote.py",
    
    151 151
         "gajim/gtk/*",
    
    152 152
         "gajim/plugins/*",
    
    153 153
         "test/*",
    

  • win/misc/create-launcher.py
    ... ... @@ -98,8 +98,8 @@ def get_launcher_code(*, debug: bool) -> str:
    98 98
             "root_path = Path(sys.executable).parents[1];"
    
    99 99
             "from ctypes import windll;"
    
    100 100
             "windll.kernel32.SetDllDirectoryW(str(root_path / 'bin'));"
    
    101
    -        "from gajim import gajim;"
    
    102
    -        "gajim.main();");
    
    101
    +        "import gajim.main;"
    
    102
    +        "gajim.main.run();");
    
    103 103
         Py_Finalize();
    
    104 104
         return result;
    
    105 105
     }