summaryrefslogtreecommitdiff
path: root/lib/i18n.py
blob: 419c03dc09402703bede00f39544c182bffd19b0 (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
import sys
import os
from os.path import dirname, realpath
import gettext

_ = translation = None
locale_dir = None

# Use N_ to mark a string for translation but _not_ immediately translate it.
# reference: https://docs.python.org/3/library/gettext.html#deferred-translations
# Makefile configures pybabel to treat N_() the same as _()
def N_(message): return message

def _set_locale_dir():
    global locale_dir

    if getattr(sys, 'frozen', False):
        # we are in a pyinstaller installation
        locale_dir = sys._MEIPASS
    else:
        locale_dir = dirname(dirname(realpath(__file__)))

    locale_dir = os.path.join(locale_dir, 'locales')

def localize(languages=None):
    global translation, _

    translation = gettext.translation("inkstitch", locale_dir, fallback=True)
    _ = translation.gettext

_set_locale_dir()
localize()