#!/usr/local/bin/python3.7 # # 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: print('[VVD] 0. language = ' + language) # 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' print('[VVD] 1. destination_dir') if not os.path.isdir(destination_dir): os.makedirs(destination_dir) print('[VVD] 2. shutil.copy') shutil.copy(source, destination) # get translation with help of gettext print('[VVD] 3.0. translation') translation = gettext.translation('inkscape', localedir=binary_dir + '/po/locale', languages=[language]) print('[VVD] 3.1. translation') translated_string = translation.gettext(LAYER_STRING) print('[VVD] 3.2. translated_string = ' + translated_string) print('[VVD] 3.3. LAYER_STRING = ' + 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') print('[VVD] 3.4. translated_string.decode') print('[VVD] 3.5. translated_string = ' + translated_string) # 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' print('[VVD] 4. output_file = ' + output_file) if os.path.isfile(output_file): os.remove(output_file) print('[VVD] 5.1. os.remove') print('[VVD] 5.1. os.path.isfile') if translated_string != LAYER_STRING: print('[VVD] 5.2. translated_string != LAYER_STRING') with open(template_file, 'r', encoding='utf-8', newline='\n') as file: filedata = file.read() print('[VVD] 5.3. file.read') filedata = filedata.replace('Layer', translated_string) print('[VVD] 5.4. filedata.replace') with open(output_file, 'w', encoding='utf-8', newline='\n') as file: file.write(filedata) print('[VVD] 6. open') # 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()