Welcome to mirror list, hosted at ThFree Co, Russian Federation.

i18n.py « common « gajim - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b2026f680a0693f57ff90546d5b2169adb15d35e (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# Copyright (C) 2003-2014 Yann Leboulanger <asterix AT lagaule.org>
# Copyright (C) 2004 Vincent Hanquez <tab AT snarc.org>
# Copyright (C) 2005-2006 Nikos Kouremenos <kourem AT gmail.com>
# Copyright (C) 2009 Benjamin Richter <br AT waldteufel-online.net>
# Copyright (C) 2018 Philipp Hörist <philipp AT hoerist.com>
#
# This file is part of Gajim.
#
# Gajim 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; version 3 only.
#
# Gajim 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 Gajim. If not, see <http://www.gnu.org/licenses/>.

from __future__ import annotations

from typing import Iterator
from typing import Optional

import os
import sys
import locale
import gettext
import unicodedata
from pathlib import Path
from gi.repository import GLib

DOMAIN = 'gajim'
_translation = gettext.NullTranslations()


def get_locale_dirs() -> Optional[list[Path]]:
    if sys.platform == 'win32':
        return

    path = gettext.find(DOMAIN)
    if path is not None:
        # gettext can find the location itself
        # so we don’t need the localedir
        return

    if Path('/app/share/run-as-flatpak').exists():
        # Check if we run as flatpak
        return [Path('/app/share/')]

    data_dirs = [GLib.get_user_data_dir()] + GLib.get_system_data_dirs()
    return [Path(dir_) for dir_ in data_dirs]


def iter_locale_dirs() -> Iterator[Optional[Path]]:
    locale_dirs = get_locale_dirs()
    if locale_dirs is None:
        yield None
        return

    # gettext fallback
    locale_dirs.append(Path(sys.base_prefix) / 'share')

    found_paths: list[Path] = []
    for path in locale_dirs:
        locale_dir = path / 'locale'
        if locale_dir in found_paths:
            continue
        found_paths.append(locale_dir)
        if locale_dir.is_dir():
            yield locale_dir


def get_win32_default_lang() -> str:
    import ctypes
    windll = ctypes.windll.kernel32
    return locale.windows_locale[windll.GetUserDefaultUILanguage()]


def get_darwin_default_lang() -> str:
    from AppKit import NSLocale
    # FIXME: This returns a two letter language code (en, de, fr)
    # We need a way to get en_US, de_DE etc.
    return NSLocale.currentLocale().languageCode()


def get_default_lang() -> str:
    if sys.platform == 'win32':
        return get_win32_default_lang()

    if sys.platform == 'darwin':
        return get_darwin_default_lang()

    return locale.getdefaultlocale()[0] or 'en'


def get_rfc5646_lang(lang: Optional[str] = None) -> str:
    if lang is None:
        lang = LANG
    return lang.replace('_', '-')


def get_short_lang_code(lang: Optional[str] = None) -> str:
    if lang is None:
        lang = LANG
    return lang[:2]


def paragraph_direction_mark(text: str) -> str:
    '''
    Determine paragraph writing direction according to
    http://www.unicode.org/reports/tr9/#The_Paragraph_Level

    Returns either Unicode LTR mark or RTL mark.
    '''
    for char in text:
        bidi = unicodedata.bidirectional(char)
        if bidi == 'L':
            return '\u200E'
        if bidi in ('AL', 'R'):
            return '\u200F'

    return '\u200E'


def Q_(text: str) -> str:
    '''
    Translate the given text, optionally qualified with a special
    construction, which will help translators to disambiguate between
    same terms, but in different contexts.

    When translated text is returned - this rudimentary construction
    will be stripped off, if it's present.

    Here is the construction to use:
        Q_("?vcard:Unknown")

    Everything between ? and : - is the qualifier to convey the context
    to the translators. Everything after : - is the text itself.
    '''
    text = _(text)
    if text.startswith('?'):
        text = text.split(':', 1)[1]
    return text


def P_(context: str, message: str) -> str:
    return _translation.pgettext(context, message)


def ngettext(s_sing: str,
             s_plural: str,
             n: int,
             replace_sing: Optional[str] = None,
             replace_plural: Optional[str] = None) -> str:
    '''
    Use as:
        i18n.ngettext(
            'leave room %s', 'leave rooms %s', len(rooms), 'a', 'a, b, c')

    In other words this is a hack to ngettext() to support %s %d etc..
    '''
    text = _translation.ngettext(s_sing, s_plural, n)
    if n == 1 and replace_sing is not None:
        text = text % replace_sing
    elif n > 1 and replace_plural is not None:
        text = text % replace_plural
    return text


try:
    locale.setlocale(locale.LC_ALL, '')
except locale.Error as error:
    print(error, file=sys.stderr)

LANG = get_default_lang()
if sys.platform == 'win32':
    # Set the env var on Windows because gettext.find() uses it to
    # find the translation
    # Use LANGUAGE instead of LANG, LANG sets LC_ALL and thus
    # doesn't retain other region settings like LC_TIME
    os.environ['LANGUAGE'] = LANG

# Search for the translation in all locale dirs
for dir_ in iter_locale_dirs():
    try:
        _translation = gettext.translation(DOMAIN, dir_)
        _ = _translation.gettext
        if hasattr(locale, 'bindtextdomain'):
            locale.bindtextdomain(DOMAIN, dir_)
    except OSError:
        continue
    else:
        break

else:
    _ = _translation.gettext
    print('No translations found for', LANG, file=sys.stderr)
    print('Dirs searched: %s' % get_locale_dirs(), file=sys.stderr)