File: locale_utils.py

package info (click to toggle)
cashbox 0.3.7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 388 kB
  • sloc: python: 1,607; makefile: 79; xml: 57
file content (77 lines) | stat: -rwxr-xr-x 2,305 bytes parent folder | download | duplicates (2)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/python3

# locale_utils.py
#
# Copyright:
#   Copyright (C) 2024-2025 Bernd Schumacher <bernd@bschu.de>
#
# License: GPL-3.0+
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#   .
#   This package is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#   .
#   You should have received a copy of the GNU General Public License
#   along with this program. If not, see <https://www.gnu.org/licenses/>.
# Comment:
#   On Debian systems, the complete text of the GNU General
#   Public License version 3 can be found in
#   "/usr/share/common-licenses/GPL-3".

""" locale utils """

import os
import gettext
import locale
from copy import copy
from inspect import currentframe


def init_locale():
    """
    Set defaults for global variable "_"
    """
    ret = gettext.gettext  # allow _("...") to mark stings as translateable

    # set locale for all categories as defined in LANG
    locale.setlocale(locale.LC_ALL, "")

    # use local po directory if exists
    locale.bindtextdomain("cashbox", "po/locale" if os.path.isdir("po/locale")
                          else "/usr/share/locale")
    locale.textdomain("cashbox")
    gettext.bindtextdomain("cashbox", "po/locale" if os.path.isdir("po/locale")
                           else "/usr/share/locale")
    gettext.textdomain("cashbox")
    return ret


# init global variable _ to be included by other modules
if "_" not in globals():
    _ = init_locale()


def f(s: str) -> str:
    """
    There are problems when using gettext and f-string:

        name="world"
        _(f"hello {name}")

    This is not possible, because f-string will modify the string.
    And the modified string can not be found in the translation.
    This workaround allows:

        name="world"
        f(_("hello {name}"))

    """
    frame = currentframe().f_back
    kwargs = copy(frame.f_globals)
    kwargs.update(frame.f_locals)
    return s.format(**kwargs)