1import gettext as gettext_module
2import locale
3import os
4import sys
5import traceback
6
7import fsboot
8from fsbc.settings import Settings
9
10_initialized = False
11
12
13def initialize_locale(language=None):
14    global translations, _initialized
15    _initialized = True
16
17    if language is None:
18        language = Settings.instance()["language"]
19
20    print("[I18N] Initialize_locale language =", language)
21    loc = language
22    if not loc:
23        try:
24            loc, _charset = locale.getdefaultlocale()
25            print("[I18N] Locale is", loc)
26        except:
27            print("[I18N] Exception while checking locale")
28            loc = ""
29        if not loc:
30            loc = ""
31
32    if not loc and sys.platform == "darwin":
33        try:
34            # noinspection PyUnresolvedReferences
35            import CoreFoundation
36
37            c_loc = CoreFoundation.CFLocaleCopyCurrent()
38            loc = CoreFoundation.CFLocaleGetIdentifier(c_loc)
39        except Exception:
40            traceback.print_exc()
41        print("[I18N] OS X locale", loc)
42
43    dirs = [
44        os.path.join(fsboot.executable_dir(), "share"),
45        os.path.join(fsboot.executable_dir(), "..", "share"),
46    ]
47    if sys.platform == "darwin":
48        dirs.insert(
49            0, os.path.join(fsboot.executable_dir(), "..", "Resources")
50        )
51
52    locale_base = None
53    for dir_ in dirs:
54        check = os.path.join(dir_, "fs-uae-launcher", "share-dir")
55        print("[I18N] Checking", check)
56        if not os.path.exists(check):
57            continue
58        locale_base = os.path.join(dir_, "locale")
59        break
60    if not locale_base and getattr(sys, "frozen", False):
61        if not locale_base:
62            p = os.path.abspath(
63                os.path.join(fsboot.executable_dir(), "..", "..", "Locale")
64            )
65            if os.path.exists(p):
66                locale_base = p
67        if not locale_base:
68            p = os.path.abspath(
69                os.path.join(
70                    fsboot.executable_dir(), "..", "..", "Data", "Locale"
71                )
72            )
73            if os.path.exists(p):
74                locale_base = p
75        if sys.platform == "darwin":
76            # .app/Contents/Resources/Locale
77            p = os.path.abspath(
78                os.path.join(
79                    fsboot.executable_dir(), "..", "Resources", "Locale"
80                )
81            )
82            if os.path.exists(p):
83                locale_base = p
84
85    if locale_base:
86        print("[I18N] bindtextdomain fs-uae-launcher:", locale_base)
87        gettext_module.bindtextdomain("fs-uae-launcher", locale_base)
88
89    mo_path = None
90    if locale_base:
91        print(
92            "[I18N] find translations for",
93            loc,
94            "in local directory",
95            locale_base,
96        )
97        try:
98            mo_path = gettext_module.find(
99                "fs-uae-launcher", locale_base, [loc]
100            )
101        except Exception as e:
102            # a bug in openSUSE 12.2's gettext.py can cause an exception
103            # in gettext.find (checking len of None).
104            print(repr(e))
105    else:
106        print("[I18N]  No locale directory found")
107    print("[I18N] Path to mo file:", mo_path)
108
109    translations = gettext_module.translation(
110        "fs-uae-launcher", locale_base, [loc], fallback=True
111    )
112    print("[I18N] Translations object:", translations)
113
114
115def gettext(msg):
116    if not _initialized:
117        initialize_locale()
118    return translations.gettext(msg)
119
120
121def ngettext(n, msg1, msg2):
122    if not _initialized:
123        initialize_locale()
124    return translations.ngettext(n, msg1, msg2)
125
126
127translations = gettext_module.NullTranslations()
128_ = gettext
129