summaryrefslogtreecommitdiff
path: root/lib/inx/utils.py
blob: a7c98a6001333eff1583fca5a7ede7336a8253d9 (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
import errno
import gettext
import os
import sys
from os.path import dirname

from jinja2 import Environment, FileSystemLoader

from ..i18n import N_, locale_dir, translation as default_translation

_top_path = dirname(dirname(dirname(os.path.realpath(__file__))))
inx_path = os.path.join(_top_path, "inx")
template_path = os.path.join(_top_path, "templates")

current_translation = default_translation
current_locale = "en_US"


def build_environment():
    env = Environment(
        loader=FileSystemLoader(template_path),
        autoescape=True,
        extensions=['jinja2.ext.i18n']
    )

    env.install_gettext_translations(current_translation)
    env.globals["locale"] = current_locale

    if "BUILD" in os.environ:
        # building a ZIP release, with inkstitch packaged as a binary
        if sys.platform == "win32":
            env.globals["command_tag"] = '<command reldir="extensions">inkstitch/bin/inkstitch.exe</command>'
        else:
            env.globals["command_tag"] = '<command reldir="extensions">inkstitch/bin/inkstitch</command>'
    else:
        # user is running inkstitch.py directly as a developer
        env.globals["command_tag"] = '<command reldir="extensions" interpreter="python">inkstitch.py</command>'

    return env


def write_inx_file(name, contents):
    inx_locale_dir = os.path.join(inx_path, current_locale)

    try:
        os.makedirs(inx_locale_dir)
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise

    inx_file_name = "inkstitch_%s.inx" % name
    with open(os.path.join(inx_locale_dir, inx_file_name), 'w') as inx_file:
        print >> inx_file, contents.encode("utf-8")


def iterate_inx_locales():
    global current_translation, current_locale

    locales = sorted(os.listdir(locale_dir))
    for locale in locales:
        translation = gettext.translation("inkstitch", locale_dir, languages=[locale], fallback=True)

        # L10N If you translate this string, that will tell Ink/Stitch to
        # generate menu items for this language in Inkscape's "Extensions"
        # menu.
        magic_string = N_("Generate INX files")
        translated_magic_string = translation.ugettext(magic_string)

        if translated_magic_string != magic_string or locale == "en_US":
            current_translation = translation
            current_locale = locale
            yield locale