File: translations.py

package info (click to toggle)
siril 1.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,656 kB
  • sloc: ansic: 175,395; cpp: 28,654; python: 8,476; makefile: 974; xml: 879; sh: 280
file content (59 lines) | stat: -rw-r--r-- 1,864 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Copyright (C) 2005-2011 Francois Meyer (dulle at free.fr)
# Copyright (C) 2012-2026 team free-astro (see more in AUTHORS file)
# Reference site is https://siril.org
# SPDX-License-Identifier: GPL-3.0-or-later

"""
Translation submodule for Siril, providing gettext integration.
TODO: make use of this submodule with actual translations.
"""

import os
import gettext
import locale
from typing import Callable, TypeVar

T = TypeVar('T')

def setup_translations(domain: str = 'sirilpy', localedir: str = None) -> Callable:
    """
    Set up translations for the module based on the system locale.
    """
    # If no localedir is specified, find the module's parent directory
    if localedir is None:
        try:
            current_module_dir = os.path.dirname(os.path.abspath(__file__))
            localedir = os.path.join(os.path.dirname(current_module_dir), 'locale')
        except Exception:
            # Fallback to module root if locale directory can't be found
            localedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

    # Bind the text domain to the locale directory
    gettext.bindtextdomain(domain, localedir)
    gettext.textdomain(domain)

    # Try to set locale, with graceful error handling
    try:
        locale.setlocale(locale.LC_ALL, '')
    except locale.Error:
        pass

    # Get default locale
    lang, encoding = locale.getlocale()

    # Try to set locale with progressive fallback
    try:
        locale.setlocale(locale.LC_ALL, lang)
        return gettext.gettext
    except locale.Error:
        try:
            locale.setlocale(locale.LC_ALL, lang.split('_')[0])
            return gettext.gettext
        except locale.Error:
            return gettext.gettext

# Initialize the translation function
_ = setup_translations()

# Export these symbols
__all__ = ['_', 'setup_translations']