1#!/usr/local/bin/python3.8 2# 3# Creates localized default templates 4# (uses default.svg as base and reads localized strings directly from .po/.gmo files) 5# 6 7from __future__ import print_function 8from __future__ import unicode_literals # make all literals unicode strings by default (even in Python 2) 9 10import gettext 11import glob 12import os 13import shutil 14import sys 15from io import open # needed for support of encoding parameter in Python 2 16 17 18LAYER_STRING = 'Layer' 19 20 21if len(sys.argv) != 3: 22 sys.exit("Usage:\n %s ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR}" % sys.argv[0]) 23 24source_dir = sys.argv[1] 25binary_dir = sys.argv[2] 26 27 28# get available languages (should match the already created .gmo files) 29gmofiles = glob.glob(binary_dir + '/po/*.gmo') 30 31languages = gmofiles 32languages = [os.path.basename(language) for language in languages] # split filename from path 33languages = [os.path.splitext(language)[0] for language in languages] # split extension 34 35 36# process each language sequentially 37for language in languages: 38 # copy .gmo file into a location where gettext can find and use it 39 source = binary_dir + '/po/' + language + '.gmo' 40 destination_dir = binary_dir + '/po/locale/' + language + '/LC_MESSAGES/' 41 destination = destination_dir + 'inkscape.mo' 42 43 if not os.path.isdir(destination_dir): 44 os.makedirs(destination_dir) 45 shutil.copy(source, destination) 46 47# do another loop to ensure we've copied all the translations before using them 48for language in languages: 49 # get translation with help of gettext 50 translation = gettext.translation('inkscape', localedir=binary_dir + '/po/locale', languages=[language]) 51 translated_string = translation.gettext(LAYER_STRING) 52 53 if type(translated_string) != type(LAYER_STRING): # python2 compat (make sure translation is a Unicode string) 54 translated_string = translated_string.decode('utf-8') 55 56 # now create localized version of English template file (if we have a translation) 57 template_file = source_dir + '/share/templates/default.svg' 58 output_file = binary_dir + '/share/templates/default.' + language + '.svg' 59 60 if os.path.isfile(output_file): 61 os.remove(output_file) 62 if translated_string != LAYER_STRING: 63 with open(template_file, 'r', encoding='utf-8', newline='\n') as file: 64 filedata = file.read() 65 filedata = filedata.replace('Layer', translated_string) 66 with open(output_file, 'w', encoding='utf-8', newline='\n') as file: 67 file.write(filedata) 68 69 70# create timestamp file (indicates last successful creation for build system) 71timestamp_file = binary_dir + '/share/templates/default_templates.timestamp' 72if os.path.exists(timestamp_file): 73 os.utime(timestamp_file, None) 74else: 75 open(timestamp_file, 'a').close() 76