summaryrefslogtreecommitdiffstats
path: root/share/templates/create_default_templates.py
blob: 11017c71fe90a73328ec8648807e43bd02d725e1 (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
#!/usr/bin/env python3
#
# Creates localized default templates
# (uses default.svg as base and reads localized strings directly from .po/.gmo files)
#

from __future__ import print_function
from __future__ import unicode_literals  # make all literals unicode strings by default (even in Python 2)

import gettext
import glob
import os
import shutil
import sys
from io import open  # needed for support of encoding parameter in Python 2


LAYER_STRING = 'Layer'


if len(sys.argv) != 3:
    sys.exit("Usage:\n  %s ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR}" % sys.argv[0])

source_dir = sys.argv[1]
binary_dir = sys.argv[2]


# get available languages (should match the already created .gmo files)
gmofiles = glob.glob(binary_dir + '/po/*.gmo')

languages = gmofiles
languages = [os.path.basename(language) for language in languages]  # split filename from path
languages = [os.path.splitext(language)[0] for language in languages]  # split extension


# process each language sequentially
for language in languages:
    # copy .gmo file into a location where gettext can find and use it
    source = binary_dir + '/po/' + language + '.gmo'
    destination_dir = binary_dir + '/po/locale/' + language + '/LC_MESSAGES/'
    destination = destination_dir + 'inkscape.mo'

    if not os.path.isdir(destination_dir):
        os.makedirs(destination_dir)
    shutil.copy(source, destination)

# do another loop to ensure we've copied all the translations before using them
for language in languages:
    # get translation with help of gettext
    translation = gettext.translation('inkscape', localedir=binary_dir + '/po/locale', languages=[language])
    translated_string = translation.gettext(LAYER_STRING)

    if type(translated_string) != type(LAYER_STRING):  # python2 compat (make sure translation is a Unicode string)
        translated_string = translated_string.decode('utf-8')

    # now create localized version of English template file (if we have a translation)
    template_file = source_dir + '/share/templates/default.svg'
    output_file = binary_dir + '/share/templates/default.' + language + '.svg'

    if os.path.isfile(output_file):
        os.remove(output_file)
    if translated_string != LAYER_STRING:
        with open(template_file, 'r', encoding='utf-8', newline='\n') as file:
            filedata = file.read()
        filedata = filedata.replace('Layer', translated_string)
        with open(output_file, 'w', encoding='utf-8', newline='\n') as file:
            file.write(filedata)


# create timestamp file (indicates last successful creation for build system)
timestamp_file = binary_dir + '/share/templates/default_templates.timestamp'
if os.path.exists(timestamp_file):
    os.utime(timestamp_file, None)
else:
    open(timestamp_file, 'a').close()